Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
[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
Open
randolf-scholz wants to merge3 commits intopython:masterChoose a base branch fromrandolf-scholz:fix_variance_polymorphic_constructor
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
[PEP 695] Fix incorrect Variance Computation with Polymorphic Methods.#19466
randolf-scholz wants to merge3 commits intopython:masterfromrandolf-scholz:fix_variance_polymorphic_constructor
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Uh oh!
There was an error while loading.Please reload this page.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Stanislav Terliakov <50529348+sterliakov@users.noreply.github.com>
According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
sterliakov approved these changesJul 16, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading.Please reload this page.
testPEP695InferVariancePolymorphicMethod
My idea for fixing it was to replace
typ = find_member(member, self_type, self_type)
withtyp = find_member(member, self_type, plain_self)
inside the functioninfer_variance
, whereplain_self
is the type of self without any type variables.To be frank, I do not myself 100% understand why it works / if it is safe, but below is my best effort explanation.
Maybe a better solution is to substitute all function variables with
UninhabitedType()
?But I am not sure how to do this directly, since the type is only obtained within
find_member
.According to the docstring of
find_member_simple
:Since
plain_self
is always a supertype of the self type, however it may be parametrized, thetyp
we get this way should be compatible with thetyp
we get using the concreteself_type
. However, by binding self only toplain_self
, it replaces substituted polymorphic variables withNever
.Examples:
With this patch:
Foo.new
becomesdef [S] (self: tmp_d.Foo[Never], arg: builtins.list[Never]) -> tmp_d.Foo[Never]
intypeops.py#L470def (arg: builtins.list[Never]) -> tmp_d.Foo[Never]
insubtypes.py#L2211Bar.new
becomesdef (arg: builtins.list[T`1]) -> tmp_d.Bar[T`1]
(✅)Without this patch:
Foo.new
becomesdef [S] (self: tmp_d.Foo[T`1], arg: builtins.list[T`1]) -> tmp_d.Foo[T`1]
intypeops.py#L470 (❌)def (arg: builtins.list[T`1]) -> tmp_d.Foo[T`1]
insubtypes.py#L2211 (❌)Bar.new
becomesdef (arg: builtins.list[T`1]) -> tmp_d.Bar[T`1]
(✅)Another way to think about it is we can generally assume a signature of the form:
Now, given
self_type
isClass[T]
, it first solvesClass[T] = Class[TypeForm[S, T]]
forS
insidebind_self
, giving us some solutionS(T)
, and then substitutes it giving us some non-polymorphic methoddef method(self: Class[T], arg: TypeForm[T]) -> TypeForm[T]
and then drops the first argument, so we get the bound method
method(arg: TypeForm[T]) -> TypeForm[T]
.By providing the
plain_self
, the solution we get isS = Never
, which solve the problem.