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
[PEP 695] Allow covariance with attribute that has "_" name prefix (#17782)
Fix this conformance test:
```
class ShouldBeCovariant5[T]:
def __init__(self, x: T) -> None:
self._x = x
@Property
def x(self) -> T:
return self._x
vo5_1: ShouldBeCovariant5[float] = ShouldBeCovariant5[int](1) # OK
vo5_2: ShouldBeCovariant5[int] = ShouldBeCovariant5[float](1) # E
```
My fix is to treat such attributes as not settable when inferring
variance.
Link:
https://github.com/python/typing/blob/main/conformance/tests/generics_variance_inference.py#L79
co1_2: Covariant1[int] = Covariant1[float](1) # E: Incompatible types in assignment (expression has type "Covariant1[float]", variable has type "Covariant1[int]")
358
+
359
+
class Covariant2[T]:
360
+
def __init__(self, x: T) -> None:
361
+
self.__foo_bar = x
362
+
363
+
@property
364
+
def x(self) -> T:
365
+
return self.__foo_bar
366
+
367
+
co2_1: Covariant2[float] = Covariant2[int](1)
368
+
co2_2: Covariant2[int] = Covariant2[float](1) # E: Incompatible types in assignment (expression has type "Covariant2[float]", variable has type "Covariant2[int]")
369
+
370
+
class Invariant1[T]:
371
+
def __init__(self, x: T) -> None:
372
+
self._x = x
373
+
374
+
# Methods behave differently from attributes
375
+
def _f(self, x: T) -> None: ...
376
+
377
+
@property
378
+
def x(self) -> T:
379
+
return self._x
380
+
381
+
inv1_1: Invariant1[float] = Invariant1[int](1) # E: Incompatible types in assignment (expression has type "Invariant1[int]", variable has type "Invariant1[float]")
382
+
inv1_2: Invariant1[int] = Invariant1[float](1) # E: Incompatible types in assignment (expression has type "Invariant1[float]", variable has type "Invariant1[int]")
383
+
384
+
class Invariant2[T]:
385
+
def __init__(self, x: T) -> None:
386
+
# Dunders are special
387
+
self.__x__ = x
388
+
389
+
@property
390
+
def x(self) -> T:
391
+
return self.__x__
392
+
393
+
inv2_1: Invariant2[float] = Invariant2[int](1) # E: Incompatible types in assignment (expression has type "Invariant2[int]", variable has type "Invariant2[float]")
394
+
inv2_2: Invariant2[int] = Invariant2[float](1) # E: Incompatible types in assignment (expression has type "Invariant2[float]", variable has type "Invariant2[int]")
395
+
396
+
class Invariant3[T]:
397
+
def __init__(self, x: T) -> None:
398
+
self._x = Invariant1(x)
399
+
400
+
@property
401
+
def x(self) -> T:
402
+
return self._x._x
403
+
404
+
inv3_1: Invariant3[float] = Invariant3[int](1) # E: Incompatible types in assignment (expression has type "Invariant3[int]", variable has type "Invariant3[float]")
405
+
inv3_2: Invariant3[int] = Invariant3[float](1) # E: Incompatible types in assignment (expression has type "Invariant3[float]", variable has type "Invariant3[int]")
0 commit comments