Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable strict_bytes by default #18371

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,9 @@ def add_invertible_flag(
)

add_invertible_flag(
"--strict-bytes",
default=False,
strict_flag=False,
"--no-strict-bytes",
default=True,
dest="strict_bytes",
help="Disable treating bytearray and memoryview as subtypes of bytes",
group=strictness_group,
)
Expand Down Expand Up @@ -1405,9 +1405,9 @@ 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
if not options.strict_bytes:
options.disable_bytearray_promotion = False
options.disable_memoryview_promotion = False

# An explicitly specified cache_fine_grained implies local_partial_types
# (because otherwise the cache is not compatible with dmypy)
Expand Down
6 changes: 3 additions & 3 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(self) -> None:
self.strict_equality = False

# Disable treating bytearray and memoryview as subtypes of bytes
self.strict_bytes = False
self.strict_bytes = True

# Deprecated, use extra_checks instead.
self.strict_concatenate = False
Expand Down Expand Up @@ -386,8 +386,8 @@ def __init__(self) -> None:
# (undocumented feature).
self.export_ref_info = False

self.disable_bytearray_promotion = False
self.disable_memoryview_promotion = False
self.disable_bytearray_promotion = True
self.disable_memoryview_promotion = True
self.force_uppercase_builtins = False
self.force_union_syntax = False

Expand Down
4 changes: 2 additions & 2 deletions mypy/test/testargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from mypy.main import infer_python_executable, process_options
from mypy.options import Options
from mypy.test.helpers import Suite, assert_equal
from mypy.test.helpers import Suite


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

def test_executable_inference(self) -> None:
"""Test the --python-executable flag with --python-version"""
Expand Down
8 changes: 6 additions & 2 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class bytes:
def __init__(self) -> None: ...
@overload
def __init__(self, x: object) -> None: ...
def __add__(self, x: bytes) -> bytes: ...
def __add__(self, x: bytes | bytearray) -> bytes: ...
def __mul__(self, x: int) -> bytes: ...
def __rmul__(self, x: int) -> bytes: ...
def __eq__(self, x: object) -> bool: ...
Expand All @@ -168,10 +168,14 @@ def __init__(self) -> None: pass
def __init__(self, x: object) -> None: pass
@overload
def __init__(self, string: str, encoding: str, err: str = ...) -> None: pass
def __add__(self, s: bytes) -> bytearray: ...
def __add__(self, s: bytes | bytearray) -> bytearray: ...
def __setitem__(self, i: int, o: int) -> None: ...
@overload
def __getitem__(self, i: int) -> int: ...
@overload
def __getitem__(self, i: slice) -> bytes: ...
def decode(self, x: str = ..., y: str = ...) -> str: ...
def join(self, x: Iterable[object]) -> bytes: ...

class bool(int):
def __init__(self, o: object = ...) -> None: ...
Expand Down
20 changes: 10 additions & 10 deletions mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def test_concat() -> None:
assert type(b1) == bytes
assert type(b2) == bytes
assert type(b3) == bytes
brr1: bytes = bytearray(3)
brr2: bytes = bytearray(range(5))
brr1: bytes | bytearray = bytearray(3)
brr2: bytes | bytearray = bytearray(range(5))
b4 = b1 + brr1
assert b4 == b'123\x00\x00\x00'
assert type(brr1) == bytearray
Expand Down Expand Up @@ -171,23 +171,23 @@ def test_bytes_slicing() -> None:
from typing import Any

def test_basics() -> None:
brr1: bytes = bytearray(3)
brr1: bytes | bytearray = bytearray(3)
assert brr1 == bytearray(b'\x00\x00\x00')
assert brr1 == b'\x00\x00\x00'
l = [10, 20, 30, 40]
brr2: bytes = bytearray(l)
brr2: bytes | bytearray = bytearray(l)
assert brr2 == bytearray(b'\n\x14\x1e(')
assert brr2 == b'\n\x14\x1e('
brr3: bytes = bytearray(range(5))
brr3: bytes | bytearray = bytearray(range(5))
assert brr3 == bytearray(b'\x00\x01\x02\x03\x04')
assert brr3 == b'\x00\x01\x02\x03\x04'
brr4: bytes = bytearray('string', 'utf-8')
brr4: bytes | bytearray = bytearray('string', 'utf-8')
assert brr4 == bytearray(b'string')
assert brr4 == b'string'
assert len(brr1) == 3
assert len(brr2) == 4

def f(b: bytes) -> bool:
def f(b: bytes | bytearray) -> bool:
return True

def test_bytearray_passed_into_bytes() -> None:
Expand All @@ -197,7 +197,7 @@ def test_bytearray_passed_into_bytes() -> None:

[case testBytearraySlicing]
def test_bytearray_slicing() -> None:
b: bytes = bytearray(b'abcdefg')
b: bytes | bytearray = bytearray(b'abcdefg')
zero = int()
ten = 10 + zero
two = 2 + zero
Expand Down Expand Up @@ -231,7 +231,7 @@ def test_bytearray_slicing() -> None:
from testutil import assertRaises

def test_bytearray_indexing() -> None:
b: bytes = bytearray(b'\xae\x80\xfe\x15')
b: bytes | bytearray = bytearray(b'\xae\x80\xfe\x15')
assert b[0] == 174
assert b[1] == 128
assert b[2] == 254
Expand Down Expand Up @@ -260,7 +260,7 @@ def test_bytes_join() -> None:
assert b' '.join([b'a', b'b']) == b'a b'
assert b' '.join([]) == b''

x: bytes = bytearray(b' ')
x: bytes | bytearray = bytearray(b' ')
assert x.join([b'a', b'b']) == b'a b'
assert type(x.join([b'a', b'b'])) == bytearray

Expand Down
19 changes: 3 additions & 16 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@ x: int = "" # E: Incompatible types in assignment (expression has type "str", v
# flags: --disable-bytearray-promotion --strict-equality
def f(x: bytes) -> None: ...
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
f(memoryview(b"asdf"))

ba = bytearray(b"")
if ba == b"":
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
Expand All @@ -2363,25 +2363,12 @@ if bytes() == ba:
[case testDisableMemoryviewPromotion]
# flags: --disable-memoryview-promotion
def f(x: bytes) -> None: ...
f(bytearray(b"asdf"))

f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
[builtins fixtures/primitives.pyi]

[case testDisableBytearrayMemoryviewPromotionStrictEquality]
# flags: --disable-bytearray-promotion --disable-memoryview-promotion --strict-equality
def f(x: bytes, y: bytearray, z: memoryview) -> None:
x == y
y == z
x == z
97 in x
97 in y
97 in z
x in y
x in z
[builtins fixtures/primitives.pyi]

[case testEnableBytearrayMemoryviewPromotionStrictEquality]
# flags: --strict-equality
# flags: --strict-equality --strict-bytes
def f(x: bytes, y: bytearray, z: memoryview) -> None:
x == y
y == z
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-type-promotion.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ f(1)
[builtins fixtures/primitives.pyi]

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

[case testPromoteMemoryviewToBytes]
# flags: --no-strict-bytes
def f(x: bytes) -> None: pass
f(memoryview(b''))
[builtins fixtures/primitives.pyi]
Expand Down
Loading