11from __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+
46from mypy .nodes import (
57 Context ,
68 ParamSpecExpr ,
2325 TypeVarType ,
2426)
2527
28+ FailFunc : _TypeAlias = Callable [[str , Context ], None ]
29+
2630
2731class 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 ):
0 commit comments