Skip to content
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

Conversation

syastrov
Copy link
Contributor

@syastrov syastrov commented Jul 6, 2021

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.

pass
reveal_type(f1) # N: Revealed type is "def (a: builtins.str) -> Any"
def f2(a: Annotated["str", "metadata"]):
pass
Copy link
Member

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?

Copy link
Member

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.

Copy link
Collaborator

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?

@JelleZijlstra JelleZijlstra merged commit 40b6b9b into python:master Nov 26, 2021
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
…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>
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

Successfully merging this pull request may close these issues.

PEP 593 Annotated containing string literal is marked incorrect
3 participants