Skip to content

Type annotation which is dynamic variable #3510

Answered by erictraut
amacfie asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, this should be flagged as an error.

The challenge for a type checker is in distinguishing between an implicit type alias and variables that are not meant to be type aliases.

For example, this is legal and the use of u in this type annotation should not be flagged as an error.

u = str
for t in [str, float]:
    def f(x: str) -> u:
        return t(x) + 1

    print(f("3.14"))

Pyright uses some heuristics that attempt to distinguish between a valid (implicit) type alias and a normal variable. Those heuristics are failing in this case, resulting in a false negative.

I'll clone this discussion as a bug report.

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
6 replies
@erictraut
Comment options

@amacfie
Comment options

@erictraut
Comment options

@amacfie
Comment options

@erictraut
Comment options

Answer selected by amacfie
Comment options

You must be logged in to vote
4 replies
@erictraut
Comment options

@amacfie
Comment options

@erictraut
Comment options

@amacfie
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants