ruff
e7a2d2c3 - [ty] Infer variance through type[T] (#27534)

Commit
13 days ago
[ty] Infer variance through type[T] (#27534) ## Summary Previously, variance inference treated `type[T]` as independent of `T`. As a result, generic classes with writable class-object attributes could incorrectly be treated as covariant: ```python class Mutable[T]: cls: type[T] def overwrite(value: Mutable[object]) -> None: value.cls = str def unsound(value: Mutable[int]) -> None: overwrite(value) # error: [invalid-argument-type] ``` We now retain the wrapped type variable when inferring variance through `type[T]`. Return positions remain covariant, parameter positions become contravariant, and writable public attributes correctly make their containing class invariant.
Author
Parents
Loading