[PEP 695] Fix incorrect Variance Computation with Polymorphic Methods. #19466
+17
−1
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.
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
.New tests
testPEP695InferVariancePolymorphicMethod
(https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=9ad67340082664afdd8bf725122c98da)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]
in typeops.py#L470def (arg: builtins.list[Never]) -> tmp_d.Foo[Never]
in subtypes.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]
in typeops.py#L470 (❌)def (arg: builtins.list[T`1]) -> tmp_d.Foo[T`1]
in subtypes.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.