Skip to content

Change in behavior with __import__('pkgutil').extend_path() # type: Iterable[str] #7582

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

Closed
ericvw opened this issue Sep 29, 2019 · 11 comments
Closed

Comments

@ericvw
Copy link

ericvw commented Sep 29, 2019

I noticed a change in behavior between versions 0.720 and 0.730 when using pkgutil-style namespace packages.

For the following __init__.py file:

__path__ = __import__('pkgutil').extend_path(__path__, __name__)

mypy == 0.720 will report:

$ mypy --version
mypy 0.720

$ mypy --show-traceback __init__.py
__init__.py:2: error: Cannot determine type of '__path__'

Thus, I "resolved" this with the following in, in 0.720:

from typing import Iterable
__path__ = __import__('pkgutil').extend_path(__path__, __name__)  # type: Iterable[str]

With mypy ==0.730, I get the following:

$ 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.

@gvanrossum
Copy link
Member

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__.

@srittau @ilevkivskyi @JukkaL Thoughts?

@ilevkivskyi
Copy link
Member

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).

@gvanrossum
Copy link
Member

@ericvw If you can submit a PR to typeshed to change the return type of __import__ to Any (link to this issue) that would help.

@msullivan
Copy link
Collaborator

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.

@msullivan
Copy link
Collaborator

msullivan commented 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
Copy link
Author

ericvw commented Oct 17, 2019

I have created python/typeshed#3383. Thanks!

srittau pushed a commit to python/typeshed that referenced this issue Oct 17, 2019
From python/mypy#7582.

This partially reverts back the change in
0ee7c3c to have `__import__` return
`Any` instead of `ModuleType`.
@tigerjack
Copy link

I'm having the same behavior in my __init__.py file containing

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

and mypy 0.780

@PymZoR
Copy link

PymZoR commented 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
Copy link

Is there a workaround for this that doesn't require annotating every __path__ reference?

@gvanrossum
Copy link
Member

The last two comments seem to be about an unrelated issue. This issue is only about the type returned by ‘import’.

@mfarrugi
Copy link

Ah, looks like we're complaining specifically about __init__.py:2: error: Cannot determine type of '__path__' which fits into #1422.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants