Skip to content

Commit 3252e0d

Browse files
committed
Add documentation for bytes formatting error code
See python#14959
1 parent dfe0281 commit 3252e0d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/source/error_code_list.rst

Lines changed: 18 additions & 0 deletions
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 that function isn't used in boolean context [str-bytes-safe]
912+
-------------------------------------------------------------------
913+
914+
Warn about dangerous coercions related to bytes and string types.
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()}") # The alphabet starts with abc
928+
911929
Report syntax errors [syntax]
912930
-----------------------------
913931

0 commit comments

Comments
 (0)