You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mypy thinks value is <nothing> when it isn't #15793
The code sample contains a bug. The f function is using type variable T in an incorrect manner. A type variable scoped to a function should always appear in the return type annotation. In your example, the correct annotation for parameter b is T[Any]. If you make this change, the code type checks without error.
It may be a good idea for mypy to emit an error when a type variable is used in an incorrect manner like this. There was a discussion about this in the typing forums a while back. If I remember correctly, the authors of pyright, pytype and pyre all decided to add such an error (or warning), but mypy does not emit an error in this case.
I don't think that is the issue here. Edited to return T (T[Any] doesn't make sense as far as I understand).
from __future__ import annotations
from typing import Generic, TypeVar, cast
T = TypeVar("T")
class A(Generic[T]):
pass
class B(A[str]):
something: int = 0
def f(b: A[T]) -> T:
if isinstance(b, B):
print(b.something)
raise NotImplementedError()
but this still fails:
error: <nothing> has no attribute "something" [attr-defined]
Activity
[-]mypy thinks value is `<nothing>` when it clearly isn't[/-][+]mypy thinks value is `<nothing>` when it isn't[/+]erictraut commentedon Aug 14, 2023
The code sample contains a bug. The
ffunction is using type variableTin an incorrect manner. A type variable scoped to a function should always appear in the return type annotation. In your example, the correct annotation for parameterbisT[Any]. If you make this change, the code type checks without error.It may be a good idea for mypy to emit an error when a type variable is used in an incorrect manner like this. There was a discussion about this in the typing forums a while back. If I remember correctly, the authors of pyright, pytype and pyre all decided to add such an error (or warning), but mypy does not emit an error in this case.
jack-tutor commentedon Aug 14, 2023
I don't think that is the issue here. Edited to return
T(T[Any]doesn't make sense as far as I understand).but this still fails:
Do not narrow types to Never with binder (python#18972)
Do not narrow types to Never with binder (#18972)