-
-
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
Update imported Vars in the third pass #5635
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I'm not sure if this fixes the problem in general. What if there are arbitrary references to the imported name that are bound during pass 2 in the module that contains the import -- I think that they don't necessarily get updated.
|
||
from b import tp | ||
x: tp | ||
reveal_type(x.x) # E: Revealed type is 'builtins.int' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you have tp('x')
in this file? Or reveal_type(tp)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this will not work. I was thinking about the Invalid type
problem. All references in type context will be correctly re-resolved in the third pass. While there is no such thing as forward reference in runtime context.
I however still think this fix may have some value:
- We have seen many complains about
Invalid type
, but few complains aboutCannot determine type
(the error that appears if you trytp('x')
) - The reason for above fact is that many of the import cycle exist only for annotations,
tp('x')
will fail at runtime anyway - This error quite often appears in stub files for large libraries, where we only care about type context
It is up to you of course.
@JukkaL As you requested elsewhere I added a detailed doctring, tests for still not fixed behavior, and updated the PR description. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the very clear docstrings! This looks good now (just left a few nits). Please try this with internal Dropbox codebases before merging, though.
mypy/semanal_pass3.py
Outdated
|
||
A = List[int] | ||
|
||
Are seen by mypy as variables, because it doesn't know yet that `List` refers to a type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar nit: use lower case 'a' in 'are'.
mypy/semanal_pass3.py
Outdated
In the second pass, such `Var` is replaced with a `TypeAlias`. But in import cycle, | ||
import of `A` will still refer to the old `Var` node. Therefore we need to update it. | ||
|
||
Note that this a partial fix that only fixes the "Invalid type" error when a type alias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'this a' -> 'this is a'
It would also be good to create a follow-up issue for the remaining problems, if one doesn't exist yet. |
It looks like an issue about |
This causes one redundant cast and one real error (bad annotation). |
Appveyor failure looks like a flake. |
Fixes #5275
Fixes #4498
Fixes #4442
This is a simple band-aid fix for
Invalid type
in import cycles where type aliases, named tuples, or typed dicts appear. Note that this is a partial fix that only fixes theInvalid type
error when a type alias etc. appears in type context. This doesn't fix errors (e.g.Cannot determine type of X
) that may appear if the type alias etc. appear in runtime context.The motivation for partial fix is two-fold:
NameExpr
andMemberExpr
in third pass is costly from performance point of view, and still nontrivial.