Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix function containing PEP 593 Annotated with a string literal second arg causing 'Name "x" is not defined" error - #10777

Merged
JelleZijlstra merged 5 commits into
python:masterfrom
syastrov:9868/fix-func-with-annotated-containing-str-literal
Nov 26, 2021
Merged

Fix function containing PEP 593 Annotated with a string literal second arg causing 'Name "x" is not defined" error#10777
JelleZijlstra merged 5 commits into
python:masterfrom
syastrov:9868/fix-func-with-annotated-containing-str-literal

Conversation

@syastrov

@syastrov syastrov commented Jul 6, 2021

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 <[email protected]>
Co-authored-by: Jingchen Ye <[email protected]>
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