Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
[PEP 695] Allow Self return types with contravariance#17786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -213,7 +213,7 @@ b: Invariant[int] | ||
| if int(): | ||
| a = b # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[object]") | ||
| if int(): | ||
| b = a | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think this is not right. It happens because with current implementation we check return type after I think it makes sense to limit the return type erasure only to situations where the original callable type contained an explicit Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Btw when looking at this I found a bug in variance inference that affects protocols as well. In particular, all three examples above should be inferred as covariant (or even bivariant, but we don't have that). Also to clarify, the unsafety I meant above relates to examples like classC[T]:defmeth(self,x:T,y:C[T])->C[T]: ... that shouldnot be contravariant, while it seems to me with the proposed PR it will be. But after playing with this more, inference doesn't behave as it should on master either. This needs some cleanup.@JukkaL we can discuss this in our meeting on Tuesday. | ||
| c: Covariant[object] | ||
| d: Covariant[int] | ||
| @@ -393,6 +393,62 @@ inv3_1: Invariant3[float] = Invariant3[int](1) # E: Incompatible types in assig | ||
| inv3_2: Invariant3[int] = Invariant3[float](1) # E: Incompatible types in assignment (expression has type "Invariant3[float]", variable has type "Invariant3[int]") | ||
| [builtins fixtures/property.pyi] | ||
| [case testPEP695InferVarianceWithInheritedSelf] | ||
| from typing import overload, Self, TypeVar, Generic | ||
| T = TypeVar("T") | ||
| S = TypeVar("S") | ||
| class C(Generic[T]): | ||
| def f(self, x: T) -> Self: ... | ||
| def g(self) -> T: ... | ||
| class D[T1, T2](C[T1]): | ||
| def m(self, x: T2) -> None: ... | ||
| a1: D[int, int] = D[int, object]() | ||
| a2: D[int, object] = D[int, int]() # E: Incompatible types in assignment (expression has type "D[int, int]", variable has type "D[int, object]") | ||
| a3: D[int, int] = D[object, object]() # E: Incompatible types in assignment (expression has type "D[object, object]", variable has type "D[int, int]") | ||
| a4: D[object, int] = D[int, object]() # E: Incompatible types in assignment (expression has type "D[int, object]", variable has type "D[object, int]") | ||
| [case testPEP695InferVarianceWithReturnSelf] | ||
| from typing import Self, overload | ||
| class Cov[T]: | ||
| def f(self) -> Self: ... | ||
| a1: Cov[int] = Cov[float]() # E: Incompatible types in assignment (expression has type "Cov[float]", variable has type "Cov[int]") | ||
| a2: Cov[float] = Cov[int]() | ||
| class Contra[T]: | ||
| def f(self) -> Self: ... | ||
| def g(self, x: T) -> None: ... | ||
| b1: Contra[int] = Contra[float]() | ||
| b2: Contra[float] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[float]") | ||
| class Cov2[T]: | ||
| @overload | ||
| def f(self, x): ... | ||
| @overload | ||
| def f(self) -> Self: ... | ||
| def f(self, x=None): ... | ||
| c1: Cov2[int] = Cov2[float]() # E: Incompatible types in assignment (expression has type "Cov2[float]", variable has type "Cov2[int]") | ||
| c2: Cov2[float] = Cov2[int]() | ||
| class Contra2[T]: | ||
| @overload | ||
| def f(self, x): ... | ||
| @overload | ||
| def f(self) -> Self: ... | ||
| def f(self, x=None): ... | ||
| def g(self, x: T) -> None: ... | ||
| d1: Contra2[int] = Contra2[float]() | ||
| d2: Contra2[float] = Contra2[int]() # E: Incompatible types in assignment (expression has type "Contra2[int]", variable has type "Contra2[float]") | ||
| [case testPEP695InheritInvariant] | ||
| class Invariant[T]: | ||
| x: T | ||