Fixed bug that resulted in the incorrect variance inference of an "auto variance" type parameter (as introduced in PEP 695).

This commit is contained in:
Eric Traut 2023-07-18 19:51:12 -07:00
parent 5569939ee6
commit ed295ddcaf
2 changed files with 13 additions and 0 deletions

View File

@ -2520,6 +2520,12 @@ export namespace TypeVarType {
// By this point, the variance should have been inferred.
assert(variance !== Variance.Auto, 'Expected variance to be inferred');
// If we're in the process of computing variance, it will still be
// unknown. Default to covariant in this case.
if (variance === Variance.Unknown) {
return Variance.Covariant;
}
return variance;
}

View File

@ -24,6 +24,13 @@ vco2_1: ShouldBeCovariant2[float] = ShouldBeCovariant2[int]()
vco2_2: ShouldBeCovariant2[int] = ShouldBeCovariant2[float]()
class ShouldBeCovariant3[T]:
def method1(self) -> "ShouldBeCovariant2[T]":
...
vco3_1: ShouldBeCovariant3[float] = ShouldBeCovariant3[int]()
class ShouldBeInvariant1[T]:
def __init__(self, value: T) -> None: