From 538786b61c43083a3f002c4a24d987737e0a7747 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Wed, 7 May 2025 16:29:13 -0700 Subject: [PATCH 1/3] add --strict-bytes to --strict This is a check that ensures static correctness, so it is useful to have here. Unlike making this the default behavior eventually in 2.0 (which we are also planning to do), it can be added to --strict immediately due to --strict having looser backwards-compatibility requirements (or so I interpret its documentation). This commit also includes tests. --- mypy/main.py | 2 +- test-data/unit/check-flags.test | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mypy/main.py b/mypy/main.py index 7bd7215bbe2a..77351e2b56c5 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -898,7 +898,7 @@ def add_invertible_flag( add_invertible_flag( "--strict-bytes", default=False, - strict_flag=False, + strict_flag=True, help="Disable treating bytearray and memoryview as subtypes of bytes", group=strictness_group, ) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 2a75b465099b..0df18ce51425 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2408,6 +2408,21 @@ f(bytearray(b"asdf")) f(memoryview(b"asdf")) [builtins fixtures/primitives.pyi] +[case testStrictBytesDisabledByDefault] +# TODO: probably change this default in Mypy v2.0, with https://github.com/python/mypy/pull/18371 +# (this would also obsolete the testStrictBytesEnabledByStrict test, below) +def f(x: bytes) -> None: ... +f(bytearray(b"asdf")) +f(memoryview(b"asdf")) +[builtins fixtures/primitives.pyi] + +[case testStrictBytesEnabledByStrict] +# flags: --strict +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 testNoCrashFollowImportsForStubs] # flags: --config-file tmp/mypy.ini {**{"x": "y"}} From 048def646140904019ff6f165dcc569c38f6c4b6 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Wed, 7 May 2025 18:17:38 -0700 Subject: [PATCH 2/3] deal with type-arg error in test code --- test-data/unit/check-flags.test | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 0df18ce51425..f628fdd68ce8 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2417,7 +2417,9 @@ f(memoryview(b"asdf")) [builtins fixtures/primitives.pyi] [case testStrictBytesEnabledByStrict] -# flags: --strict +# flags: --strict --disable-error-code type-arg +# The type-arg thing is just work around the primitives.pyi isinstance Tuple not having type parameters, +# which isn't important for this. 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" From 43c8f199073d470dc126d697f79c4387338a46c3 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Wed, 7 May 2025 19:20:03 -0700 Subject: [PATCH 3/3] get rid of strict-bytes in mypy_self_check.ini, as it is now redundant --- mypy_self_check.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/mypy_self_check.ini b/mypy_self_check.ini index 816e6321c06f..8bf7a514f481 100644 --- a/mypy_self_check.ini +++ b/mypy_self_check.ini @@ -1,7 +1,6 @@ [mypy] strict = True -strict_bytes = True local_partial_types = True disallow_any_unimported = True show_traceback = True