You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Change in behavior with __import__('pkgutil').extend_path() # type: Iterable[str] #7582
$ mypy --version
mypy 0.730
mypy --show-traceback __init__.py
__init__.py:2: error: Module has no attribute "extend_path"
Found 1 error in 1 file (checked 1 source file)
When researching this issue, #7501 (comment) indicates that __import__ is not followed. However, I don't fully understand the change in behavior and had trouble using git-bisect to narrow it down.
The root cause is in python/typeshed#3188, which was incorporated into mypy 0.730. Before that PR, __import__() was declared as returning Any. Now it's declared as ModuleType. While Any was to wide (we do know that it will return a module), ModuleType is probably too narrow, at least in case the argument is a literal.
I'm thinking that maybe this should be rolled back -- it doesn't make sense to try and do strict type checking on the module returned by __import__.
I am fine either way. Short term we can set it back to Any and later probably add some special casing if the argument is a literal string or a final value (low priority probably).
I think making it return Any is a good idea, since that is probably needed for most uses. I don't think that it is even close to worth trying to support this properly as a special case.
Activity
gvanrossum commentedon Sep 29, 2019
The root cause is in python/typeshed#3188, which was incorporated into mypy 0.730. Before that PR,
__import__()was declared as returningAny. Now it's declared asModuleType. WhileAnywas to wide (we do know that it will return a module),ModuleTypeis probably too narrow, at least in case the argument is a literal.I'm thinking that maybe this should be rolled back -- it doesn't make sense to try and do strict type checking on the module returned by
__import__.@srittau @ilevkivskyi @JukkaL Thoughts?
ilevkivskyi commentedon Sep 30, 2019
I am fine either way. Short term we can set it back to
Anyand later probably add some special casing if the argument is a literal string or a final value (low priority probably).gvanrossum commentedon Oct 1, 2019
@ericvw If you can submit a PR to typeshed to change the return type of
__import__toAny(link to this issue) that would help.msullivan commentedon Oct 1, 2019
I think making it return
Anyis a good idea, since that is probably needed for most uses. I don't think that it is even close to worth trying to support this properly as a special case.msullivan commentedon Oct 1, 2019
So I'm going to close this issue, but I would merge a typeshed PR to change the return type of
__import__.ericvw commentedon Oct 17, 2019
I have created python/typeshed#3383. Thanks!
Revert __import__ function annotation to return type back to Any (#3383)
tigerjack commentedon Jun 30, 2020
I'm having the same behavior in my
__init__.pyfile containingand
mypy 0.780UnexpectedLobster commentedon Jul 27, 2020
Same with
mypy 0.782.This is troublesome when working on ROS because every catkin packages uses the following template for python modules: https://github.com/ros/catkin/blob/noetic-devel/cmake/templates/__init__.py.in
mfarrugi commentedon Aug 12, 2020
Is there a workaround for this that doesn't require annotating every
__path__reference?gvanrossum commentedon Aug 12, 2020
The last two comments seem to be about an unrelated issue. This issue is only about the type returned by ‘import’.
mfarrugi commentedon Aug 12, 2020
Ah, looks like we're complaining specifically about
__init__.py:2: error: Cannot determine type of '__path__'which fits into #1422.