Skip to content

Commit a9a047e

Browse files
authored
Add documentation for bytes formatting error code (#14971)
See #14959
1 parent 10ae912 commit a9a047e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: docs/source/error_code_list.rst

+18
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,24 @@ Functions will always evaluate to true in boolean contexts.
908908
if f: # Error: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
909909
pass
910910
911+
Check for implicit bytes coercions [str-bytes-safe]
912+
-------------------------------------------------------------------
913+
914+
Warn about cases where a bytes object may be converted to a string in an unexpected manner.
915+
916+
.. code-block:: python
917+
918+
b = b"abc"
919+
920+
# Error: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc".
921+
# If this is desired behavior, use f"{x!r}" or "{!r}".format(x).
922+
# Otherwise, decode the bytes [str-bytes-safe]
923+
print(f"The alphabet starts with {b}")
924+
925+
# Okay
926+
print(f"The alphabet starts with {b!r}") # The alphabet starts with b'abc'
927+
print(f"The alphabet starts with {b.decode('utf-8')}") # The alphabet starts with abc
928+
911929
Report syntax errors [syntax]
912930
-----------------------------
913931

Diff for: mypy/errorcodes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __str__(self) -> str:
113113
"str-format", "Check that string formatting/interpolation is type-safe", "General"
114114
)
115115
STR_BYTES_PY3: Final = ErrorCode(
116-
"str-bytes-safe", "Warn about dangerous coercions related to bytes and string types", "General"
116+
"str-bytes-safe", "Warn about implicit coercions related to bytes and string types", "General"
117117
)
118118
EXIT_RETURN: Final = ErrorCode(
119119
"exit-return", "Warn about too general return type for '__exit__'", "General"

0 commit comments

Comments
 (0)