-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fix function containing PEP 593 Annotated with a string literal second arg causing 'Name "x" is not defined" error #10777
Fix function containing PEP 593 Annotated with a string literal second arg causing 'Name "x" is not defined" error #10777
Conversation
…d result in 'Name "x" is not defined' errors.
pass | ||
reveal_type(f1) # N: Revealed type is "def (a: builtins.str) -> Any" | ||
def f2(a: Annotated["str", "metadata"]): | ||
pass |
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.
Could you reveal_type() this one too?
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.
And maybe also add a testcase with a generic function, so we can confirm that a typevar in the first arg to Annotated is still picked up correctly.
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.
@JelleZijlstra I added a case. Is it correct?
…d arg causing 'Name "x" is not defined" error (python#10777) Fixes python#9868 When analyzing function definitions, mypy attempts to infer type variables. The code which did that also tried to look into the second type arg to `Annotated`, which, if a string literal, resulted in a lookup which may cause a 'Name "x" is not defined" error. Co-authored-by: 97littleleaf11 <97littleleaf11@users.noreply.github.com> Co-authored-by: Jingchen Ye <97littleleaf11@gmail.com>
Description
Fixes #9868
When analyzing function definitions, mypy attempts to infer type variables. The code which did that also tried to look into the second type arg to
Annotated
, which, if a string literal, resulted in a lookup which may cause a 'Name "x" is not defined" error.Test Plan
Test was added for this case, which previously failed.
Tests were added to ensure that the first type var to
Annotated
was still analyzed correctly.