-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
__path__ missing in ModuleType for python 3 #4812
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
Comments
This wouldn't be technically true, e.g. |
You are right, and I had find about it earlier, but forgot to post. It may be harder to fix as it seems that the field is not always set and it can be missing. >>> import importlib
>>> importlib.import_module('xml').__path__
['/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml']
>>> importlib.import_module('string').__path__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'string' has no attribute '__path__' Note that The actual use case where I came up with this was when discovering plugins, where you need to use something like: for module_info in pkgutil.iter_modules(package_path, prefix=f"{qualified_name}."): and I ended up doing something like: package_path = getattr(package, "__path__", None)
if not package_path:
return |
Yup, |
Could you clarify then why At the very least I'd expect a comment on |
I don't think Python really has a concept of a PackageType, whereas ModuleType is a thing that exists at runtime (and so is easy to discriminate). If you returned a Union here, where one of the members of the Union doesn't exist at runtime (but is a subclass of the other), it'd be basically impossible to discriminate and get type checking to work. https://docs.python.org/3/library/types.html#types.ModuleType
To be honest, I have the good fortune to able to forget how Python 2 works, but I suspect those stubs are incorrect.
|
Ok, so Is this pattern pervasive (or at least somewhat common) in the stdlib? If so, we may need to extend the typing system to support something like type/interface optionals in Typescript where you can write:
Of course in Python the semantics would need to be different, because it raises |
In python 2,
ModuleType
declares to have an optional__path__
:typeshed/stdlib/2/types.pyi
Line 131 in 3d14016
However in Python 3, such field is missing:
typeshed/stdlib/3/_importlib_modulespec.pyi
Lines 35 to 42 in 3d14016
The text was updated successfully, but these errors were encountered: