class StringFormatterChecker: (source)
String interpolation/formatter type checker.
This class works closely together with checker.ExpressionChecker.
Method | __init__ |
Construct an expression type checker. |
Method | accept |
Type check a node. Alias for TypeChecker.accept. |
Method | analyze_conversion_specifiers |
Undocumented |
Method | apply_field_accessors |
Transform and validate expr in '{.attr[item]}'.format(expr) into expr.attr['item']. |
Method | auto_generate_keys |
Translate '{} {name} {}' to '{0} {name} {1}'. |
Method | build_dict_type |
Build expected mapping type for right operand in % formatting. |
Method | build_replacement_checkers |
Undocumented |
Method | check_mapping_str_interpolation |
Check % string interpolation with names specifiers '%(name)s' % {'name': 'John'}. |
Method | check_placeholder_type |
Undocumented |
Method | check_s_special_cases |
Additional special cases for %s in bytes vs string context. |
Method | check_simple_str_interpolation |
Check % string interpolation with positional specifiers '%s, %d' % ('yes, 42'). |
Method | check_specs_in_format_call |
Perform pairwise checks for conversion specifiers vs their replacements. |
Method | check_str_format_call |
Perform more precise checks for str.format() calls when possible. |
Method | check_str_interpolation |
Check the types of the 'replacements' in a string interpolation expression: str % replacements. |
Method | checkers_for_c_type |
Returns a tuple of check functions that check whether, respectively, a node or a type is compatible with 'type' that is a character type. |
Method | checkers_for_regular_type |
Returns a tuple of check functions that check whether, respectively, a node or a type is compatible with 'type'. Return None in case of an error. |
Method | checkers_for_star |
Returns a tuple of check functions that check whether, respectively, a node or a type is compatible with a star in a conversion specifier. |
Method | conversion_type |
Return the type that is accepted for a string interpolation conversion specifier type. |
Method | find_replacements_in_call |
Find replacement expression for every specifier in str.format() call. |
Method | get_expr_by_name |
Get named replacement expression from '{name}'.format(name=...) call. |
Method | get_expr_by_position |
Get positional replacement expression from '{0}, {1}'.format(x, y, ...) call. |
Method | named_type |
Return an instance type with type given by the name and no type arguments. Alias for TypeChecker.named_type. |
Method | perform_special_format_checks |
Undocumented |
Method | replacement_checkers |
Returns a list of tuples of two functions that check whether a replacement is of the right type for the specifier. The first function takes a node and checks its type in the right type context. The second function just checks a type. |
Method | validate_and_transform_accessors |
Validate and transform (in-place) format field accessors. |
Instance Variable | chk |
Undocumented |
Instance Variable | exprchk |
Undocumented |
Instance Variable | msg |
Undocumented |
Instance Variable | unicode_upcast |
Undocumented |
Parameters | |
exprchk:mypy.checkexpr.ExpressionChecker | Undocumented |
chk:mypy.checker.TypeChecker | Undocumented |
msg:MessageBuilder | Undocumented |
Parameters | |
expr:Expression | Undocumented |
context:Optional[ | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
specifiers:List[ | Undocumented |
context:Context | Undocumented |
Returns | |
Optional[ | Undocumented |
Transform and validate expr in '{.attr[item]}'.format(expr) into expr.attr['item'].
If validation fails, return TempNode(AnyType).
Parameters | |
spec:ConversionSpecifier | Undocumented |
repl:Expression | Undocumented |
ctx:Context | Undocumented |
Returns | |
Expression | Undocumented |
Translate '{} {name} {}' to '{0} {name} {1}'.
Return True if generation was successful, otherwise report an error and return false.
Parameters | |
all_specs:List[ | Undocumented |
ctx:Context | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
expr:FormatStringExpr | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
specifiers:List[ | Undocumented |
context:Context | Undocumented |
expr:FormatStringExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
specifiers:List[ | Undocumented |
replacements:Expression | Undocumented |
expr:FormatStringExpr | Undocumented |
Parameters | |
expr:FormatStringExpr | Undocumented |
typ:Type | Undocumented |
context:Context | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
specifiers:List[ | Undocumented |
replacements:Expression | Undocumented |
expr:FormatStringExpr | Undocumented |
Perform pairwise checks for conversion specifiers vs their replacements.
The core logic for format checking is implemented in this method.
Parameters | |
call:CallExpr | Undocumented |
specs:List[ | Undocumented |
format_value:str | Undocumented |
Perform more precise checks for str.format() calls when possible.
Parameters | |
call:CallExpr | Undocumented |
format_value:str | Undocumented |
Parameters | |
expr:FormatStringExpr | Undocumented |
replacements:Expression | Undocumented |
Returns | |
Type | Undocumented |
Parameters | |
type:str | Undocumented |
context:Context | Undocumented |
format_expr:FormatStringExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
conv_type:str | Undocumented |
context:Context | Undocumented |
expr:FormatStringExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Return the type that is accepted for a string interpolation conversion specifier type.
Note that both Python's float (e.g. %f) and integer (e.g. %d) specifier types accept both float and integers.
The 'format_call' argument indicates whether this type came from % interpolation or from a str.format() call, the meaning of few formatting types are different.
Parameters | |
p:str | Undocumented |
context:Context | Undocumented |
expr:FormatStringExpr | Undocumented |
format_call:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Find replacement expression for every specifier in str.format() call.
In case of an error use TempNode(AnyType).
Parameters | |
call:CallExpr | Undocumented |
keys:List[ | Undocumented |
Returns | |
List[ | Undocumented |
Get named replacement expression from '{name}'.format(name=...) call.
If the type is from **kwargs, return TempNode(<item type>). Return None in case of an error.
Parameters | |
key:str | Undocumented |
call:CallExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Get positional replacement expression from '{0}, {1}'.format(x, y, ...) call.
If the type is from *args, return TempNode(<item type>). Return None in case of an error.
Parameters | |
pos:int | Undocumented |
call:CallExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
name:str | Undocumented |
Returns | |
Instance | Undocumented |
Undocumented
Parameters | |
spec:ConversionSpecifier | Undocumented |
call:CallExpr | Undocumented |
repl:Expression | Undocumented |
actual_type:Type | Undocumented |
expected_type:Type | Undocumented |
Parameters | |
specifier:ConversionSpecifier | Undocumented |
context:Context | Undocumented |
expr:FormatStringExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Validate and transform (in-place) format field accessors.
On error, report it and return False. The transformations include replacing the dummy variable with actual replacement expression and translating any name expressions in an index into strings, so that this will work:
- class User(TypedDict):
- name: str id: int
u: User '{[id]:d} -> {[name]}'.format(u)
Parameters | |
temp_ast:Expression | Undocumented |
original_repl:Expression | Undocumented |
spec:ConversionSpecifier | Undocumented |
ctx:Context | Undocumented |
Returns | |
bool | Undocumented |