-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Module X has no attribute Y when using star imports #4930
Comments
I can still reproduce this with |
I looked into this for a bit. Here is a test that reproduces the problem: JelleZijlstra@6b124e2. The problem goes away when one uses I think what's going on here is as follows:
This could be fixable with an additional semanal pass or something like a callback in pass 1 that runs when the module we're import_alling from is processed, and adds names to the module that is using import_all. |
My branch master...JelleZijlstra:circularstar has a fix for the original bug now, but it introduces various new bugs. I don't understand for now what's going on in some of those tests. I'll take another look at this later. |
The reason why my code is buggy is that if there is a circular dependency involving This doesn't seem easy to solve. Strictly speaking, mypy should follow the same sequence as Python does at import time: start with one file, then if there is an import, switch to the imported file, then return to the original file. But that would require a full rewrite of pass 1 of semantic analysis and probably a broader change in mypy's build pipeline. Another solution could be to declare that mypy does not support |
declare that mypy does not support import * in an import cycle
I like that.
|
not supporting |
Clarification: we'd document that we don't support `import *` *in an import
cycle*. It seems expendable, since you can add an explicit import where
necessary.
|
ok, the effect I'm seeing here is then caused by something else. thanks! Click to expand # cat <<EOF > ./mypy.ini
[mypy]
strict_optional = True
no_implicit_optional = True
ignore_missing_imports = True
warn_incomplete_stub = True
check_untyped_defs = True
show_error_context = True
EOF
# cat test_foo.py
from nose.tools import *
def test_foo():
eq_(1,1)
# nosetests test_foo.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
# mypy --config-file=mypy.ini test_foo.py
test_foo.py: note: In function "test_foo":
test_foo.py:3: error: Name 'eq_' is not defined |
This might be fixed by the new semantic analyzer (experimental version available on GitHub mypy master by using |
I have the same issue. I tried with |
If you do decide to drop support for I would really rather mypy did not drop support for |
We are not planning to drop support for |
I'm dropping the new-semantic-analyzer label since this doesn't seem specific to it. |
Would it be any easier to constrain support for |
This happens while using pygame:
results in
even though the constant is there. The type hint is also properly resolved by VS Code. The constant is explicitly re-exported using |
Any update on this? If anything, there really should be something to tell the user what the issue is, as I just spent 20 minutes trying to figure out the cause. Though, best would be if Also, as a note: pylint seems completely fine with this behavior. |
FWIW the original example passes cleanly on current master (both with and without |
mypy seems to emit a spurious warning that it can't find an attribute of a module that should be imported via a star import and where another import refers to it.
Input
$ head mypackage/*
==> mypackage/__init__.py <==
==> mypackage/submod1.py <==
==> mypackage/submod2.py <==
Current Output
(haven't checked with master, but I couldn't find any recently closed issues that seemed to talk about something like this)
Expected Output
No error from mypy.
As per the comments in the
__init__.py
, if some of the imports are tweaked, then mypy is happy. It doesn't seem to be sated by an__all__
to help the star import. I don't quite understand why not importingmypackage.submod2
causes the error to go away as well; it's not like there's a circular import between 1 and 2 where the names in the module aren't defined before it imports (i.e. if you moved the...import greet
line above the other, Python would choke)The text was updated successfully, but these errors were encountered: