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

Method check_s_special_cases

mypy/checkstrformat.py:952–973  ·  view source on GitHub ↗

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

(self, expr: FormatStringExpr, typ: Type, context: Context)

Source from the content-addressed store, hash-verified

950 return check_expr, check_type
951
952 def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Context) -> bool:
953 """Additional special cases for %s in bytes vs string context."""
954 if isinstance(expr, StrExpr):
955 # Couple special cases for string formatting.
956 if has_type_component(typ, "builtins.bytes"):
957 self.msg.fail(
958 'If x = b\'abc\' then "%s" % x produces "b\'abc\'", not "abc". '
959 'If this is desired behavior use "%r" % x. Otherwise, decode the bytes',
960 context,
961 code=codes.STR_BYTES_PY3,
962 )
963 return False
964 if isinstance(expr, BytesExpr):
965 # A special case for bytes formatting: b'%s' actually requires bytes on Python 3.
966 if has_type_component(typ, "builtins.str"):
967 self.msg.fail(
968 "On Python 3 b'%s' requires bytes, not string",
969 context,
970 code=codes.STRING_FORMATTING,
971 )
972 return False
973 return True
974
975 def checkers_for_c_type(
976 self, type: str, context: Context, format_expr: FormatStringExpr

Callers 2

check_typeMethod · 0.95

Calls 3

isinstanceFunction · 0.85
has_type_componentFunction · 0.85
failMethod · 0.45

Tested by

no test coverage detected