From 3252e0d2ebdb270d3aa5ffab7bbcdff0a36656b1 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 28 Mar 2023 20:50:28 -0700 Subject: [PATCH 1/5] Add documentation for bytes formatting error code See #14959 --- docs/source/error_code_list.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index 1f08a36414e9c..9cfe81e36f98c 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 that function isn't used in boolean context [str-bytes-safe] +------------------------------------------------------------------- + +Warn about dangerous coercions related to bytes and string types. + +.. 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()}") # The alphabet starts with abc + Report syntax errors [syntax] ----------------------------- From 9cda59a168dfc677e1300bba62c7dfb8fcecb501 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Tue, 28 Mar 2023 22:02:40 -0700 Subject: [PATCH 2/5] Update docs/source/error_code_list.rst Co-authored-by: Jelle Zijlstra --- docs/source/error_code_list.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index 9cfe81e36f98c..a6d3fd8c40bd0 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -908,7 +908,7 @@ 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 that function isn't used in boolean context [str-bytes-safe] +Check for unexpected bytes coercions [str-bytes-safe] ------------------------------------------------------------------- Warn about dangerous coercions related to bytes and string types. From 95b4a383da0373de5c1ba4c735685ad3504dd6c0 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 28 Mar 2023 22:03:44 -0700 Subject: [PATCH 3/5] unexpected --- docs/source/error_code_list.rst | 2 +- mypy/errorcodes.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index a6d3fd8c40bd0..406e350e8e8f9 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -911,7 +911,7 @@ Functions will always evaluate to true in boolean contexts. Check for unexpected bytes coercions [str-bytes-safe] ------------------------------------------------------------------- -Warn about dangerous coercions related to bytes and string types. +Warn about cases where a bytes object may be converted to a string in an unexpected manner. .. code-block:: python diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 2eb2d5c624b6d..722a9ce7c1380 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 unexpected coercions related to bytes and string types", "General" ) EXIT_RETURN: Final = ErrorCode( "exit-return", "Warn about too general return type for '__exit__'", "General" From 0adfaedaabac1cd91ee2c106da30f3dd5ced9292 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 28 Mar 2023 22:05:15 -0700 Subject: [PATCH 4/5] implicit --- docs/source/error_code_list.rst | 2 +- mypy/errorcodes.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index 406e350e8e8f9..bb7f08ac9c8f9 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -908,7 +908,7 @@ 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 unexpected bytes coercions [str-bytes-safe] +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. diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 722a9ce7c1380..19cdadba0e16f 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 unexpected 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" From 1089c8153f14a70c70338bc09e4594e9afcb2c6f Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 28 Mar 2023 22:06:07 -0700 Subject: [PATCH 5/5] explicit encoding --- docs/source/error_code_list.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index bb7f08ac9c8f9..54dc31f2cfcbb 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -924,7 +924,7 @@ Warn about cases where a bytes object may be converted to a string in an unexpec # Okay print(f"The alphabet starts with {b!r}") # The alphabet starts with b'abc' - print(f"The alphabet starts with {b.decode()}") # The alphabet starts with abc + print(f"The alphabet starts with {b.decode('utf-8')}") # The alphabet starts with abc Report syntax errors [syntax] -----------------------------