97
97
DeferredNodeType : _TypeAlias = Union [FuncDef , LambdaExpr , OverloadedFuncDef , Decorator ]
98
98
FineGrainedDeferredNodeType : _TypeAlias = Union [FuncDef , MypyFile , OverloadedFuncDef ]
99
99
100
+
100
101
# A node which is postponed to be processed during the next pass.
101
102
# In normal mode one can defer functions and methods (also decorated and/or overloaded)
102
103
# and lambda expressions. Nested functions can't be deferred -- only top-level functions
103
104
# 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
+
111
110
112
111
# Same as above, but for fine-grained mode targets. Only top-level functions/methods
113
112
# 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
+
120
117
121
118
# Data structure returned by find_isinstance_check representing
122
119
# information learned from the truth or falsehood of a condition. The
131
128
# (such as two references to the same variable). TODO: it would
132
129
# probably be better to have the dict keyed by the nodes' literal_hash
133
130
# field instead.
134
-
135
131
TypeMap : _TypeAlias = Optional [Dict [Expression , Type ]]
136
132
133
+
137
134
# An object that represents either a precise type or a type with an upper bound;
138
135
# 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
+
145
140
146
141
# Keeps track of partial types in a single scope. In fine-grained incremental
147
142
# mode partial types initially defined at the top level cannot be completed in
148
143
# 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
153
148
154
149
155
150
class TypeChecker (NodeVisitor [None ], CheckerPluginInterface ):
@@ -891,7 +886,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
891
886
self .msg .unimported_type_becomes_any ("Return type" , ret_type , fdef )
892
887
for idx , arg_type in enumerate (fdef .type .arg_types ):
893
888
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 } \" "
895
890
self .msg .unimported_type_becomes_any (prefix , arg_type , fdef )
896
891
check_for_explicit_any (fdef .type , self .options , self .is_typeshed_stub ,
897
892
self .msg , context = fdef )
@@ -1062,9 +1057,9 @@ def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
1062
1057
name = arg .variable .name
1063
1058
msg = 'Incompatible default for '
1064
1059
if name .startswith ('__tuple_arg_' ):
1065
- msg += "tuple argument {}" . format ( name [12 :])
1060
+ msg += f "tuple argument { name [12 :]} "
1066
1061
else :
1067
- msg += 'argument "{}"' . format ( name )
1062
+ msg += f 'argument "{ name } "'
1068
1063
self .check_simple_assignment (
1069
1064
arg .variable .type ,
1070
1065
arg .initializer ,
@@ -1964,7 +1959,7 @@ def check_enum_bases(self, defn: ClassDef) -> None:
1964
1959
continue
1965
1960
elif enum_base is not None :
1966
1961
self .fail (
1967
- 'No base classes are allowed after "{}"' . format ( enum_base ) ,
1962
+ f 'No base classes are allowed after "{ enum_base } "' ,
1968
1963
defn ,
1969
1964
)
1970
1965
break
@@ -3308,8 +3303,8 @@ def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expressio
3308
3303
self .msg .deleted_as_lvalue (lvalue_type , context )
3309
3304
elif lvalue_type :
3310
3305
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 )
3313
3308
return rvalue_type
3314
3309
3315
3310
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
3717
3712
expected_type = TypeType (exc_type )
3718
3713
self .check_subtype (
3719
3714
typ .items [0 ], expected_type , s ,
3720
- 'Argument 1 must be "{}" subtype' . format ( expected_type ) ,
3715
+ f 'Argument 1 must be "{ expected_type } " subtype' ,
3721
3716
)
3722
3717
3723
3718
# Typecheck `traceback` part:
@@ -3732,7 +3727,7 @@ def _type_check_raise_python2(self, e: Expression, s: RaiseStmt, typ: ProperType
3732
3727
])
3733
3728
self .check_subtype (
3734
3729
typ .items [2 ], traceback_type , s ,
3735
- 'Argument 3 must be "{}" subtype' . format ( traceback_type ) ,
3730
+ f 'Argument 3 must be "{ traceback_type } " subtype' ,
3736
3731
)
3737
3732
else :
3738
3733
expected_type_items = [
@@ -4302,7 +4297,7 @@ def _make_fake_typeinfo_and_full_name(
4302
4297
curr_module_ : MypyFile ,
4303
4298
) -> Tuple [TypeInfo , str ]:
4304
4299
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 } >'
4306
4301
full_name_ = gen_unique_name (short_name , curr_module_ .names )
4307
4302
cdef , info_ = self .make_fake_typeinfo (
4308
4303
curr_module_ .fullname ,
@@ -4354,7 +4349,7 @@ def intersect_instance_callable(self, typ: Instance, callable_type: CallableType
4354
4349
# have a valid fullname and a corresponding entry in a symbol table. We generate
4355
4350
# a unique name inside the symbol table of the current module.
4356
4351
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 } >" ,
4358
4353
cur_module .names )
4359
4354
4360
4355
# Synthesize a fake TypeInfo
@@ -5367,7 +5362,7 @@ def lookup(self, name: str) -> SymbolTableNode:
5367
5362
table = cast (MypyFile , b .node ).names
5368
5363
if name in table :
5369
5364
return table [name ]
5370
- raise KeyError ('Failed lookup: {}' . format ( name ) )
5365
+ raise KeyError (f 'Failed lookup: { name } ' )
5371
5366
5372
5367
def lookup_qualified (self , name : str ) -> SymbolTableNode :
5373
5368
if '.' not in name :
@@ -5891,7 +5886,7 @@ def and_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
5891
5886
# arbitrarily give precedence to m2. (In the future, we could use
5892
5887
# an intersection type.)
5893
5888
result = m2 .copy ()
5894
- m2_keys = set ( literal_hash (n2 ) for n2 in m2 )
5889
+ m2_keys = { literal_hash (n2 ) for n2 in m2 }
5895
5890
for n1 in m1 :
5896
5891
if literal_hash (n1 ) not in m2_keys :
5897
5892
result [n1 ] = m1 [n1 ]
@@ -6561,7 +6556,7 @@ def is_static(func: Union[FuncBase, Decorator]) -> bool:
6561
6556
return is_static (func .func )
6562
6557
elif isinstance (func , FuncBase ):
6563
6558
return func .is_static
6564
- assert False , "Unexpected func type: {}" . format ( type (func ))
6559
+ assert False , f "Unexpected func type: { type (func )} "
6565
6560
6566
6561
6567
6562
def is_subtype_no_promote (left : Type , right : Type ) -> bool :
0 commit comments