Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,10 @@ def infer_variance(info: TypeInfo, i: int) -> bool:
typ2 = expand_type(typ, {tvar.id: object_type})
if not is_subtype(typ, typ2):
co = False
if settable:
# A writable attribute is also used in an input position,
# e.g. "x: Callable[[T], None]" must not be contravariant.
contra = False
if not is_subtype(typ2, typ):
contra = False
if settable:
Expand Down
91 changes: 91 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,97 @@ if int():
if int():
b3 = a3 # E: Incompatible types in assignment (expression has type "Invariant3[object]", variable has type "Invariant3[int]")

[case testPEP695InferVarianceWritableCallableAttribute]
from typing import Callable, Final

class Animal: ...
class Dog(Animal):
def bark(self) -> None: ...

class Handler[T]:
def __init__(self, cb: Callable[[T], None]) -> None:
self.cb = cb

def handle_dog(h: Handler[Dog]) -> None:
h.cb = lambda d: d.bark()

def handle_animal(h: Handler[Animal]) -> None: ...

animal_handler = Handler[Animal](lambda a: None)
handle_dog(animal_handler) # E: Argument 1 to "handle_dog" has incompatible type "Handler[Animal]"; expected "Handler[Dog]"
dog_handler = Handler[Dog](lambda d: None)
handle_animal(dog_handler) # E: Argument 1 to "handle_animal" has incompatible type "Handler[Dog]"; expected "Handler[Animal]"

class Invariant1[T]:
def __init__(self, cb: Callable[[T], None]) -> None:
self.cb: Callable[[T], None] = cb

a1: Invariant1[object]
b1: Invariant1[int]
if int():
a1 = b1 # E: Incompatible types in assignment (expression has type "Invariant1[int]", variable has type "Invariant1[object]")
if int():
b1 = a1 # E: Incompatible types in assignment (expression has type "Invariant1[object]", variable has type "Invariant1[int]")

class Invariant2[T]:
def __init__(self, f: Callable[[], T]) -> None:
self.f = f

a2: Invariant2[object]
b2: Invariant2[int]
if int():
a2 = b2 # E: Incompatible types in assignment (expression has type "Invariant2[int]", variable has type "Invariant2[object]")
if int():
b2 = a2 # E: Incompatible types in assignment (expression has type "Invariant2[object]", variable has type "Invariant2[int]")

class Contravariant1[T]:
def __init__(self, cb: Callable[[T], None]) -> None:
self.cb: Final = cb

c1: Contravariant1[object]
d1: Contravariant1[int]
if int():
c1 = d1 # E: Incompatible types in assignment (expression has type "Contravariant1[int]", variable has type "Contravariant1[object]")
if int():
d1 = c1

class Contravariant2[T]:
def __init__(self, cb: Callable[[T], None]) -> None:
self._cb = cb

c2: Contravariant2[object]
d2: Contravariant2[int]
if int():
c2 = d2 # E: Incompatible types in assignment (expression has type "Contravariant2[int]", variable has type "Contravariant2[object]")
if int():
d2 = c2

class Contravariant3[T]:
def __init__(self, cb: Callable[[T], None]) -> None:
self._cb = cb

@property
def cb(self) -> Callable[[T], None]:
return self._cb

c3: Contravariant3[object]
d3: Contravariant3[int]
if int():
c3 = d3 # E: Incompatible types in assignment (expression has type "Contravariant3[int]", variable has type "Contravariant3[object]")
if int():
d3 = c3

class Contravariant4[T]:
def f(self, x: T) -> None: ...

c4: Contravariant4[object]
d4: Contravariant4[int]
if int():
c4 = d4 # E: Incompatible types in assignment (expression has type "Contravariant4[int]", variable has type "Contravariant4[object]")
if int():
d4 = c4
[builtins fixtures/property.pyi]

[case testPEP695InferVarianceRecursive]
class Invariant[T]:
def f(self, x: Invariant[T]) -> Invariant[T]:
Expand Down
Loading