Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30e8f1d

Browse files
Enable strict_bytes by default (#18371)
For mypy v2 --------- Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent ef03ccd commit 30e8f1d

6 files changed

Lines changed: 72 additions & 77 deletions

File tree

mypy/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,9 +936,9 @@ def add_invertible_flag(
936936
)
937937

938938
add_invertible_flag(
939-
"--strict-bytes",
940-
default=False,
941-
strict_flag=True,
939+
"--no-strict-bytes",
940+
default=True,
941+
dest="strict_bytes",
942942
help="Disable treating bytearray and memoryview as subtypes of bytes",
943943
group=strictness_group,
944944
)

mypy/options.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def __init__(self) -> None:
245245
self.strict_equality_for_none = False
246246

247247
# Disable treating bytearray and memoryview as subtypes of bytes
248-
self.strict_bytes = False
248+
self.strict_bytes = True
249249

250250
# Deprecated, use extra_checks instead.
251251
self.strict_concatenate = False
@@ -423,8 +423,8 @@ def __init__(self) -> None:
423423
# Set to False when running stubtest.
424424
self.pos_only_special_methods = True
425425

426-
self.disable_bytearray_promotion = False
427-
self.disable_memoryview_promotion = False
426+
self.disable_bytearray_promotion = True
427+
self.disable_memoryview_promotion = True
428428

429429
# Sets custom output format
430430
self.output: str | None = None
@@ -487,9 +487,9 @@ def process_strict_bytes(self) -> None:
487487
# backwards compatibility
488488
self.disable_bytearray_promotion = True
489489
self.disable_memoryview_promotion = True
490-
elif self.disable_bytearray_promotion and self.disable_memoryview_promotion:
491-
# forwards compatibility
492-
self.strict_bytes = True
490+
else:
491+
self.disable_bytearray_promotion = False
492+
self.disable_memoryview_promotion = False
493493

494494
def apply_changes(self, changes: dict[str, object]) -> Options:
495495
# Note: effects of this method *must* be idempotent.

mypy/test/testargs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from mypy.main import infer_python_executable, process_options
1515
from mypy.options import Options
16-
from mypy.test.helpers import Suite, assert_equal
16+
from mypy.test.helpers import Suite
1717

1818

1919
class ArgSuite(Suite):
@@ -22,7 +22,7 @@ def test_coherence(self) -> None:
2222
_, parsed_options = process_options([], require_targets=False)
2323
# FIX: test this too. Requires changing working dir to avoid finding 'setup.cfg'
2424
options.config_file = parsed_options.config_file
25-
assert_equal(options.snapshot(), parsed_options.snapshot())
25+
assert options.snapshot() == parsed_options.snapshot()
2626

2727
def test_executable_inference(self) -> None:
2828
"""Test the --python-executable flag with --python-version"""

mypyc/test-data/fixtures/ir.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def __getitem__(self, i: int) -> int: ...
204204
@overload
205205
def __getitem__(self, i: slice) -> bytearray: ...
206206
def decode(self, x: str = ..., y: str = ...) -> str: ...
207+
def join(self, x: Iterable[object]) -> bytes: ...
207208
def startswith(self, t: bytes) -> bool: ...
208209
def endswith(self, t: bytes) -> bool: ...
209210

test-data/unit/check-flags.test

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,55 +2542,6 @@ x: int = "" # E: Incompatible types in assignment (expression has type "str", v
25422542
# flags: --hide-error-codes
25432543
x: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
25442544

2545-
[case testDisableBytearrayPromotion]
2546-
# flags: --disable-bytearray-promotion --strict-equality --warn-unreachable
2547-
def f(x: bytes) -> None: ...
2548-
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2549-
f(memoryview(b"asdf"))
2550-
ba = bytearray(b"")
2551-
if ba == b"":
2552-
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2553-
if b"" == ba:
2554-
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2555-
if ba == bytes():
2556-
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2557-
if bytes() == ba:
2558-
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2559-
[builtins fixtures/primitives.pyi]
2560-
2561-
[case testDisableMemoryviewPromotion]
2562-
# flags: --disable-memoryview-promotion
2563-
def f(x: bytes) -> None: ...
2564-
f(bytearray(b"asdf"))
2565-
f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
2566-
[builtins fixtures/primitives.pyi]
2567-
2568-
[case testDisableBytearrayMemoryviewPromotionStrictEquality]
2569-
# flags: --disable-bytearray-promotion --disable-memoryview-promotion --strict-equality
2570-
def f(x: bytes, y: bytearray, z: memoryview) -> None:
2571-
x == y
2572-
y == z
2573-
x == z
2574-
97 in x
2575-
97 in y
2576-
97 in z
2577-
x in y
2578-
x in z
2579-
[builtins fixtures/primitives.pyi]
2580-
2581-
[case testEnableBytearrayMemoryviewPromotionStrictEquality]
2582-
# flags: --strict-equality
2583-
def f(x: bytes, y: bytearray, z: memoryview) -> None:
2584-
x == y
2585-
y == z
2586-
x == z
2587-
97 in x
2588-
97 in y
2589-
97 in z
2590-
x in y
2591-
x in z
2592-
[builtins fixtures/primitives.pyi]
2593-
25942545
[case testStrictBytes]
25952546
# flags: --strict-bytes
25962547
def f(x: bytes) -> None: ...
@@ -2605,23 +2556,6 @@ f(bytearray(b"asdf"))
26052556
f(memoryview(b"asdf"))
26062557
[builtins fixtures/primitives.pyi]
26072558

2608-
[case testStrictBytesDisabledByDefault]
2609-
# TODO: probably change this default in Mypy v2.0, with https://github.com/python/mypy/pull/18371
2610-
# (this would also obsolete the testStrictBytesEnabledByStrict test, below)
2611-
def f(x: bytes) -> None: ...
2612-
f(bytearray(b"asdf"))
2613-
f(memoryview(b"asdf"))
2614-
[builtins fixtures/primitives.pyi]
2615-
2616-
[case testStrictBytesEnabledByStrict]
2617-
# flags: --strict --disable-error-code type-arg
2618-
# The type-arg thing is just work around the primitives.pyi isinstance Tuple not having type parameters,
2619-
# which isn't important for this.
2620-
def f(x: bytes) -> None: ...
2621-
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
2622-
f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
2623-
[builtins fixtures/primitives.pyi]
2624-
26252559
[case testNoCrashFollowImportsForStubs]
26262560
# flags: --config-file tmp/mypy.ini
26272561
{**{"x": "y"}}

test-data/unit/check-type-promotion.test

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,75 @@ f(1)
2222
[builtins fixtures/primitives.pyi]
2323

2424
[case testPromoteBytearrayToByte]
25+
# flags: --no-strict-bytes
2526
def f(x: bytes) -> None: pass
2627
f(bytearray(b''))
2728
[builtins fixtures/primitives.pyi]
2829

2930
[case testPromoteMemoryviewToBytes]
31+
# flags: --no-strict-bytes
3032
def f(x: bytes) -> None: pass
3133
f(memoryview(b''))
3234
[builtins fixtures/primitives.pyi]
3335

36+
[case testDisableBytearrayMemoryviewPromotion]
37+
# flags: --strict-bytes --strict-equality --warn-unreachable
38+
def f(x: bytes) -> None: ...
39+
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
40+
f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
41+
ba = bytearray(b"")
42+
if ba == b"":
43+
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
44+
if b"" == ba:
45+
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
46+
if ba == bytes():
47+
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
48+
if bytes() == ba:
49+
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
50+
[builtins fixtures/primitives.pyi]
51+
52+
[case testEnableBytearrayMemoryviewPromotion]
53+
# flags: --no-strict-bytes --strict-equality --warn-unreachable
54+
def f(x: bytes) -> None: ...
55+
f(bytearray(b"asdf"))
56+
f(memoryview(b"asdf"))
57+
ba = bytearray(b"")
58+
if ba == b"":
59+
f(ba)
60+
if b"" == ba:
61+
f(ba)
62+
if ba == bytes():
63+
f(ba)
64+
if bytes() == ba:
65+
f(ba)
66+
[builtins fixtures/primitives.pyi]
67+
68+
[case testDisableBytearrayMemoryviewPromotionStrictEquality]
69+
# flags: --strict-equality --strict-bytes
70+
def f(x: bytes, y: bytearray, z: memoryview) -> None:
71+
x == y
72+
y == z
73+
x == z
74+
97 in x
75+
97 in y
76+
97 in z
77+
x in y
78+
x in z
79+
[builtins fixtures/primitives.pyi]
80+
81+
[case testEnableBytearrayMemoryviewPromotionStrictEquality]
82+
# flags: --strict-equality --no-strict-bytes
83+
def f(x: bytes, y: bytearray, z: memoryview) -> None:
84+
x == y
85+
y == z
86+
x == z
87+
97 in x
88+
97 in y
89+
97 in z
90+
x in y
91+
x in z
92+
[builtins fixtures/primitives.pyi]
93+
3494
[case testNarrowingDownFromPromoteTargetType]
3595
y = 0.0
3696
y = 1

0 commit comments

Comments
 (0)