Skip to content

Commit 27eb224

Browse files
committed
Type parameter should be introduced irrespective of outer scope missing names
1 parent b38413d commit 27eb224

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

mypy/semanal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6830,6 +6830,7 @@ def add_symbol_table_node(
68306830
else:
68316831
# see note in docstring describing None contexts
68326832
self.defer()
6833+
68336834
if (
68346835
existing is not None
68356836
and context is not None
@@ -6849,7 +6850,9 @@ def add_symbol_table_node(
68496850
self.add_redefinition(names, name, symbol)
68506851
if not (isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new)):
68516852
self.name_already_defined(name, context, existing)
6852-
elif name not in self.missing_names[-1] and "*" not in self.missing_names[-1]:
6853+
elif type_param or (
6854+
name not in self.missing_names[-1] and "*" not in self.missing_names[-1]
6855+
):
68536856
names[name] = symbol
68546857
if not no_progress:
68556858
self.progress = True

test-data/unit/check-python312.test

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,43 @@ class C[T]:
12281228
def f[S, S](x: S) -> S: # E: "S" already defined as a type parameter
12291229
return x
12301230

1231+
[case testPEP695TypeVarNameClashNoCrashForwardReference]
1232+
# https://github.com/python/mypy/issues/18507
1233+
from typing import TypeVar
1234+
T = TypeVar("T", bound=Foo) # E: Name "Foo" is used before definition
1235+
1236+
class Foo: ...
1237+
class Bar[T]: ...
1238+
1239+
[case testPEP695TypeVarNameClashNoCrashDeferredSymbol]
1240+
# https://github.com/python/mypy/issues/19526
1241+
T = Unknown # E: Name "Unknown" is not defined
1242+
1243+
class Foo[T]: ...
1244+
class Bar[*T]: ...
1245+
class Baz[**T]: ...
1246+
[builtins fixtures/tuple.pyi]
1247+
1248+
[case testPEP695TypeVarNameClashTypeAlias]
1249+
type Tb = object
1250+
type Ta[Tb] = 'B[Tb]'
1251+
class A[Ta]: ...
1252+
class B[Tb](A[Ta]): ...
1253+
1254+
[case testPEP695TypeVarNameClashStarImport]
1255+
# Similar to
1256+
# https://github.com/python/mypy/issues/19946
1257+
import a
1258+
1259+
[file a.py]
1260+
from b import *
1261+
class Foo[T]: ...
1262+
1263+
[file b.py]
1264+
from a import *
1265+
class Bar[T]: ...
1266+
[builtins fixtures/tuple.pyi]
1267+
12311268
[case testPEP695ClassDecorator]
12321269
from typing import Any
12331270

0 commit comments

Comments
 (0)