MCPcopy Create free account
hub / github.com/python/mypy / check_str_format_call

Method check_str_format_call

mypy/checkexpr.py:658–680  ·  view source on GitHub ↗

More precise type checking for str.format() calls on literals and folded constants.

(self, e: CallExpr)

Source from the content-addressed store, hash-verified

656 return ret_type
657
658 def check_str_format_call(self, e: CallExpr) -> None:
659 """More precise type checking for str.format() calls on literals and folded constants."""
660 assert isinstance(e.callee, MemberExpr)
661 format_value = None
662 folded_callee_expr = constant_fold_expr(e.callee.expr, "<unused>")
663 if isinstance(folded_callee_expr, str):
664 format_value = folded_callee_expr
665 elif self.chk.has_type(e.callee.expr):
666 typ = get_proper_type(self.chk.lookup_type(e.callee.expr))
667 if (
668 isinstance(typ, Instance)
669 and typ.type.is_enum
670 and isinstance(typ.last_known_value, LiteralType)
671 and isinstance(typ.last_known_value.value, str)
672 ):
673 value_type = typ.type.names[typ.last_known_value.value].type
674 if isinstance(value_type, Type):
675 typ = get_proper_type(value_type)
676 base_typ = try_getting_literal(typ)
677 if isinstance(base_typ, LiteralType) and isinstance(base_typ.value, str):
678 format_value = base_typ.value
679 if format_value is not None:
680 self.strfrm_checker.check_str_format_call(e, format_value)
681
682 def method_fullname(self, object_type: Type, method_name: str) -> str | None:
683 """Convert a method name to a fully qualified name, based on the type of the object that

Callers 1

visit_call_expr_innerMethod · 0.95

Calls 6

constant_fold_exprFunction · 0.90
get_proper_typeFunction · 0.90
isinstanceFunction · 0.85
try_getting_literalFunction · 0.85
has_typeMethod · 0.80
lookup_typeMethod · 0.45

Tested by

no test coverage detected