diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index 1f08a36414e9..54dc31f2cfcb 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -908,6 +908,24 @@ Functions will always evaluate to true in boolean contexts. if f: # Error: Function "Callable[[], Any]" could always be true in boolean context [truthy-function] pass +Check for implicit bytes coercions [str-bytes-safe] +------------------------------------------------------------------- + +Warn about cases where a bytes object may be converted to a string in an unexpected manner. + +.. code-block:: python + + b = b"abc" + + # Error: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". + # If this is desired behavior, use f"{x!r}" or "{!r}".format(x). + # Otherwise, decode the bytes [str-bytes-safe] + print(f"The alphabet starts with {b}") + + # Okay + print(f"The alphabet starts with {b!r}") # The alphabet starts with b'abc' + print(f"The alphabet starts with {b.decode('utf-8')}") # The alphabet starts with abc + Report syntax errors [syntax] ----------------------------- diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 2eb2d5c624b6..19cdadba0e16 100644 --- a/mypy/errorcodes.py +++ b/mypy/errorcodes.py @@ -113,7 +113,7 @@ def __str__(self) -> str: "str-format", "Check that string formatting/interpolation is type-safe", "General" ) STR_BYTES_PY3: Final = ErrorCode( - "str-bytes-safe", "Warn about dangerous coercions related to bytes and string types", "General" + "str-bytes-safe", "Warn about implicit coercions related to bytes and string types", "General" ) EXIT_RETURN: Final = ErrorCode( "exit-return", "Warn about too general return type for '__exit__'", "General"