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] Fix incorrect Variance Computation with Polymorphic Methods.#19466
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
base:master
Are you sure you want to change the base?
Changes fromall commits
d7b88cca0b037eab680d10f33c795d16a0825e8efa98701390b2aead307382e01e8651d41749bFile 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 |
|---|---|---|
| @@ -450,6 +450,36 @@ class Contra2[T]: | ||
| 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 testPEP695InferVariancePolymorphicMethod] | ||
| class Cov[T]: | ||
| def get(self) -> T: ... | ||
| def new[S](self: "Cov[S]", arg: list[S]) -> "Cov[S]": ... | ||
| cov_pos: Cov[object] = Cov[int]() | ||
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. This is not safe, if I continue this example like this I get an error at runtime that is not detected: classSub(Cov[int]):defnew(self,arg:list[int])->Sub:print(arg[0].to_bytes())returnselfcov_pos:Cov[object]=Sub()cov_pos.new([object()]) On a more general level, how exactly this: classCov[T]:defget(self)->T: ...defnew[S](self:Cov[S],arg:list[S])->Cov[S]: ... is different from this classCov[T]:defget(self)->T: ...defnew(self,arg:list[T])->Cov[T]: ... ? Maybe I am missing something but these two are literally identical in terms of semantics. Can you give an example ofuses of ContributorAuthor
| ||
| cov_neg: Cov[int] = Cov[object]() # E: Incompatible types in assignment (expression has type "Cov[object]", variable has type "Cov[int]") | ||
| class Contra[T]: | ||
| def set(self, arg: T) -> None: ... | ||
| def new[S](self: "Contra[S]", arg: list[S]) -> "Contra[S]": ... | ||
| contra_pos: Contra[object] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[object]") | ||
| contra_neg: Contra[int] = Contra[object]() | ||
| [case testPEP695SelfAttribute] | ||
| # https://github.com/python/mypy/issues/18334 | ||
| from typing import Self | ||
| class Foo[T]: | ||
| instance: Self | ||
| def foo(self) -> T: ... # type:ignore[empty-body] | ||
| class Bar(Foo[int]): ... | ||
| # OK | ||
| foo: Foo[object] = Bar() | ||
| [case testPEP695InheritInvariant] | ||
| class Invariant[T]: | ||
| x: T | ||