Skip to content

Commit 4c648d5

Browse files
authored
Refer to SPEC for stub usage (#29)
1 parent 7ed6a75 commit 4c648d5

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

README.md

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,42 +72,15 @@ from .edges import (sobel, scharr, prewitt, roberts,
7272

7373
Except that all subpackages (such as `rank`) and functions (such as `sobel`) are loaded upon access.
7474

75-
### Lazily load subpackages and functions from type stubs
75+
### Type checkers
7676

77-
Because static type checkers and IDEs will likely be unable to find your
78-
dynamically declared imports, you can use a [type
79-
stub](https://mypy.readthedocs.io/en/stable/stubs.html) (`.pyi` file) to declare
80-
the imports. However, if used with the above pattern, this results in code
81-
duplication, as you now need to declare your submodules and attributes in two places.
77+
Static type checkers and IDEs cannot infer type information from
78+
lazily loaded imports. As a workaround you can load [type
79+
stubs](https://mypy.readthedocs.io/en/stable/stubs.html) (`.pyi`
80+
files) with `lazy.attach_stub`.
8281

83-
You can infer the `submodules` and `submod_attrs` arguments (explicitly provided
84-
above to `lazy.attach`) from a stub adjacent to the `.py` file by using the
85-
`lazy.attach_stub` function.
86-
87-
Carrying on with the example above:
88-
89-
The `skimage/filters/__init__.py` module would be declared as such:
90-
91-
```python
92-
from ..util import lazy
93-
94-
__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)
95-
```
96-
97-
... and the adjacent `skimage/filters/__init__.pyi` stub would contain:
98-
99-
```python
100-
from . import rank
101-
from ._gaussian import gaussian, difference_of_gaussians
102-
from .edges import (sobel, scharr, prewitt, roberts,
103-
laplace, farid)
104-
```
105-
106-
Note that in order for this to work, you must be sure to include the `.pyi`
107-
files in your package distribution. For example, with setuptools, you would need
108-
to [set the `package_data`
109-
option](https://setuptools.pypa.io/en/latest/userguide/datafiles.html#package-data)
110-
to include `*.pyi` files.
82+
The SPEC [describes this workaround in more
83+
detail](https://scientific-python.org/specs/spec-0001/#type-checkers).
11184

11285
### Early failure
11386

0 commit comments

Comments
 (0)