API Reference
Some Extra Help¶
Given that extras are not all that common in most projects, this package offers some help to developers and users who are trying to call code that is gated by one.
Functions¶
dependencies_grouped_by_extra(pkg)
cached
¶
Collect all required dependencies.
Note that this function does not inspect the current python environment, it only checks the installation metadata. In other words, it doesn't find what is installed here, only what should be, and why.
Dependencies that have no extra are collected under the empty string.
Parameters:
-
pkg
(
str
) –Name of the inspected distribution.
Returns:
-
dict[str, list[str]]
–All dependencies of the supplied distribution grouped by their extra, or
""
if they have none.
Raises:
-
importlib.metadata.PackageNotFoundError
–If the supplied package name does not identify a currently installed package.
Source code in src/chextra/__init__.py
warn(pkg=None, extras=None, *, eager=False)
¶
Raise a Warning
if a user imports a package relying on uninstalled extras.
Call this function in the __init__.py
of a sub-package which contains code that
relies on an extra being installed. In order to be reachable, it must be executed
before any third party package imports take place. Any subsecuent ImportError
s will
still be raised, the warning merely tries to give users a hint why their code might be
failing.
Given the following package structure:
andfoo/bar/code.py
containing a 3rd party package import baz
defined in the
extra bar
, you'd put these two lines into foo/bar/__init__.py
:
with foo
being the name of your distribution and bar
the name of the extra.
If the third parameter, eager
, is left in its default of False
and a user
only installed foo
and not foo[bar]
, the following will happen
>>> import foo.bar # prints a UserWarning telling to install foo[bar]
>>> import foo.bar.code # raise an ImportError on "baz" not being installed
If eager
were set to True
, the first line would have raised an ImportError for
baz
already.
In this particular example, it would also be possible to call chextra.warn
without any parameters:
By inspecting the callstack, guessing the distribution name is under normal circumstances straight forward. For the name of the extra, it is not unusual to call the sub-package the same as the extra, so that name can be guessed from the context here as well. It is safe to use when your package was installed properly, but might be a hassle if your dev-environment or test setup executes your files directly instead.
Parameters:
-
pkg
(
str | None
) –Name of the installable distribution.
-
extras
(
str | list[str] | None
) –Name of the extra this call should be guarding against. Can specify multiple as a list.
-
eager
(
bool
) –If set to
True
, manually raise an ImportError listing missing 3rd party packages.
Source code in src/chextra/__init__.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|