From 6427ef17f0180422e0113bc67440d2b911d68f39 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Tue, 10 Dec 2024 13:13:53 +0100 Subject: [PATCH] Add `--strict-bytes` flag (#18263) Closes #18256 --- docs/source/command_line.rst | 29 +++++++++++++++++++++++++++++ docs/source/config_file.rst | 8 ++++++++ mypy/main.py | 13 +++++++++++++ mypy/options.py | 4 ++++ test-data/unit/check-flags.test | 14 ++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index ea96e9f64790..17758484f243 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -659,6 +659,35 @@ of the above sections. assert text is not None # OK, check against None is allowed as a special case. + +.. option:: --strict-bytes + + By default, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes`` which + is not true at runtime. Use this flag to disable this behavior. ``--strict-bytes`` will + be enabled by default in *mypy 2.0*. + + .. code-block:: python + + def f(buf: bytes) -> None: + assert isinstance(buf, bytes) # Raises runtime AssertionError with bytearray/memoryview + with open("binary_file", "wb") as fp: + fp.write(buf) + + f(bytearray(b"")) # error: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" + f(memoryview(b"")) # error: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes" + + # If `f` accepts any object that implements the buffer protocol, consider using: + from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier + + def f(buf: Buffer) -> None: + with open("binary_file", "wb") as fp: + fp.write(buf) + + f(b"") # Ok + f(bytearray(b"")) # Ok + f(memoryview(b"")) # Ok + + .. option:: --extra-checks This flag enables additional checks that are technically correct but may be diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index d7ae1b7a00df..747ef3a9fdaa 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -778,6 +778,14 @@ section of the command line docs. Prohibit equality checks, identity checks, and container checks between non-overlapping types. +.. confval:: strict_bytes + + :type: boolean + :default: False + + Disable treating ``bytearray`` and ``memoryview`` as subtypes of ``bytes``. + This will be enabled by default in *mypy 2.0*. + .. confval:: strict :type: boolean diff --git a/mypy/main.py b/mypy/main.py index 7032682c9fd0..e1c9f20400bc 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -859,6 +859,14 @@ def add_invertible_flag( group=strictness_group, ) + add_invertible_flag( + "--strict-bytes", + default=False, + strict_flag=False, + help="Disable treating bytearray and memoryview as subtypes of bytes", + group=strictness_group, + ) + add_invertible_flag( "--extra-checks", default=False, @@ -1386,6 +1394,11 @@ def set_strict_flags() -> None: process_cache_map(parser, special_opts, options) + # Process --strict-bytes + if options.strict_bytes: + options.disable_bytearray_promotion = True + options.disable_memoryview_promotion = True + # An explicitly specified cache_fine_grained implies local_partial_types # (because otherwise the cache is not compatible with dmypy) if options.cache_fine_grained: diff --git a/mypy/options.py b/mypy/options.py index 33a2c75d164e..eb3d389b5d8a 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -67,6 +67,7 @@ class BuildType: "plugins", "disable_bytearray_promotion", "disable_memoryview_promotion", + "strict_bytes", } ) - {"debug_cache"} @@ -215,6 +216,9 @@ def __init__(self) -> None: # This makes 1 == '1', 1 in ['1'], and 1 is '1' errors. self.strict_equality = False + # Disable treating bytearray and memoryview as subtypes of bytes + self.strict_bytes = False + # Deprecated, use extra_checks instead. self.strict_concatenate = False diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index c3a5f9e3bc04..86a65d85a8b2 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2393,6 +2393,20 @@ def f(x: bytes, y: bytearray, z: memoryview) -> None: x in z [builtins fixtures/primitives.pyi] +[case testStrictBytes] +# flags: --strict-bytes +def f(x: bytes) -> None: ... +f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" +f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes" +[builtins fixtures/primitives.pyi] + +[case testNoStrictBytes] +# flags: --no-strict-bytes +def f(x: bytes) -> None: ... +f(bytearray(b"asdf")) +f(memoryview(b"asdf")) +[builtins fixtures/primitives.pyi] + [case testNoCrashFollowImportsForStubs] # flags: --config-file tmp/mypy.ini {**{"x": "y"}}