MCPcopy Index your code
hub / github.com/python/mypy / check_str_interpolation

Method check_str_interpolation

mypy/checkstrformat.py:676–695  ·  view source on GitHub ↗

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

(self, expr: FormatStringExpr, replacements: Expression)

Source from the content-addressed store, hash-verified

674 # TODO: In Python 3, the bytes formatting has a more restricted set of options
675 # compared to string formatting.
676 def check_str_interpolation(self, expr: FormatStringExpr, replacements: Expression) -> Type:
677 """Check the types of the 'replacements' in a string interpolation
678 expression: str % replacements.
679 """
680 self.chk.expr_checker.accept(expr)
681 specifiers = parse_conversion_specifiers(expr.value)
682 has_mapping_keys = self.analyze_conversion_specifiers(specifiers, expr)
683 if has_mapping_keys is None:
684 pass # Error was reported
685 elif has_mapping_keys:
686 self.check_mapping_str_interpolation(specifiers, replacements, expr)
687 else:
688 self.check_simple_str_interpolation(specifiers, replacements, expr)
689
690 if isinstance(expr, BytesExpr):
691 return self.named_type("builtins.bytes")
692 elif isinstance(expr, StrExpr):
693 return self.named_type("builtins.str")
694 else:
695 assert False
696
697 def analyze_conversion_specifiers(
698 self, specifiers: list[ConversionSpecifier], context: Context

Callers 1

visit_op_exprMethod · 0.80

Calls 7

named_typeMethod · 0.95
isinstanceFunction · 0.85
acceptMethod · 0.45

Tested by

no test coverage detected