Skip to content

Commit 772cd0c

Browse files
Add --strict-bytes to --strict (#19049)
This is a check that ensures static correctness, so it is useful to have in --strict. 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 --strict's [documentation](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-strict)). This PR also includes tests, for --strict and also for no flags.
1 parent 0b65f21 commit 772cd0c

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

mypy/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def add_invertible_flag(
917917
add_invertible_flag(
918918
"--strict-bytes",
919919
default=False,
920-
strict_flag=False,
920+
strict_flag=True,
921921
help="Disable treating bytearray and memoryview as subtypes of bytes",
922922
group=strictness_group,
923923
)

mypy_self_check.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[mypy]
22

33
strict = True
4-
strict_bytes = True
54
local_partial_types = True
65
disallow_any_unimported = True
76
show_traceback = True

test-data/unit/check-flags.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,23 @@ f(bytearray(b"asdf"))
24082408
f(memoryview(b"asdf"))
24092409
[builtins fixtures/primitives.pyi]
24102410

2411+
[case testStrictBytesDisabledByDefault]
2412+
# TODO: probably change this default in Mypy v2.0, with https://github.com/python/mypy/pull/18371
2413+
# (this would also obsolete the testStrictBytesEnabledByStrict test, below)
2414+
def f(x: bytes) -> None: ...
2415+
f(bytearray(b"asdf"))
2416+
f(memoryview(b"asdf"))
2417+
[builtins fixtures/primitives.pyi]
2418+
2419+
[case testStrictBytesEnabledByStrict]
2420+
# flags: --strict --disable-error-code type-arg
2421+
# The type-arg thing is just work around the primitives.pyi isinstance Tuple not having type parameters,
2422+
# which isn't important for this.
2423+
def f(x: bytes) -> None: ...
2424+
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2425+
f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
2426+
[builtins fixtures/primitives.pyi]
2427+
24112428
[case testNoCrashFollowImportsForStubs]
24122429
# flags: --config-file tmp/mypy.ini
24132430
{**{"x": "y"}}

0 commit comments

Comments
 (0)