-
-
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
Circular import + reexport = "error: Module '...' has no attribute '..."? #4049
Comments
Yes, creating some sort of forward references for imported names seems like a potential way to fix this. Another thing that might work is improving the processing order of modules within cycles. We should also make sure that any fix will also work in fine-grained incremental checking. |
Is there a workaround? I tried doing something like the following in from browse.services.database.models import dbx
#...
db: SQLAlchemy = dbx However, in my test code I still get errors like:
for code like: def setUp(self) -> None:
# ...
from browse.services import database
self.database_service = database
#
self.database_service.db.init_app(mock_app) #accessing db here causes the error Edit: I found the discussion on forward references ... so I should look into that next. |
@JukkaL After looking into it more, I now realize you probably meant that forward references might provide a fix internally to mypy - I couldn't see how to use them myself to avoid the error I mentioned above. Is there currently a workaround? |
@bbarker Yes, I was talking how we could fix this in mypy internally. Your workaround should work, if you access the attribute directly and not through a "module alias":
You seem to be assigning a module to an instance attribute, which isn't supported right now (#4291). |
There's an open issue on mypy for when there are circular imports AND re-exporting a name. This appears to be what we're seeing here. python/mypy#4049
We're running into this problem at Instagram, too. It's a high priority for us since it's causing type errors we can't easily work around, so I'm looking into it. It seems like we already have a sort of internal "forward reference" for imported names, in the form of (It's hard to workaround in our case because the error is raised in generated code, and the code generator has no way to tell whether this particular module will be involved in an import cycle, so it doesn't know whether to generate a |
This adds supports for some cases of importing an imported name within an import cycle. Originally they could result in false positives or false negatives. The idea is to use a new node type ImportedName in semantic analysis pass 1 to represent an indirect reference to a name in another module. It will get resolved in semantic analysis pass 2. ImportedName is not yet used everywhere where it could make sense and this doesn't fix all related issues with import cycles. Also did a bit of refactoring of type semantic analysis to avoid passing multiple callback functions. Fixes #4049. Fixes #4429. Fixes #4682. Inspired by (and borrowed test cases from) #4495 by @carljm. Supersedes #4495
This adds supports for some cases of importing an imported name within an import cycle. Originally they could result in false positives or false negatives. The idea is to use a new node type ImportedName in semantic analysis pass 1 to represent an indirect reference to a name in another module. It will get resolved in semantic analysis pass 2. ImportedName is not yet used everywhere where it could make sense and this doesn't fix all related issues with import cycles. Also did a bit of refactoring of type semantic analysis to avoid passing multiple callback functions. Fixes python#4049. Fixes python#4429. Fixes python#4682. Inspired by (and borrowed test cases from) python#4495 by @carljm. Supersedes python#4495
attempting to use sqlalchemy stubs fleshed out mypy.ini - verified works with vscode; updated mypy improved support for mypy-sqlalchemy questionable_model_change fixed issues except python/mypy#4049 adding some mypy workarounds for mypy #4049 and #4291
attempting to use sqlalchemy stubs fleshed out mypy.ini - verified works with vscode; updated mypy improved support for mypy-sqlalchemy questionable_model_change fixed issues except python/mypy#4049 adding some mypy workarounds for mypy #4049 and #4291
This is a minimal test case replicating what I'm experiencing in a larger project:
Python can work with it:
mypy, however, doesn't like it:
It fails all the same when I change
__init__.py
to useimport .. as ..
(I saw this pattern mentioned in #3981):Apologies if there's already an issue filled for this, I searched for one briefly and haven't found any.
mypy 0.521, CPython 3.6.2
The text was updated successfully, but these errors were encountered: