Additional special cases for %s in bytes vs string context.
(self, expr: FormatStringExpr, typ: Type, context: Context)
| 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 |
no test coverage detected