diff --git a/mypy/main.py b/mypy/main.py index 926e72515d95..503d9c110532 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -930,9 +930,9 @@ def add_invertible_flag( ) add_invertible_flag( - "--strict-bytes", - default=False, - strict_flag=True, + "--no-strict-bytes", + default=True, + dest="strict_bytes", help="Disable treating bytearray and memoryview as subtypes of bytes", group=strictness_group, ) diff --git a/mypy/options.py b/mypy/options.py index 9bfbc5f68af8..4af07d6cb049 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -236,7 +236,7 @@ def __init__(self) -> None: self.strict_equality_for_none = 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 @@ -407,8 +407,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 # Sets custom output format self.output: str | None = None @@ -471,9 +471,9 @@ def process_strict_bytes(self) -> None: # backwards compatibility self.disable_bytearray_promotion = True self.disable_memoryview_promotion = True - elif self.disable_bytearray_promotion and self.disable_memoryview_promotion: - # forwards compatibility - self.strict_bytes = True + else: + self.disable_bytearray_promotion = False + self.disable_memoryview_promotion = False def apply_changes(self, changes: dict[str, object]) -> Options: # Note: effects of this method *must* be idempotent. diff --git a/mypy/test/testargs.py b/mypy/test/testargs.py index 7af9981e6d34..767b36fbedd7 100644 --- a/mypy/test/testargs.py +++ b/mypy/test/testargs.py @@ -13,7 +13,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): @@ -22,7 +22,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""" diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 4f8a29672911..a444cba60a66 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -199,6 +199,7 @@ def __getitem__(self, i: int) -> int: ... @overload def __getitem__(self, i: slice) -> bytearray: ... def decode(self, x: str = ..., y: str = ...) -> str: ... + def join(self, x: Iterable[object]) -> bytes: ... def startswith(self, t: bytes) -> bool: ... def endswith(self, t: bytes) -> bool: ... diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 94aae547882f..764bb0bc275b 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2542,55 +2542,6 @@ x: int = "" # E: Incompatible types in assignment (expression has type "str", v # flags: --hide-error-codes x: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int") -[case testDisableBytearrayPromotion] -# flags: --disable-bytearray-promotion --strict-equality --warn-unreachable -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" -if b"" == ba: - f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" -if ba == bytes(): - f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" -if bytes() == ba: - f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" -[builtins fixtures/primitives.pyi] - -[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 -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 testStrictBytes] # flags: --strict-bytes def f(x: bytes) -> None: ... @@ -2605,23 +2556,6 @@ 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 --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" -[builtins fixtures/primitives.pyi] - [case testNoCrashFollowImportsForStubs] # flags: --config-file tmp/mypy.ini {**{"x": "y"}} diff --git a/test-data/unit/check-type-promotion.test b/test-data/unit/check-type-promotion.test index 7f5e14c01c1a..2dbfe253c2ea 100644 --- a/test-data/unit/check-type-promotion.test +++ b/test-data/unit/check-type-promotion.test @@ -22,15 +22,75 @@ 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] +[case testDisableBytearrayMemoryviewPromotion] +# flags: --strict-bytes --strict-equality --warn-unreachable +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" +ba = bytearray(b"") +if ba == b"": + f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" +if b"" == ba: + f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" +if ba == bytes(): + f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" +if bytes() == ba: + f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" +[builtins fixtures/primitives.pyi] + +[case testEnableBytearrayMemoryviewPromotion] +# flags: --no-strict-bytes --strict-equality --warn-unreachable +def f(x: bytes) -> None: ... +f(bytearray(b"asdf")) +f(memoryview(b"asdf")) +ba = bytearray(b"") +if ba == b"": + f(ba) +if b"" == ba: + f(ba) +if ba == bytes(): + f(ba) +if bytes() == ba: + f(ba) +[builtins fixtures/primitives.pyi] + +[case testDisableBytearrayMemoryviewPromotionStrictEquality] +# flags: --strict-equality --strict-bytes +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 --no-strict-bytes +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 testNarrowingDownFromPromoteTargetType] y = 0.0 y = 1