Format expression type checker.
This file is conceptually part of ExpressionChecker and TypeChecker. Main functionality is located in StringFormatterChecker.check_str_format_call() for '{}'.format(), and in StringFormatterChecker.check_str_interpolation() for printf-style % interpolation.
Note that although at runtime format strings are parsed using custom parsers, here we use a regexp-based approach. This way we 99% match runtime behaviour while keeping implementation simple.
Class | ConversionSpecifier |
Undocumented |
Class | StringFormatterChecker |
String interpolation/formatter type checker. |
Function | compile_format_re |
Construct regexp to match format conversion specifiers in % interpolation. |
Function | compile_new_format_re |
Construct regexps to match format conversion specifiers in str.format() calls. |
Function | find_non_escaped_targets |
Return list of raw (un-parsed) format specifiers in format string. |
Function | has_type_component |
Is this a specific instance type, or a union that contains it? |
Function | parse_conversion_specifiers |
Parse c-printf-style format string into list of conversion specifiers. |
Function | parse_format_value |
Parse format string into list of conversion specifiers. |
Constant | DUMMY_FIELD_NAME |
Undocumented |
Constant | FLOAT_TYPES |
Undocumented |
Constant | FORMAT_RE |
Undocumented |
Constant | FORMAT_RE_NEW |
Undocumented |
Constant | FORMAT_RE_NEW_CUSTOM |
Undocumented |
Constant | NUMERIC_TYPES_NEW |
Undocumented |
Constant | NUMERIC_TYPES_OLD |
Undocumented |
Constant | REQUIRE_INT_NEW |
Undocumented |
Constant | REQUIRE_INT_OLD |
Undocumented |
Constant | SUPPORTED_TYPES_NEW |
Undocumented |
Variable | Checkers |
Undocumented |
Variable | FormatStringExpr |
Undocumented |
Variable | MatchMap |
Undocumented |
Construct regexp to match format conversion specifiers in % interpolation.
See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting The regexp is intentionally a bit wider to report better errors.
Returns | |
Pattern[ | Undocumented |
Construct regexps to match format conversion specifiers in str.format() calls.
See After https://docs.python.org/3/library/string.html#formatspec for specifications. The regexps are intentionally wider, to report better errors, instead of just not matching.
Parameters | |
custom_spec:bool | Undocumented |
Returns | |
Pattern[ | Undocumented |
Return list of raw (un-parsed) format specifiers in format string.
Format specifiers don't include enclosing braces. We don't use regexp for this because they don't work well with nested/repeated patterns (both greedy and non-greedy), and these are heavily used internally for representation of f-strings.
Return None in case of an error.
Parameters | |
format_value:str | Undocumented |
ctx:Context | Undocumented |
msg:MessageBuilder | Undocumented |
Returns | |
Optional[ | Undocumented |
Is this a specific instance type, or a union that contains it?
We use this ad-hoc function instead of a proper visitor or subtype check because some str vs bytes errors are strictly speaking not runtime errors, but rather highly counter-intuitive behavior. This is similar to what is used for --strict-equality.
Parameters | |
typ:Type | Undocumented |
fullname:str | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
format_str:str | Undocumented |
Returns | |
List[ | Undocumented |
Parse format string into list of conversion specifiers.
The specifiers may be nested (two levels maximum), in this case they are ordered as '{0:{1}}, {2:{3}{4}}'. Return None in case of an error.
Parameters | |
format_value:str | Undocumented |
ctx:Context | Undocumented |
msg:MessageBuilder | Undocumented |
nested:bool | Undocumented |
Returns | |
Optional[ | Undocumented |