Skip to content

Commit 4847ec8

Browse files
Fix forward references in type parameters of overparameterized PEP 695 type aliases (#19725)
Fixes #19734 Make semanal defer for placeholders in the upper bounds / constraints, even when the type variable doesn't appear in the target. https://discuss.python.org/t/mypy-raises-exception-for-certain-type-alias-definitions-with-parameters-bounded-by-a-not-yet-defined-type/103305
1 parent c32b0a5 commit 4847ec8

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

mypy/semanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5598,7 +5598,8 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None:
55985598
else:
55995599
incomplete_target = has_placeholder(res)
56005600

5601-
if self.found_incomplete_ref(tag) or incomplete_target:
5601+
incomplete_tv = any(has_placeholder(tv) for tv in alias_tvars)
5602+
if self.found_incomplete_ref(tag) or incomplete_target or incomplete_tv:
56025603
# Since we have got here, we know this must be a type alias (incomplete refs
56035604
# may appear in nested positions), therefore use becomes_typeinfo=True.
56045605
self.mark_incomplete(s.name.name, s.value, becomes_typeinfo=True)

test-data/unit/check-python312.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,3 +2112,12 @@ if T(x) is str: # E: "TypeAliasType" not callable
21122112
reveal_type(x) # N: Revealed type is "builtins.object"
21132113
[builtins fixtures/tuple.pyi]
21142114
[typing fixtures/typing-full.pyi]
2115+
2116+
[case testPEP695TypeAliasForwardReferenceInUnusedTypeVar]
2117+
# https://discuss.python.org/t/103305
2118+
type Alias1[T: "A"] = int
2119+
type Alias2[T: ("A", int)] = int
2120+
class A: ...
2121+
2122+
x1: Alias1[A] # ok
2123+
x2: Alias2[A] # ok

0 commit comments

Comments
 (0)