Skip to content

Commit bf62d3e

Browse files
authored
Document that --strict-bytes is enabled by default (python#21272)
`--strict-bytes` was enabled by default in python#18371.
1 parent 15f1287 commit bf62d3e

5 files changed

Lines changed: 20 additions & 15 deletions

File tree

docs/source/command_line.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,11 @@ of the above sections.
764764
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>`
765765
only works in combination with :option:`--strict-equality <mypy --strict-equality>`.
766766

767-
.. option:: --strict-bytes
767+
.. option:: --no-strict-bytes
768768

769-
By default, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes`` which
770-
is not true at runtime. Use this flag to disable this behavior. ``--strict-bytes`` will
771-
be enabled by default in *mypy 2.0*.
769+
Treat ``bytearray`` and ``memoryview`` as subtypes of ``bytes``. This is not true
770+
at runtime and can lead to unexpected behavior. This was the default behavior prior
771+
to mypy 2.0.
772772

773773
.. code-block:: python
774774
@@ -777,10 +777,12 @@ of the above sections.
777777
with open("binary_file", "wb") as fp:
778778
fp.write(buf)
779779
780-
f(bytearray(b"")) # error: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
781-
f(memoryview(b"")) # error: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
780+
# Using --no-strict-bytes disables the following errors
781+
f(bytearray(b"")) # Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
782+
f(memoryview(b"")) # Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
782783
783-
# If `f` accepts any object that implements the buffer protocol, consider using:
784+
# If `f` accepts any object that implements the buffer protocol,
785+
# consider using Buffer instead:
784786
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier
785787
786788
def f(buf: Buffer) -> None:

docs/source/config_file.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,10 @@ section of the command line docs.
867867
.. confval:: strict_bytes
868868

869869
:type: boolean
870-
:default: False
870+
:default: True
871871

872-
Disable treating ``bytearray`` and ``memoryview`` as subtypes of ``bytes``.
873-
This will be enabled by default in *mypy 2.0*.
872+
If disabled, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes``.
873+
This has been enabled by default since mypy 2.0.
874874

875875
.. confval:: strict
876876

docs/source/duck_type_compatibility.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ supported for a small set of built-in types:
88

99
* ``int`` is duck type compatible with ``float`` and ``complex``.
1010
* ``float`` is duck type compatible with ``complex``.
11-
* ``bytearray`` and ``memoryview`` are duck type compatible with ``bytes``.
12-
(this will be disabled by default in **mypy 2.0**, and currently can be
13-
disabled with :option:`--strict-bytes <mypy --strict-bytes>`.)
11+
12+
.. note::
13+
``bytearray`` and ``memoryview`` were duck type compatible with ``bytes``
14+
by default prior to mypy 2.0. This can still be enabled with
15+
:option:`--no-strict-bytes <mypy --no-strict-bytes>`.
1416

1517
For example, mypy considers an ``int`` object to be valid whenever a
1618
``float`` object is expected. Thus code like this is nice and clean

mypy/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def add_invertible_flag(
939939
"--no-strict-bytes",
940940
default=True,
941941
dest="strict_bytes",
942-
help="Disable treating bytearray and memoryview as subtypes of bytes",
942+
help="Treat bytearray and memoryview as subtypes of bytes",
943943
group=strictness_group,
944944
)
945945

mypy/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ def __init__(self) -> None:
244244
# Extend the logic of `strict_equality` to comparisons with `None`.
245245
self.strict_equality_for_none = False
246246

247-
# Disable treating bytearray and memoryview as subtypes of bytes
247+
# If False, switch to pre-mypy-2.0 legacy behavior where bytearray and memoryview are
248+
# treated as subtypes of bytes
248249
self.strict_bytes = True
249250

250251
# Deprecated, use extra_checks instead.

0 commit comments

Comments
 (0)