class documentation

class StringFormatterChecker: (source)

View In Hierarchy

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

Construct an expression type checker.

def accept(self, expr: Expression, context: Type|None = None) -> Type: (source)

Type check a node. Alias for TypeChecker.accept.

def analyze_conversion_specifiers(self, specifiers: list[ConversionSpecifier], context: Context) -> bool|None: (source)

Undocumented

def apply_field_accessors(self, spec: ConversionSpecifier, repl: Expression, ctx: Context) -> Expression: (source)

Transform and validate expr in '{.attr[item]}'.format(expr) into expr.attr['item']. If validation fails, return TempNode(AnyType).

def auto_generate_keys(self, all_specs: list[ConversionSpecifier], ctx: Context) -> bool: (source)

Translate '{} {name} {}' to '{0} {name} {1}'. Return True if generation was successful, otherwise report an error and return false.

def build_dict_type(self, expr: FormatStringExpr) -> Type: (source)

Build expected mapping type for right operand in % formatting.

def build_replacement_checkers(self, specifiers: list[ConversionSpecifier], context: Context, expr: FormatStringExpr) -> list[Checkers]|None: (source)

Undocumented

def check_mapping_str_interpolation(self, specifiers: list[ConversionSpecifier], replacements: Expression, expr: FormatStringExpr): (source)

Check % string interpolation with names specifiers '%(name)s' % {'name': 'John'}.

def check_placeholder_type(self, typ: Type, expected_type: Type, context: Context) -> bool: (source)

Undocumented

def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Context) -> bool: (source)

Additional special cases for %s in bytes vs string context.

def check_simple_str_interpolation(self, specifiers: list[ConversionSpecifier], replacements: Expression, expr: FormatStringExpr): (source)

Check % string interpolation with positional specifiers '%s, %d' % ('yes, 42').

def check_specs_in_format_call(self, call: CallExpr, specs: list[ConversionSpecifier], format_value: str): (source)

Perform pairwise checks for conversion specifiers vs their replacements. The core logic for format checking is implemented in this method.

def check_str_format_call(self, call: CallExpr, format_value: str): (source)

Perform more precise checks for str.format() calls when possible. Currently the checks are performed for: * Actual string literals * Literal types with string values * Final names with string values The checks that we currently perform: * Check generic validity (e.g. unmatched { or }, and {} in invalid positions) * Check consistency of specifiers' auto-numbering * Verify that replacements can be found for all conversion specifiers, and all arguments were used * Non-standard format specs are only allowed for types with custom __format__ * Type check replacements with accessors applied (if any). * Verify that specifier type is known and matches replacement type * Perform special checks for some specifier types: - 'c' requires a single character string - 's' must not accept bytes - non-empty flags are only allowed for numeric types

def check_str_interpolation(self, expr: FormatStringExpr, replacements: Expression) -> Type: (source)

Check the types of the 'replacements' in a string interpolation expression: str % replacements.

def checkers_for_c_type(self, type: str, context: Context, format_expr: FormatStringExpr) -> Checkers|None: (source)

Returns a tuple of check functions that check whether, respectively, a node or a type is compatible with 'type' that is a character type.

def checkers_for_regular_type(self, conv_type: str, context: Context, expr: FormatStringExpr) -> Checkers|None: (source)

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.

def checkers_for_star(self, context: Context) -> Checkers: (source)

Returns a tuple of check functions that check whether, respectively, a node or a type is compatible with a star in a conversion specifier.

def conversion_type(self, p: str, context: Context, expr: FormatStringExpr, format_call: bool = False) -> Type|None: (source)

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.

def find_replacements_in_call(self, call: CallExpr, keys: list[str]) -> list[Expression]: (source)

Find replacement expression for every specifier in str.format() call. In case of an error use TempNode(AnyType).

def get_expr_by_name(self, key: str, call: CallExpr) -> Expression|None: (source)

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.

def get_expr_by_position(self, pos: int, call: CallExpr) -> Expression|None: (source)

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.

def named_type(self, name: str) -> Instance: (source)

Return an instance type with type given by the name and no type arguments. Alias for TypeChecker.named_type.

def perform_special_format_checks(self, spec: ConversionSpecifier, call: CallExpr, repl: Expression, actual_type: Type, expected_type: Type): (source)

Undocumented

def replacement_checkers(self, specifier: ConversionSpecifier, context: Context, expr: FormatStringExpr) -> list[Checkers]|None: (source)

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.

def validate_and_transform_accessors(self, temp_ast: Expression, original_repl: Expression, spec: ConversionSpecifier, ctx: Context) -> bool: (source)

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)

Undocumented

Undocumented

Undocumented