2
2
3
3
import re
4
4
5
- from typing import cast , List , Tuple , Dict , Callable , Union
5
+ from typing import cast , List , Tuple , Dict , Callable , Union , Optional
6
6
7
7
from mypy .types import (
8
8
Type , AnyType , TupleType , Instance , UnionType
18
18
from mypy .messages import MessageBuilder
19
19
20
20
FormatStringExpr = Union [StrExpr , BytesExpr , UnicodeExpr ]
21
+ Checkers = Tuple [Callable [[Expression ], None ], Callable [[Type ], None ]]
21
22
22
23
23
24
class ConversionSpecifier :
@@ -105,7 +106,7 @@ def parse_conversion_specifiers(self, format: str) -> List[ConversionSpecifier]:
105
106
return specifiers
106
107
107
108
def analyze_conversion_specifiers (self , specifiers : List [ConversionSpecifier ],
108
- context : Context ) -> bool :
109
+ context : Context ) -> Optional [ bool ] :
109
110
has_star = any (specifier .has_star () for specifier in specifiers )
110
111
has_key = any (specifier .has_key () for specifier in specifiers )
111
112
all_have_keys = all (
@@ -192,9 +193,8 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
192
193
193
194
def build_replacement_checkers (self , specifiers : List [ConversionSpecifier ],
194
195
context : Context , expr : FormatStringExpr
195
- ) -> List [Tuple [Callable [[Expression ], None ],
196
- Callable [[Type ], None ]]]:
197
- checkers = [] # type: List[Tuple[Callable[[Expression], None], Callable[[Type], None]]]
196
+ ) -> Optional [List [Checkers ]]:
197
+ checkers = [] # type: List[Checkers]
198
198
for specifier in specifiers :
199
199
checker = self .replacement_checkers (specifier , context , expr )
200
200
if checker is None :
@@ -203,13 +203,12 @@ def build_replacement_checkers(self, specifiers: List[ConversionSpecifier],
203
203
return checkers
204
204
205
205
def replacement_checkers (self , specifier : ConversionSpecifier , context : Context ,
206
- expr : FormatStringExpr ) -> List [Tuple [Callable [[Expression ], None ],
207
- Callable [[Type ], None ]]]:
206
+ expr : FormatStringExpr ) -> Optional [List [Checkers ]]:
208
207
"""Returns a list of tuples of two functions that check whether a replacement is
209
208
of the right type for the specifier. The first functions take a node and checks
210
209
its type in the right type context. The second function just checks a type.
211
210
"""
212
- checkers = [] # type: List[Tuple[Callable[[Expression], None], Callable[[Type], None]] ]
211
+ checkers = [] # type: List[Checkers ]
213
212
214
213
if specifier .width == '*' :
215
214
checkers .append (self .checkers_for_star (context ))
@@ -227,14 +226,13 @@ def replacement_checkers(self, specifier: ConversionSpecifier, context: Context,
227
226
checkers .append (c )
228
227
return checkers
229
228
230
- def checkers_for_star (self , context : Context ) -> Tuple [Callable [[Expression ], None ],
231
- Callable [[Type ], None ]]:
229
+ def checkers_for_star (self , context : Context ) -> Checkers :
232
230
"""Returns a tuple of check functions that check whether, respectively,
233
231
a node or a type is compatible with a star in a conversion specifier
234
232
"""
235
233
expected = self .named_type ('builtins.int' )
236
234
237
- def check_type (type : Type = None ) -> None :
235
+ def check_type (type : Type ) -> None :
238
236
expected = self .named_type ('builtins.int' )
239
237
self .chk .check_subtype (type , expected , context , '* wants int' )
240
238
@@ -246,16 +244,16 @@ def check_expr(expr: Expression) -> None:
246
244
247
245
def checkers_for_regular_type (self , type : str ,
248
246
context : Context ,
249
- expr : FormatStringExpr ) -> Tuple [Callable [[Expression ], None ],
250
- Callable [[Type ], None ]]:
247
+ expr : FormatStringExpr ) -> Optional [Checkers ]:
251
248
"""Returns a tuple of check functions that check whether, respectively,
252
249
a node or a type is compatible with 'type'. Return None in case of an
253
250
"""
254
251
expected_type = self .conversion_type (type , context , expr )
255
252
if expected_type is None :
256
253
return None
257
254
258
- def check_type (type : Type = None ) -> None :
255
+ def check_type (type : Type ) -> None :
256
+ assert expected_type is not None
259
257
self .chk .check_subtype (type , expected_type , context ,
260
258
messages .INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION ,
261
259
'expression has type' , 'placeholder has type' )
@@ -268,16 +266,16 @@ def check_expr(expr: Expression) -> None:
268
266
269
267
def checkers_for_c_type (self , type : str ,
270
268
context : Context ,
271
- expr : FormatStringExpr ) -> Tuple [Callable [[Expression ], None ],
272
- Callable [[Type ], None ]]:
269
+ expr : FormatStringExpr ) -> Optional [Checkers ]:
273
270
"""Returns a tuple of check functions that check whether, respectively,
274
271
a node or a type is compatible with 'type' that is a character type
275
272
"""
276
273
expected_type = self .conversion_type (type , context , expr )
277
274
if expected_type is None :
278
275
return None
279
276
280
- def check_type (type : Type = None ) -> None :
277
+ def check_type (type : Type ) -> None :
278
+ assert expected_type is not None
281
279
self .chk .check_subtype (type , expected_type , context ,
282
280
messages .INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION ,
283
281
'expression has type' , 'placeholder has type' )
@@ -291,7 +289,7 @@ def check_expr(expr: Expression) -> None:
291
289
292
290
return check_expr , check_type
293
291
294
- def conversion_type (self , p : str , context : Context , expr : FormatStringExpr ) -> Type :
292
+ def conversion_type (self , p : str , context : Context , expr : FormatStringExpr ) -> Optional [ Type ] :
295
293
"""Return the type that is accepted for a string interpolation
296
294
conversion specifier type.
297
295
0 commit comments