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
Instance Variable unicode​_upcast Undocumented
def __init__(self, exprchk, chk, msg): (source)
Construct an expression type checker.
Parameters
exprchk:mypy.checkexpr.ExpressionCheckerUndocumented
chk:mypy.checker.TypeCheckerUndocumented
msg:MessageBuilderUndocumented
def accept(self, expr, context=None): (source)
Type check a node. Alias for TypeChecker.accept.
Parameters
expr:ExpressionUndocumented
context:Optional[Type]Undocumented
Returns
TypeUndocumented
def analyze_conversion_specifiers(self, specifiers, context): (source)

Undocumented

Parameters
specifiers:List[ConversionSpecifier]Undocumented
context:ContextUndocumented
Returns
Optional[bool]Undocumented
def apply_field_accessors(self, spec, repl, ctx): (source)

Transform and validate expr in '{.attr[item]}'.format(expr) into expr.attr['item'].

If validation fails, return TempNode(AnyType).

Parameters
spec:ConversionSpecifierUndocumented
repl:ExpressionUndocumented
ctx:ContextUndocumented
Returns
ExpressionUndocumented
def auto_generate_keys(self, all_specs, ctx): (source)

Translate '{} {name} {}' to '{0} {name} {1}'.

Return True if generation was successful, otherwise report an error and return false.

Parameters
all​_specs:List[ConversionSpecifier]Undocumented
ctx:ContextUndocumented
Returns
boolUndocumented
def build_dict_type(self, expr): (source)
Build expected mapping type for right operand in % formatting.
Parameters
expr:FormatStringExprUndocumented
Returns
TypeUndocumented
def build_replacement_checkers(self, specifiers, context, expr): (source)

Undocumented

Parameters
specifiers:List[ConversionSpecifier]Undocumented
context:ContextUndocumented
expr:FormatStringExprUndocumented
Returns
Optional[List[Checkers]]Undocumented
def check_mapping_str_interpolation(self, specifiers, replacements, expr): (source)
Check % string interpolation with names specifiers '%(name)s' % {'name': 'John'}.
Parameters
specifiers:List[ConversionSpecifier]Undocumented
replacements:ExpressionUndocumented
expr:FormatStringExprUndocumented
def check_placeholder_type(self, typ, expected_type, context): (source)

Undocumented

Parameters
typ:TypeUndocumented
expected​_type:TypeUndocumented
context:ContextUndocumented
Returns
boolUndocumented
def check_s_special_cases(self, expr, typ, context): (source)
Additional special cases for %s in bytes vs string context.
Parameters
expr:FormatStringExprUndocumented
typ:TypeUndocumented
context:ContextUndocumented
Returns
boolUndocumented
def check_simple_str_interpolation(self, specifiers, replacements, expr): (source)
Check % string interpolation with positional specifiers '%s, %d' % ('yes, 42').
Parameters
specifiers:List[ConversionSpecifier]Undocumented
replacements:ExpressionUndocumented
expr:FormatStringExprUndocumented
def check_specs_in_format_call(self, call, specs, format_value): (source)

Perform pairwise checks for conversion specifiers vs their replacements.

The core logic for format checking is implemented in this method.

Parameters
call:CallExprUndocumented
specs:List[ConversionSpecifier]Undocumented
format​_value:strUndocumented
def check_str_format_call(self, call, format_value): (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
Parameters
call:CallExprUndocumented
format​_value:strUndocumented
def check_str_interpolation(self, expr, replacements): (source)
Check the types of the 'replacements' in a string interpolation expression: str % replacements.
Parameters
expr:FormatStringExprUndocumented
replacements:ExpressionUndocumented
Returns
TypeUndocumented
def checkers_for_c_type(self, type, context, format_expr): (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.
Parameters
type:strUndocumented
context:ContextUndocumented
format​_expr:FormatStringExprUndocumented
Returns
Optional[Checkers]Undocumented
def checkers_for_regular_type(self, conv_type, context, expr): (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.
Parameters
conv​_type:strUndocumented
context:ContextUndocumented
expr:FormatStringExprUndocumented
Returns
Optional[Checkers]Undocumented
def checkers_for_star(self, context): (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.
Parameters
context:ContextUndocumented
Returns
CheckersUndocumented
def conversion_type(self, p, context, expr, format_call=False): (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.

Parameters
p:strUndocumented
context:ContextUndocumented
expr:FormatStringExprUndocumented
format​_call:boolUndocumented
Returns
Optional[Type]Undocumented
def find_replacements_in_call(self, call, keys): (source)

Find replacement expression for every specifier in str.format() call.

In case of an error use TempNode(AnyType).

Parameters
call:CallExprUndocumented
keys:List[str]Undocumented
Returns
List[Expression]Undocumented
def get_expr_by_name(self, key, call): (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.

Parameters
key:strUndocumented
call:CallExprUndocumented
Returns
Optional[Expression]Undocumented
def get_expr_by_position(self, pos, call): (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.

Parameters
pos:intUndocumented
call:CallExprUndocumented
Returns
Optional[Expression]Undocumented
def named_type(self, name): (source)
Return an instance type with type given by the name and no type arguments. Alias for TypeChecker.named_type.
Parameters
name:strUndocumented
Returns
InstanceUndocumented
def perform_special_format_checks(self, spec, call, repl, actual_type, expected_type): (source)

Undocumented

Parameters
spec:ConversionSpecifierUndocumented
call:CallExprUndocumented
repl:ExpressionUndocumented
actual​_type:TypeUndocumented
expected​_type:TypeUndocumented
def replacement_checkers(self, specifier, context, expr): (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.
Parameters
specifier:ConversionSpecifierUndocumented
context:ContextUndocumented
expr:FormatStringExprUndocumented
Returns
Optional[List[Checkers]]Undocumented
def validate_and_transform_accessors(self, temp_ast, original_repl, spec, ctx): (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)

Parameters
temp​_ast:ExpressionUndocumented
original​_repl:ExpressionUndocumented
spec:ConversionSpecifierUndocumented
ctx:ContextUndocumented
Returns
boolUndocumented

Undocumented

exprchk = (source)

Undocumented

Undocumented

unicode_upcast: bool = (source)

Undocumented