Arm your library with a simple warning-call if users try to make use of uninstalled extras.
Project description
che[ck-e]xtra 🔎
When you're writing a python library, being able to define extras is sweet. People who are either new to programming in general or python's dynamic nature specifically might be caught off-guard by this feature though, so it might be nice to give them a little help when they run into
Traceback (most recent call last):
File "/home/me/code.py", line 1, in <module>
import foo
ModuleNotFoundError: No module named 'foo'
instead of letting them assume that you shipped them a broken distribution.
Usage
This package can be pip-installed
pip install chextra
If you decide that this is a feature that you want in your library, you
have to declare chextra
as a project dependency as well.
Expected Project Layout
Code that relies on extras being installed will usually sit in a sub-package as a kind of barrier between it and the rest of the code.
It makes sense for the sub-package to have the same name as the extra, since extras are supposed to denote a functionality, which coincides with package naming conventions.
Given a distribution foo
with an extra bar
, this would be the expected
structure:
foo
├── __init__.py
└── bar
├── __init__.py
└── code.py
Triggering a Warning
You only need add the following code to the __init__.py
of the sub-package:
import chextra
chextra.warn(pkg="foo", extras="bar")
And you're done. If someone tries to run import foo.bar
or from foo.bar import code
, they'll get the following message:
/home/me/my_project.py:3: UserWarning:
The feature you're trying to use requires the extra 'bar',
install it by running `pip install foo[bar]`.
chextra.warn(pkg="foo", extras="bar")
If the code is only executed within an attempted import and your extra has the same normalized name as the sub-package, you can also omit the arguments, since their values can be guessed from context.
import chextra
chextra.warn()
Focus
This package tries to be light-weight and not any fancier than it needs to. Installing dependencies automatically when they were found to be missing might sound great, but has been proven to be a bad idea.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file chextra-0.1.1.tar.gz
.
File metadata
- Download URL: chextra-0.1.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.9 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cbf320f0b4ecb1d431fd55c7ad1b68df33967643a3a0d42ec252f779a3acf2a |
|
MD5 | f3dae4f2d64d9ba4158c5854353cf3ec |
|
BLAKE2b-256 | 944e4d7f1db3523e6b9ed78ec48718c26f2708846f78a12d0e78687408f8f78d |
File details
Details for the file chextra-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: chextra-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.9 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52f69c038a068d19921f15054cf065a1ee686d69f1d5ee90c487380646d23d66 |
|
MD5 | c98e33925e219c48dd8b8b53c78d82a9 |
|
BLAKE2b-256 | 51fa463cb40b31de28048374b8670112fcac31af206d99eec5e90664add6221c |