22
33import re
44
5- from typing import cast , List , Tuple , Dict , Callable , Union
5+ from typing import cast , List , Tuple , Dict , Callable , Union , Optional
66
77from mypy .types import (
88 Type , AnyType , TupleType , Instance , UnionType
1818from mypy .messages import MessageBuilder
1919
2020FormatStringExpr = Union [StrExpr , BytesExpr , UnicodeExpr ]
21+ Checkers = Tuple [Callable [[Expression ], None ], Callable [[Type ], None ]]
2122
2223
2324class ConversionSpecifier :
@@ -105,7 +106,7 @@ def parse_conversion_specifiers(self, format: str) -> List[ConversionSpecifier]:
105106 return specifiers
106107
107108 def analyze_conversion_specifiers (self , specifiers : List [ConversionSpecifier ],
108- context : Context ) -> bool :
109+ context : Context ) -> Optional [ bool ] :
109110 has_star = any (specifier .has_star () for specifier in specifiers )
110111 has_key = any (specifier .has_key () for specifier in specifiers )
111112 all_have_keys = all (
@@ -192,9 +193,8 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
192193
193194 def build_replacement_checkers (self , specifiers : List [ConversionSpecifier ],
194195 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]
198198 for specifier in specifiers :
199199 checker = self .replacement_checkers (specifier , context , expr )
200200 if checker is None :
@@ -203,13 +203,12 @@ def build_replacement_checkers(self, specifiers: List[ConversionSpecifier],
203203 return checkers
204204
205205 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 ]]:
208207 """Returns a list of tuples of two functions that check whether a replacement is
209208 of the right type for the specifier. The first functions take a node and checks
210209 its type in the right type context. The second function just checks a type.
211210 """
212- checkers = [] # type: List[Tuple[Callable[[Expression], None], Callable[[Type], None]] ]
211+ checkers = [] # type: List[Checkers ]
213212
214213 if specifier .width == '*' :
215214 checkers .append (self .checkers_for_star (context ))
@@ -227,14 +226,13 @@ def replacement_checkers(self, specifier: ConversionSpecifier, context: Context,
227226 checkers .append (c )
228227 return checkers
229228
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 :
232230 """Returns a tuple of check functions that check whether, respectively,
233231 a node or a type is compatible with a star in a conversion specifier
234232 """
235233 expected = self .named_type ('builtins.int' )
236234
237- def check_type (type : Type = None ) -> None :
235+ def check_type (type : Type ) -> None :
238236 expected = self .named_type ('builtins.int' )
239237 self .chk .check_subtype (type , expected , context , '* wants int' )
240238
@@ -246,16 +244,16 @@ def check_expr(expr: Expression) -> None:
246244
247245 def checkers_for_regular_type (self , type : str ,
248246 context : Context ,
249- expr : FormatStringExpr ) -> Tuple [Callable [[Expression ], None ],
250- Callable [[Type ], None ]]:
247+ expr : FormatStringExpr ) -> Optional [Checkers ]:
251248 """Returns a tuple of check functions that check whether, respectively,
252249 a node or a type is compatible with 'type'. Return None in case of an
253250 """
254251 expected_type = self .conversion_type (type , context , expr )
255252 if expected_type is None :
256253 return None
257254
258- def check_type (type : Type = None ) -> None :
255+ def check_type (type : Type ) -> None :
256+ assert expected_type is not None
259257 self .chk .check_subtype (type , expected_type , context ,
260258 messages .INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION ,
261259 'expression has type' , 'placeholder has type' )
@@ -268,16 +266,16 @@ def check_expr(expr: Expression) -> None:
268266
269267 def checkers_for_c_type (self , type : str ,
270268 context : Context ,
271- expr : FormatStringExpr ) -> Tuple [Callable [[Expression ], None ],
272- Callable [[Type ], None ]]:
269+ expr : FormatStringExpr ) -> Optional [Checkers ]:
273270 """Returns a tuple of check functions that check whether, respectively,
274271 a node or a type is compatible with 'type' that is a character type
275272 """
276273 expected_type = self .conversion_type (type , context , expr )
277274 if expected_type is None :
278275 return None
279276
280- def check_type (type : Type = None ) -> None :
277+ def check_type (type : Type ) -> None :
278+ assert expected_type is not None
281279 self .chk .check_subtype (type , expected_type , context ,
282280 messages .INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION ,
283281 'expression has type' , 'placeholder has type' )
@@ -291,7 +289,7 @@ def check_expr(expr: Expression) -> None:
291289
292290 return check_expr , check_type
293291
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 ] :
295293 """Return the type that is accepted for a string interpolation
296294 conversion specifier type.
297295
0 commit comments