Skip to content

Commit c467949

Browse files
committed
Revert "Simplify using has_no_typevars"
This reverts commit a945fe6.
1 parent dab921a commit c467949

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

mypy/semanal.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
type_vars_as_args,
296296
)
297297
from mypy.types_utils import is_invalid_recursive_alias, store_argument_type
298-
from mypy.typevars import fill_typevars, has_no_typevars
298+
from mypy.typevars import fill_typevars
299299
from mypy.util import correct_relative_import, is_dunder, module_prefix, unmangle, unnamed_function
300300
from mypy.visitor import NodeVisitor
301301

@@ -2369,8 +2369,11 @@ def analyze_unbound_tvar_impl(
23692369
assert isinstance(sym.node, TypeVarExpr)
23702370
return t.name, sym.node
23712371

2372-
def find_type_var_likes(self, t: Type) -> TypeVarLikeList:
2372+
def find_type_var_likes(
2373+
self, t: Type, *, include_bound_tvars: bool = False
2374+
) -> TypeVarLikeList:
23732375
visitor = FindTypeVarVisitor(self, self.tvar_scope)
2376+
visitor.include_bound_tvars = include_bound_tvars
23742377
t.accept(visitor)
23752378
return visitor.type_var_likes
23762379

@@ -5036,25 +5039,21 @@ def analyze_value_types(self, items: list[Expression]) -> list[Type]:
50365039
result: list[Type] = []
50375040
for node in items:
50385041
try:
5039-
analyzed = self.anal_type(
5040-
self.expr_to_unanalyzed_type(node), allow_placeholder=True
5041-
)
5042+
unanalyzed_type = self.expr_to_unanalyzed_type(node)
5043+
if self.find_type_var_likes(unanalyzed_type, include_bound_tvars=True):
5044+
self.fail(
5045+
"TypeVar constraint type cannot be parametrized by type variables", node
5046+
)
5047+
result.append(AnyType(TypeOfAny.from_error))
5048+
continue
5049+
5050+
analyzed = self.anal_type(unanalyzed_type, allow_placeholder=True)
50425051
if analyzed is None:
50435052
# Type variables are special: we need to place them in the symbol table
50445053
# soon, even if some value is not ready yet, see process_typevar_parameters()
50455054
# for an example.
50465055
analyzed = PlaceholderType(None, [], node.line)
5047-
5048-
# has_no_typevars does not work with PlaceholderType.
5049-
if not isinstance(
5050-
get_proper_type(analyzed), PlaceholderType
5051-
) and not has_no_typevars(analyzed):
5052-
self.fail(
5053-
"TypeVar constraint type cannot be parametrized by type variables", node
5054-
)
5055-
result.append(AnyType(TypeOfAny.from_error))
5056-
else:
5057-
result.append(analyzed)
5056+
result.append(analyzed)
50585057
except TypeTranslationError:
50595058
self.fail("Type expected", node)
50605059
result.append(AnyType(TypeOfAny.from_error))

mypy/typeanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,7 @@ def __init__(self, api: SemanticAnalyzerCoreInterface, scope: TypeVarLikeScope)
25582558
self.has_self_type = False
25592559
self.seen_aliases: set[TypeAliasType] | None = None
25602560
self.include_callables = True
2561+
self.include_bound_tvars = False
25612562

25622563
def _seems_like_callable(self, type: UnboundType) -> bool:
25632564
if not type.args:
@@ -2583,7 +2584,7 @@ def visit_unbound_type(self, t: UnboundType) -> None:
25832584
if (
25842585
node
25852586
and isinstance(node.node, TypeVarLikeExpr)
2586-
and self.scope.get_binding(node) is None
2587+
and (self.scope.get_binding(node) is None or self.include_bound_tvars)
25872588
):
25882589
if (name, node.node) not in self.type_var_likes:
25892590
self.type_var_likes.append((name, node.node))

test-data/unit/semanal-errors.test

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,13 @@ from typing import Generic, List, TypeVar
10651065

10661066
T = TypeVar("T")
10671067

1068+
Bad1 = TypeVar("Bad1", int, T) # E: TypeVar constraint type cannot be parametrized by type variables
1069+
Bad2 = TypeVar("Bad2", int, List[T]) # E: TypeVar constraint type cannot be parametrized by type variables
1070+
Bad3 = TypeVar("Bad3", int, List[List[T]]) # E: TypeVar constraint type cannot be parametrized by type variables
10681071
def f(x: T) -> None:
1069-
Bad = TypeVar("Bad", int, List[T]) # E: TypeVar constraint type cannot be parametrized by type variables
1072+
Bad4 = TypeVar("Bad4", int, List[T]) # E: TypeVar constraint type cannot be parametrized by type variables
10701073
class C(Generic[T]):
1071-
Bad = TypeVar("Bad", int, List[T]) # E: TypeVar constraint type cannot be parametrized by type variables
1074+
Bad5 = TypeVar("Bad5", int, List[T]) # E: TypeVar constraint type cannot be parametrized by type variables
10721075

10731076
[case testInvalidTypevarValues]
10741077
from typing import TypeVar

0 commit comments

Comments
 (0)