Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ def push_type_args(
return None
tvs.append((p.name, tv))

if self.is_defined_type_param(p.name):
if self.get_defined_type_param(p.name):
self.fail(f'"{p.name}" already defined as a type parameter', context)
else:
assert self.add_symbol(
Expand All @@ -1851,15 +1851,15 @@ def push_type_args(

return tvs

def is_defined_type_param(self, name: str) -> bool:
def get_defined_type_param(self, name: str) -> TypeVarLikeExpr | None:
for names in self.locals:
Copy link
Collaborator

@sterliakov sterliakov Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ough, I spelled that out but didn't realize that this goes in the wrong direction (?). We are typically interested in the most recent locals frame, so shouldn't this be

Suggested change
for names in self.locals:
for names in reversed(self.locals):

? It didn't matter when we only requested a boolean "is defined" result, but now could potentially resolve to a wrong symbol. I don't have a testcase for that, but anyway the variable will normally be found in the most recent local frame, so checking it first makes more sense? Upd: no, it cannot resolve to the wrong symbol, we only accept type variables, and type param redefinitions are never added to symtable, but maybe it's still better to avoid the unnecessary work? reversed is fast

if names is None:
continue
if name in names:
Comment on lines 1856 to 1858
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And just to save a bit on branching,

Suggested change
if names is None:
continue
if name in names:
if names is not None and name in names:

node = names[name].node
if isinstance(node, TypeVarLikeExpr):
return True
return False
return node
return None

def analyze_type_param(
self, type_param: TypeParam, context: Context
Expand Down Expand Up @@ -2272,15 +2272,15 @@ class Foo(Bar, Generic[T]): ...
has_type_var_tuple = False
if defn.type_args is not None:
for p in defn.type_args:
node = self.lookup(p.name, context)
node = self.get_defined_type_param(p.name)
assert node is not None
assert isinstance(node.node, TypeVarLikeExpr)
if isinstance(node.node, TypeVarTupleExpr):
assert isinstance(node, TypeVarLikeExpr)
if isinstance(node, TypeVarTupleExpr):
if has_type_var_tuple:
self.fail("Can only use one type var tuple in a class def", context)
continue
has_type_var_tuple = True
declared_tvars.append((p.name, node.node))
declared_tvars.append((p.name, node))

for i, base_expr in enumerate(base_type_exprs):
if isinstance(base_expr, StarExpr):
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -2237,3 +2237,10 @@ class D[*Ts](Generic[Unpack[Us]]): # E: Generic[...] base class is redundant \
# E: Can only use one type var tuple in a class def
pass
[builtins fixtures/tuple.pyi]

[case testPEP695TypeVarReusedName]
class C:
X = 5
class Inner[X]:
pass
[builtins fixtures/tuple.pyi]