Skip to content

Commit 0ece662

Browse files
[PEP 696] Fix swapping TypeVars with defaults. (#19449)
- Fixes #19444. (added `testTypeVarDefaultsSwap`) - Fixes #19362 (added `testTypeVarDefaultsSwap2`) Changed the logic for recursion guards of `TypeVarType`: Instead of always substituting `repl = repl.accept(self)`, and situationally updating `repl.default = repl.default.accept(self)` if the result is a `TypeVarType`, we now always update `repl.default = repl.default.accept(self)` a priori and then only choose the expanded `repl.accept(self)` if the result is a concrete type. ## New Tests - `testTypeVarDefaultsSwap` (https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=d5a025a31ae3c8b9e2a36f4738aa1991) - `testTypeVarDefaultsSwap2` (https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=d3ed42c82f7144967c97d846c4c041ef) PS: closed earlier PRs #19447, since it contained debugging changes, and #19448 because it didn't solve #19362.
1 parent fb16e93 commit 0ece662

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

mypy/expandtype.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ def visit_type_var(self, t: TypeVarType) -> Type:
251251
if (tvar_id := repl.id) in self.recursive_tvar_guard:
252252
return self.recursive_tvar_guard[tvar_id] or repl
253253
self.recursive_tvar_guard[tvar_id] = None
254-
repl = repl.accept(self)
255-
if isinstance(repl, TypeVarType):
256-
repl.default = repl.default.accept(self)
254+
repl.default = repl.default.accept(self)
255+
expanded = repl.accept(self) # Note: `expanded is repl` may be true.
256+
repl = repl if isinstance(expanded, TypeVarType) else expanded
257257
self.recursive_tvar_guard[tvar_id] = repl
258258
return repl
259259

test-data/unit/check-typevar-defaults.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,38 @@ def func_c4(
416416
reveal_type(m) # N: Revealed type is "__main__.ClassC4[builtins.int, builtins.float]"
417417
[builtins fixtures/tuple.pyi]
418418

419+
[case testTypeVarDefaultsSwap]
420+
from typing import TypeVar, Generic
421+
422+
T = TypeVar("T")
423+
X = TypeVar("X", default=object)
424+
Y = TypeVar("Y", default=object)
425+
426+
427+
class Foo(Generic[T, Y]):
428+
def test(self) -> None:
429+
reveal_type( Foo[Y, T]() ) # N: Revealed type is "__main__.Foo[Y`2 = builtins.object, T`1]"
430+
431+
432+
class Bar(Generic[X, Y]):
433+
def test(self) -> None:
434+
reveal_type( Bar[Y, X]() ) # N: Revealed type is "__main__.Bar[Y`2 = builtins.object, X`1 = builtins.object]"
435+
436+
437+
[case testTypeVarDefaultsSwap2]
438+
from typing import TypeVar, Generic
439+
440+
X = TypeVar("X", default=object)
441+
Y = TypeVar("Y", default=object)
442+
U = TypeVar("U", default=object)
443+
V = TypeVar("V", default=object)
444+
445+
class Transform(Generic[X, Y]):
446+
def invert(self) -> "Transform[Y, X]": ...
447+
448+
class Foo(Transform[U, V], Generic[U, V]):
449+
def invert(self) -> "Foo[V, U]": ...
450+
419451
[case testTypeVarDefaultsClassRecursive1]
420452
# flags: --disallow-any-generics
421453
from typing import Generic, TypeVar, List

0 commit comments

Comments
 (0)