forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use __getattr__ to mark partial stub packages (python#5231)
There is a problem with annotating large frameworks -- they are large. Therefore it is hard to produce good stubs in a single pass. A possible better workflow would be to allow indicating that a given (sub-)package is incomplete. I propose to use `__getattr__` for the role of such indicator. A motivation is that currently adding a `__getattr__` to `package/__init__.pyi` already makes `from package import mod` work, but `import package.mod` still fails (plus simplicity of implementation). Here are the rules that I propose: * One can declare a (sub-)package as incomplete by adding a `__getattr__` to its `__init__.pyi` * If the return type of this function is `types.ModuleType` or `Any`, we assume that all imports from this (sub-)package succeed. * Incomplete package can contain a complete subpackage: ``` # file a/__init__.pyi from types import ModuleType def __getattr__(attr: str) -> ModuleType: ... # file a/b/__init__.pyi # empty (i.e. complete package) # file main.py import a.d # OK import a.b.c # Error module not found ``` Note: these rules apply only to stubs (i.e. `.pyi` files). I add several tests to illustrate this behaviour. This PR shouldn't give any significant performance penalty because the added parsing/loading only happens when an error would be reported (for our internal workflow the penalty will be zero because of the flags we use). This PR will allow gradually adding stub modules to a large framework package, without generating loads of false positives for user code. Note: PEP 561 introduces the notion of a partial stub package, implemented in python#5227. I think however this is a bit different use case that I don't want to mix with this one for two reasons: * Partial packages in PEP 561 are mainly focused on interaction between stubs and inline/runtime packages. * The proposed feature may be also used in typeshed, not only for installed stub packages.
- Loading branch information
1 parent
6d8d50c
commit 4c3f800
Showing
4 changed files
with
189 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters