-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add placeholder stubs for all sub-modules #17104
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this; in the future I think we could take it even further by having every attribute in the module enumerated and typed as Any
(until we have stubs for it) and removing the __getattr__
s. In practice I've noticed that partial stubs tend to cause accidental typos, i.e. things like
def f(x: np.array): ...
It would be nice to have mypy catch those.
numpy/tests/typing/reveal/modules.py
Outdated
reveal_type(np.warnings) # E: ModuleType | ||
|
||
# TODO: Remove when annotations have been added to `np.linalg.norm` | ||
reveal_type(np.linalg.norm) # E: Any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to deal with this one right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this just serves as a test case to check that the module-level __getattr__()
indeed returns Any
.
Can these be smoothly extended, for example, could a part of random be typed without completing the full module and have that part correctly picked up? |
Yup, in fact that's the current state of the main |
Good news: turns out the future is today! (see 4af8fd3) Explicit type annotations (i.e. ExamplesThe code used for grabbing all from types import ModuleType
from typing import Dict, Optional, List
import numpy as np
def get_submodule_all(super_module: ModuleType) -> Dict[str, Optional[List[str]]]:
"""Return a dict with all (public) sub-modules as keys and their respective `__all__` as values.
A value is set to `None` if sub-module does not define `__all__`.
"""
items = ((name, getattr(super_module, name)) for name in dir(super_module))
superset = {name for name, obj in items if not name.startswith("_") and isinstance(obj, ModuleType)}
sub_modules = superset - {"sys", "os", "warnings", "math"} # builtin modules
return {name: getattr(getattr(super_module, name), '__all__', None) for name in sub_modules} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 good idea to add placeholder annotations for all public modules.
I would prefer if we didn't blindly annotate everything that doesn't start with an underscore (not sure if that's what you aimed for). Not typing things that are meant to be private, or things that are deprecated, would be better as a signal to users they're in the wrong place.
See numpy/tests/test_public_api.py
for things that look public but really aren't.
I'm happy to help out but this has a new learning curve. I made an attempt on a single function from
A few questions if you have time:
|
These annotations will definitely have to be refined in the future (they are annotated as |
If #17117 gets merged then typing them as
#16759 is currently working on making
They do; you can just use |
Thanks @BvB93 . |
This pull request adds placeholder stub files for all (public) modules.
"Fixes" the issue encountered in #17103 by adding placeholder stubs for all currently untyped modules.
Note that all objects within the respective modules are currently typed as
Any
, so this is more of a duct tape solution until proper stubs are added.Examples
How the submodules were identified: