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

false error "Name "..." is not defined" when a valid symbol is used in a type position #11520

Open
KotlinIsland opened this issue Nov 11, 2021 · 3 comments
Labels
bug mypy got something wrong diagnostics

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Nov 11, 2021

from typing import cast

class Test:
    a: int
    
t = Test()

cast(t.a, int)  # error: Name "t.a" is not defined

This leave me being like:
image

Till I realize that I got the args the wrong way around.

@KotlinIsland KotlinIsland added the bug mypy got something wrong label Nov 11, 2021
@sobolevn
Copy link
Member

This looks ok to me: Name "t.a" is indeed not defined as a type name.

What would be a better error message? Please, share your ideas.

@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Nov 11, 2021

"t.a" is not valid as a type

The current message doesn't clearly indicate that you are using a value in a type position.

@DetachHead
Copy link
Contributor

yeah this seems odd, if we create our own cast function we get the correct error:

from typing import Any, TypeVar, cast


class Test:
    a: int


t = Test()
_T = TypeVar("_T")


def cast2(typ: type[_T], val: Any) -> _T:
    ...


cast2(t.a, int)  # error: Argument 1 to "cast2" has incompatible type "int"; expected "Type[<nothing>]"  [arg-type]
cast(t.a, int)  # error: Name "t.a" is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong diagnostics
Projects
None yet
Development

No branches or pull requests

4 participants