-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrong
Description
The following example fails to type-check with mypy and pyright, but succeeds with ty:
from dataclasses import dataclass
from typing import TypeVar, Generic
class X:
pass
class Y(X):
pass
T = TypeVar("T", X, Y)
@dataclass
class C(Generic[T]):
v: T
def f(c: C[T]):
# no error with mypy and ty
# pyright reports an error on the argument
c.v = Y()
# no error with mypy and ty
# pyright reports an error on the assignment
d: C[T] = C(Y())
class M(Generic[T]):
# no error with ty
# mypy and pyright report an error on the assignment
c: C[T] = C(Y())The constraints on T effectively make Y a lower bound for T, meaning that C(Y()) should be considered a valid C[T] for any allowed T. This seems to be correctly understood by mypy for field assignment and constructor assignment within a function, but not for field initialization at the class level.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong