module documentation

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
Type Alias Checkers Undocumented
Type Alias FormatStringExpr Undocumented
Type Alias MatchMap Undocumented
def compile_format_re() -> Pattern[str]: (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.

def compile_new_format_re(custom_spec: bool) -> Pattern[str]: (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.

def find_non_escaped_targets(format_value: str, ctx: Context, msg: MessageBuilder) -> list[tuple[str, int]]|None: (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.

def has_type_component(typ: Type, fullname: str) -> bool: (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.

def parse_conversion_specifiers(format_str: str) -> list[ConversionSpecifier]: (source)

Parse c-printf-style format string into list of conversion specifiers.

def parse_format_value(format_value: str, ctx: Context, msg: MessageBuilder, nested: bool = False) -> list[ConversionSpecifier]|None: (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.

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'])
Checkers: _TypeAlias = (source)

Undocumented

Value
Tuple[Callable[[Expression], None], Callable[[Type], bool]]
FormatStringExpr: _TypeAlias = (source)

Undocumented

Value
Union[StrExpr, BytesExpr]
MatchMap: _TypeAlias = (source)

Undocumented

Value
Dict[Tuple[int, int], Match[str]]