Skip to content

Commit dd867a4

Browse files
committed
Refactor to pass fail func to bind_new instead
1 parent 6e08879 commit dd867a4

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

mypy/semanal.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,13 @@ def __init__(
451451
self.type_stack = []
452452
# Are the namespaces of classes being processed complete?
453453
self.incomplete_type_stack: list[bool] = []
454+
self.tvar_scope = TypeVarLikeScope()
454455
self.function_stack = []
455456
self.block_depth = [0]
456457
self.loop_depth = [0]
457458
self.errors = errors
458459
self.modules = modules
459460
self.msg = MessageBuilder(errors, modules)
460-
self.tvar_scope = TypeVarLikeScope(msg=self.msg)
461461
self.missing_modules = missing_modules
462462
self.missing_names = [set()]
463463
# These namespaces are still in process of being populated. If we encounter a
@@ -859,7 +859,7 @@ def file_context(
859859
self._is_stub_file = file_node.path.lower().endswith(".pyi")
860860
self._is_typeshed_stub_file = file_node.is_typeshed_file(options)
861861
self.globals = file_node.names
862-
self.tvar_scope = TypeVarLikeScope(msg=self.msg)
862+
self.tvar_scope = TypeVarLikeScope()
863863

864864
self.named_tuple_analyzer = NamedTupleAnalyzer(options, self, self.msg)
865865
self.typed_dict_analyzer = TypedDictAnalyzer(options, self, self.msg)
@@ -2404,7 +2404,7 @@ def tvar_defs_from_tvars(
24042404
self.fail(
24052405
message_registry.TYPE_VAR_REDECLARED_IN_NESTED_CLASS.format(name), context
24062406
)
2407-
tvar_def = self.tvar_scope.bind_new(name, tvar_expr, context)
2407+
tvar_def = self.tvar_scope.bind_new(name, tvar_expr, self.fail, context)
24082408
if last_tvar_name_with_default is not None and not tvar_def.has_default():
24092409
self.msg.tvar_without_default_type(
24102410
tvar_def.name, last_tvar_name_with_default, context
@@ -2433,7 +2433,7 @@ def get_and_bind_all_tvars(self, type_exprs: list[Expression]) -> list[TypeVarLi
24332433
tvars.setdefault(name, (expr, base_expr))
24342434
tvar_defs = []
24352435
for name, (tvar_expr, context) in tvars.items():
2436-
tvar_def = self.tvar_scope.bind_new(name, tvar_expr, context)
2436+
tvar_def = self.tvar_scope.bind_new(name, tvar_expr, self.fail, context)
24372437
tvar_defs.append(tvar_def)
24382438
return tvar_defs
24392439

@@ -7441,7 +7441,7 @@ def analyze_type_expr(self, expr: Expression) -> None:
74417441
# them semantically analyzed, however, if they need to treat it as an expression
74427442
# and not a type. (Which is to say, mypyc needs to do this.) Do the analysis
74437443
# in a fresh tvar scope in order to suppress any errors about using type variables.
7444-
with self.tvar_scope_frame(TypeVarLikeScope(msg=self.msg)), self.allow_unbound_tvars_set():
7444+
with self.tvar_scope_frame(TypeVarLikeScope()), self.allow_unbound_tvars_set():
74457445
expr.accept(self)
74467446

74477447
def type_analyzer(

mypy/tvar_scope.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3-
from mypy.messages import MessageBuilder
3+
from collections.abc import Callable
4+
from typing_extensions import TypeAlias as _TypeAlias
5+
46
from mypy.nodes import (
57
Context,
68
ParamSpecExpr,
@@ -23,14 +25,21 @@
2325
TypeVarType,
2426
)
2527

28+
FailFunc: _TypeAlias = Callable[[str, Context], None]
29+
2630

2731
class TypeVarLikeDefaultFixer(TrivialSyntheticTypeTranslator):
2832
"""Set namespace for all TypeVarLikeTypes types."""
2933

3034
def __init__(
31-
self, scope: TypeVarLikeScope, source_tv: TypeVarLikeExpr, context: Context
35+
self,
36+
scope: TypeVarLikeScope,
37+
fail_func: FailFunc,
38+
source_tv: TypeVarLikeExpr,
39+
context: Context,
3240
) -> None:
3341
self.scope = scope
42+
self.fail_func = fail_func
3443
self.source_tv = source_tv
3544
self.context = context
3645
super().__init__()
@@ -60,7 +69,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
6069
return t
6170

6271
def _report_unbound_tvar(self, tvar: TypeVarLikeType) -> None:
63-
self.scope.msg.fail(
72+
self.fail_func(
6473
f"Type variable {tvar.name} referenced in the default"
6574
f" of {self.source_tv.name} is unbound",
6675
self.context,
@@ -79,8 +88,6 @@ def __init__(
7988
is_class_scope: bool = False,
8089
prohibited: TypeVarLikeScope | None = None,
8190
namespace: str = "",
82-
*,
83-
msg: MessageBuilder,
8491
) -> None:
8592
"""Initializer for TypeVarLikeScope
8693
@@ -97,7 +104,6 @@ def __init__(
97104
self.is_class_scope = is_class_scope
98105
self.prohibited = prohibited
99106
self.namespace = namespace
100-
self.msg = msg
101107
if parent is not None:
102108
self.func_id = parent.func_id
103109
self.class_id = parent.class_id
@@ -120,20 +126,20 @@ def allow_binding(self, fullname: str) -> bool:
120126

121127
def method_frame(self, namespace: str) -> TypeVarLikeScope:
122128
"""A new scope frame for binding a method"""
123-
return TypeVarLikeScope(self, False, None, namespace=namespace, msg=self.msg)
129+
return TypeVarLikeScope(self, False, None, namespace=namespace)
124130

125131
def class_frame(self, namespace: str) -> TypeVarLikeScope:
126132
"""A new scope frame for binding a class. Prohibits *this* class's tvars"""
127-
return TypeVarLikeScope(
128-
self.get_function_scope(), True, self, namespace=namespace, msg=self.msg
129-
)
133+
return TypeVarLikeScope(self.get_function_scope(), True, self, namespace=namespace)
130134

131135
def new_unique_func_id(self) -> TypeVarId:
132136
"""Used by plugin-like code that needs to make synthetic generic functions."""
133137
self.func_id -= 1
134138
return TypeVarId(self.func_id)
135139

136-
def bind_new(self, name: str, tvar_expr: TypeVarLikeExpr, context: Context) -> TypeVarLikeType:
140+
def bind_new(
141+
self, name: str, tvar_expr: TypeVarLikeExpr, fail_func: FailFunc, context: Context
142+
) -> TypeVarLikeType:
137143
if self.is_class_scope:
138144
self.class_id += 1
139145
i = self.class_id
@@ -146,7 +152,9 @@ def bind_new(self, name: str, tvar_expr: TypeVarLikeExpr, context: Context) -> T
146152
# referenced variable is already in scope (textually precedes the definition we're
147153
# processing now).
148154
default = tvar_expr.default.accept(
149-
TypeVarLikeDefaultFixer(self, tvar_expr, context=context)
155+
TypeVarLikeDefaultFixer(
156+
self, fail_func=fail_func, source_tv=tvar_expr, context=context
157+
)
150158
)
151159

152160
if isinstance(tvar_expr, TypeVarExpr):

mypy/typeanal.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,9 @@ def analyze_callable_type(self, t: UnboundType) -> Type:
15611561
# below happens at very early stage.
15621562
variables = []
15631563
for name, tvar_expr in self.find_type_var_likes(callable_args):
1564-
variables.append(self.tvar_scope.bind_new(name, tvar_expr, t))
1564+
variables.append(
1565+
self.tvar_scope.bind_new(name, tvar_expr, self.fail_func, t)
1566+
)
15651567
maybe_ret = self.analyze_callable_args_for_paramspec(
15661568
callable_args, ret_type, fallback
15671569
) or self.analyze_callable_args_for_concatenate(
@@ -1833,7 +1835,7 @@ def bind_function_type_variables(
18331835
assert var_node, "Binding for function type variable not found within function"
18341836
var_expr = var_node.node
18351837
assert isinstance(var_expr, TypeVarLikeExpr)
1836-
binding = self.tvar_scope.bind_new(var.name, var_expr, fun_type)
1838+
binding = self.tvar_scope.bind_new(var.name, var_expr, self.fail_func, fun_type)
18371839
defs.append(binding)
18381840
return tuple(defs), has_self_type
18391841
typevars, has_self_type = self.infer_type_variables(fun_type)
@@ -1846,7 +1848,7 @@ def bind_function_type_variables(
18461848
if not self.tvar_scope.allow_binding(tvar.fullname):
18471849
err_msg = message_registry.TYPE_VAR_REDECLARED_IN_NESTED_CLASS.format(name)
18481850
self.fail(err_msg.value, defn, code=err_msg.code)
1849-
binding = self.tvar_scope.bind_new(name, tvar, fun_type)
1851+
binding = self.tvar_scope.bind_new(name, tvar, self.fail_func, fun_type)
18501852
defs.append(binding)
18511853

18521854
return tuple(defs), has_self_type

0 commit comments

Comments
 (0)