module documentation
(source)

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 ​Conversion​Specifier Undocumented
Class ​String​Formatter​Checker 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 ​Format​String​Expr Undocumented
Variable ​Match​Map Undocumented
def compile_format_re(): (source)

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[str]Undocumented
def compile_new_format_re(custom_spec): (source)

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:boolUndocumented
Returns
Pattern[str]Undocumented
def find_non_escaped_targets(format_value, ctx, msg): (source)

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:strUndocumented
ctx:ContextUndocumented
msg:MessageBuilderUndocumented
Returns
Optional[List[Tuple[str, int]]]Undocumented
def has_type_component(typ, fullname): (source)

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:TypeUndocumented
fullname:strUndocumented
Returns
boolUndocumented
def parse_conversion_specifiers(format_str): (source)
Parse c-printf-style format string into list of conversion specifiers.
Parameters
format​_str:strUndocumented
Returns
List[ConversionSpecifier]Undocumented
def parse_format_value(format_value, ctx, msg, nested=False): (source)

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:strUndocumented
ctx:ContextUndocumented
msg:MessageBuilderUndocumented
nested:boolUndocumented
Returns
Optional[List[ConversionSpecifier]]Undocumented
DUMMY_FIELD_NAME: str = (source)

Undocumented

Value
'__dummy_name__'
FLOAT_TYPES: set[str] = (source)

Undocumented

Value
set(['e', 'E', 'f', 'F', 'g', 'G'])
FORMAT_RE = (source)

Undocumented

Value
compile_format_re()
FORMAT_RE_NEW = (source)

Undocumented

Value
compile_new_format_re(False)
FORMAT_RE_NEW_CUSTOM = (source)

Undocumented

Value
compile_new_format_re(True)
NUMERIC_TYPES_NEW: set[str] = (source)

Undocumented

Value
set(['b', 'd', 'o', 'e', 'E', 'f', 'F', 'g', 'G', 'n', 'x', 'X', '%'])
NUMERIC_TYPES_OLD: set[str] = (source)

Undocumented

Value
set(['d', 'i', 'o', 'u', 'x', 'X', 'e', 'E', 'f', 'F', 'g', 'G'])
REQUIRE_INT_NEW: set[str] = (source)

Undocumented

Value
set(['b', 'd', 'o', 'x', 'X'])
REQUIRE_INT_OLD: set[str] = (source)

Undocumented

Value
set(['o', 'x', 'X'])
SUPPORTED_TYPES_NEW: set[str] = (source)

Undocumented

Value
set(['b', 'c', 'd', 'e', 'E', 'f', 'F', 'g', 'G', 'n', 'o', 's', 'x', 'X', '%'])
Checkers: _TypeAlias = (source)

Undocumented

FormatStringExpr: _TypeAlias = (source)

Undocumented

MatchMap: _TypeAlias = (source)

Undocumented