Skip to content

Fix missing error when redeclaring type variable in nested generic class #18883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
"TypeVar constraint type cannot be parametrized by type variables", codes.MISC
)

TYPE_VAR_REDECLARED_IN_NESTED_CLASS: Final = ErrorMessage(
'Type variable "{}" is bound by an outer class', codes.VALID_TYPE
)

TYPE_ALIAS_WITH_YIELD_EXPRESSION: Final = ErrorMessage(
"Yield expression cannot be used within a type alias", codes.SYNTAX
)
Expand Down
8 changes: 8 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,14 @@ def tvar_defs_from_tvars(
tvar_expr.default = tvar_expr.default.accept(
TypeVarDefaultTranslator(self, tvar_expr.name, context)
)
# PEP-695 type variables that are redeclared in an inner scope are warned
# about elsewhere.
if not tvar_expr.is_new_style and not self.tvar_scope.allow_binding(
tvar_expr.fullname
):
self.fail(
message_registry.TYPE_VAR_REDECLARED_IN_NESTED_CLASS.format(name), context
)
tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
if last_tvar_name_with_default is not None and not tvar_def.has_default():
self.msg.tvar_without_default_type(
Expand Down
7 changes: 2 additions & 5 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,11 +1869,8 @@ def bind_function_type_variables(
defs = []
for name, tvar in typevars:
if not self.tvar_scope.allow_binding(tvar.fullname):
self.fail(
f'Type variable "{name}" is bound by an outer class',
defn,
code=codes.VALID_TYPE,
)
err_msg = message_registry.TYPE_VAR_REDECLARED_IN_NESTED_CLASS.format(name)
self.fail(err_msg.value, defn, code=err_msg.code)
binding = self.tvar_scope.bind_new(name, tvar)
defs.append(binding)

Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,12 @@ class A(Generic[T]):
# E: Free type variable expected in Generic[...]
[out]

[case testRedeclaredTypeVarWithinNestedGenericClass]
from typing import Generic, Iterable, TypeVar
T = TypeVar('T')
class A(Generic[T]):
class B(Iterable[T]): pass # E: Type variable "T" is bound by an outer class

[case testIncludingGenericTwiceInBaseClassList]
from typing import Generic, TypeVar
T = TypeVar('T')
Expand Down