9797DeferredNodeType : _TypeAlias = Union [FuncDef , LambdaExpr , OverloadedFuncDef , Decorator ]
9898FineGrainedDeferredNodeType : _TypeAlias = Union [FuncDef , MypyFile , OverloadedFuncDef ]
9999
100+
100101# A node which is postponed to be processed during the next pass.
101102# In normal mode one can defer functions and methods (also decorated and/or overloaded)
102103# and lambda expressions. Nested functions can't be deferred -- only top-level functions
103104# and methods of classes not defined within a function can be deferred.
104- DeferredNode = NamedTuple (
105- 'DeferredNode' ,
106- [
107- ('node' , DeferredNodeType ),
108- ('active_typeinfo' , Optional [TypeInfo ]), # And its TypeInfo (for semantic analysis
109- # self type handling)
110- ])
105+ class DeferredNode (NamedTuple ):
106+ node : DeferredNodeType
107+ # And its TypeInfo (for semantic analysis self type handling
108+ active_typeinfo : Optional [TypeInfo ]
109+
111110
112111# Same as above, but for fine-grained mode targets. Only top-level functions/methods
113112# and module top levels are allowed as such.
114- FineGrainedDeferredNode = NamedTuple (
115- 'FineGrainedDeferredNode' ,
116- [
117- ('node' , FineGrainedDeferredNodeType ),
118- ('active_typeinfo' , Optional [TypeInfo ]),
119- ])
113+ class FineGrainedDeferredNode (NamedTuple ):
114+ node : FineGrainedDeferredNodeType
115+ active_typeinfo : Optional [TypeInfo ]
116+
120117
121118# Data structure returned by find_isinstance_check representing
122119# information learned from the truth or falsehood of a condition. The
131128# (such as two references to the same variable). TODO: it would
132129# probably be better to have the dict keyed by the nodes' literal_hash
133130# field instead.
134-
135131TypeMap : _TypeAlias = Optional [Dict [Expression , Type ]]
136132
133+
137134# An object that represents either a precise type or a type with an upper bound;
138135# it is important for correct type inference with isinstance.
139- TypeRange = NamedTuple (
140- 'TypeRange' ,
141- [
142- ('item' , Type ),
143- ('is_upper_bound' , bool ), # False => precise type
144- ])
136+ class TypeRange (NamedTuple ):
137+ item : Type
138+ is_upper_bound : bool # False => precise type
139+
145140
146141# Keeps track of partial types in a single scope. In fine-grained incremental
147142# mode partial types initially defined at the top level cannot be completed in
148143# a function, and we use the 'is_function' attribute to enforce this.
149- PartialTypeScope = NamedTuple ( ' PartialTypeScope' , [( 'map' , Dict [ Var , Context ]),
150- ( 'is_function' , bool ),
151- ( 'is_local' , bool ),
152- ])
144+ class PartialTypeScope ( NamedTuple ):
145+ map : Dict [ Var , Context ]
146+ is_function : bool
147+ is_local : bool
153148
154149
155150class TypeChecker (NodeVisitor [None ], CheckerPluginInterface ):
@@ -891,7 +886,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
891886 self .msg .unimported_type_becomes_any ("Return type" , ret_type , fdef )
892887 for idx , arg_type in enumerate (fdef .type .arg_types ):
893888 if has_any_from_unimported_type (arg_type ):
894- prefix = "Argument {} to \" {}\" " . format ( idx + 1 , fdef . name )
889+ prefix = f "Argument { idx + 1 } to \" { fdef . name } \" "
895890 self .msg .unimported_type_becomes_any (prefix , arg_type , fdef )
896891 check_for_explicit_any (fdef .type , self .options , self .is_typeshed_stub ,
897892 self .msg , context = fdef )
@@ -1062,9 +1057,9 @@ def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
10621057 name = arg .variable .name
10631058 msg = 'Incompatible default for '
10641059 if name .startswith ('__tuple_arg_' ):
1065- msg += "tuple argument {}" . format ( name [12 :])
1060+ msg += f "tuple argument { name [12 :]} "
10661061 else :
1067- msg += 'argument "{}"' . format ( name )
1062+ msg += f 'argument "{ name } "'
10681063 self .check_simple_assignment (
10691064 arg .variable .type ,
10701065 arg .initializer ,
@@ -1964,7 +1959,7 @@ def check_enum_bases(self, defn: ClassDef) -> None:
19641959 continue
19651960 elif enum_base is not None :
19661961 self .fail (
1967- 'No base classes are allowed after "{}"' . format ( enum_base ) ,
1962+ f 'No base classes are allowed after "{ enum_base } "' ,
19681963 defn ,
19691964 )
19701965 break
@@ -3308,8 +3303,8 @@ def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expressio
33083303 self .msg .deleted_as_lvalue (lvalue_type , context )
33093304 elif lvalue_type :
33103305 self .check_subtype (rvalue_type , lvalue_type , context , msg ,
3311- '{ } has type'. format ( rvalue_name ) ,
3312- '{ } has type'. format ( lvalue_name ) , code = code )
3306+ f' { rvalue_name } has type' ,
3307+ f' { lvalue_name } has type' , code = code )
33133308 return rvalue_type
33143309
33153310 def check_member_assignment (self , instance_type : Type , attribute_type : Type ,
@@ -3717,7 +3712,7 @@ def _type_check_raise_python2(self, e: Expression, s: RaiseStmt, typ: ProperType
37173712 expected_type = TypeType (exc_type )
37183713 self .check_subtype (
37193714 typ .items [0 ], expected_type , s ,
3720- 'Argument 1 must be "{}" subtype' . format ( expected_type ) ,
3715+ f 'Argument 1 must be "{ expected_type } " subtype' ,
37213716 )
37223717
37233718 # Typecheck `traceback` part:
@@ -3732,7 +3727,7 @@ def _type_check_raise_python2(self, e: Expression, s: RaiseStmt, typ: ProperType
37323727 ])
37333728 self .check_subtype (
37343729 typ .items [2 ], traceback_type , s ,
3735- 'Argument 3 must be "{}" subtype' . format ( traceback_type ) ,
3730+ f 'Argument 3 must be "{ traceback_type } " subtype' ,
37363731 )
37373732 else :
37383733 expected_type_items = [
@@ -4302,7 +4297,7 @@ def _make_fake_typeinfo_and_full_name(
43024297 curr_module_ : MypyFile ,
43034298 ) -> Tuple [TypeInfo , str ]:
43044299 names_list = pretty_seq ([x .type .name for x in base_classes_ ], "and" )
4305- short_name = '<subclass of {}>' . format ( names_list )
4300+ short_name = f '<subclass of { names_list } >'
43064301 full_name_ = gen_unique_name (short_name , curr_module_ .names )
43074302 cdef , info_ = self .make_fake_typeinfo (
43084303 curr_module_ .fullname ,
@@ -4354,7 +4349,7 @@ def intersect_instance_callable(self, typ: Instance, callable_type: CallableType
43544349 # have a valid fullname and a corresponding entry in a symbol table. We generate
43554350 # a unique name inside the symbol table of the current module.
43564351 cur_module = cast (MypyFile , self .scope .stack [0 ])
4357- gen_name = gen_unique_name ("<callable subtype of {}>" . format ( typ .type .name ) ,
4352+ gen_name = gen_unique_name (f "<callable subtype of { typ .type .name } >" ,
43584353 cur_module .names )
43594354
43604355 # Synthesize a fake TypeInfo
@@ -5367,7 +5362,7 @@ def lookup(self, name: str) -> SymbolTableNode:
53675362 table = cast (MypyFile , b .node ).names
53685363 if name in table :
53695364 return table [name ]
5370- raise KeyError ('Failed lookup: {}' . format ( name ) )
5365+ raise KeyError (f 'Failed lookup: { name } ' )
53715366
53725367 def lookup_qualified (self , name : str ) -> SymbolTableNode :
53735368 if '.' not in name :
@@ -5891,7 +5886,7 @@ def and_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
58915886 # arbitrarily give precedence to m2. (In the future, we could use
58925887 # an intersection type.)
58935888 result = m2 .copy ()
5894- m2_keys = set ( literal_hash (n2 ) for n2 in m2 )
5889+ m2_keys = { literal_hash (n2 ) for n2 in m2 }
58955890 for n1 in m1 :
58965891 if literal_hash (n1 ) not in m2_keys :
58975892 result [n1 ] = m1 [n1 ]
@@ -6561,7 +6556,7 @@ def is_static(func: Union[FuncBase, Decorator]) -> bool:
65616556 return is_static (func .func )
65626557 elif isinstance (func , FuncBase ):
65636558 return func .is_static
6564- assert False , "Unexpected func type: {}" . format ( type (func ))
6559+ assert False , f "Unexpected func type: { type (func )} "
65656560
65666561
65676562def is_subtype_no_promote (left : Type , right : Type ) -> bool :
0 commit comments