Skip to content
Merged
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
16 changes: 9 additions & 7 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,11 @@ of the above sections.
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>`
only works in combination with :option:`--strict-equality <mypy --strict-equality>`.

.. option:: --strict-bytes
.. option:: --no-strict-bytes

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

.. code-block:: python

Expand All @@ -777,10 +777,12 @@ of the above sections.
with open("binary_file", "wb") as fp:
fp.write(buf)

f(bytearray(b"")) # error: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
f(memoryview(b"")) # error: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
# Using --no-strict-bytes disables the following errors
f(bytearray(b"")) # Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
f(memoryview(b"")) # Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"

# If `f` accepts any object that implements the buffer protocol, consider using:
# If `f` accepts any object that implements the buffer protocol,
# consider using Buffer instead:
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier

def f(buf: Buffer) -> None:
Expand Down
6 changes: 3 additions & 3 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -867,10 +867,10 @@ section of the command line docs.
.. confval:: strict_bytes

:type: boolean
:default: False
:default: True

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

.. confval:: strict

Expand Down
8 changes: 5 additions & 3 deletions docs/source/duck_type_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ supported for a small set of built-in types:

* ``int`` is duck type compatible with ``float`` and ``complex``.
* ``float`` is duck type compatible with ``complex``.
* ``bytearray`` and ``memoryview`` are duck type compatible with ``bytes``.
(this will be disabled by default in **mypy 2.0**, and currently can be
disabled with :option:`--strict-bytes <mypy --strict-bytes>`.)

.. note::
``bytearray`` and ``memoryview`` were duck type compatible with ``bytes``
by default prior to mypy 2.0. This can still be enabled with
:option:`--no-strict-bytes <mypy --no-strict-bytes>`.

For example, mypy considers an ``int`` object to be valid whenever a
``float`` object is expected. Thus code like this is nice and clean
Expand Down
2 changes: 1 addition & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ def add_invertible_flag(
"--no-strict-bytes",
default=True,
dest="strict_bytes",
help="Disable treating bytearray and memoryview as subtypes of bytes",
help="Treat bytearray and memoryview as subtypes of bytes",
group=strictness_group,
)

Expand Down
3 changes: 2 additions & 1 deletion mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ def __init__(self) -> None:
# Extend the logic of `strict_equality` to comparisons with `None`.
self.strict_equality_for_none = False

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

# Deprecated, use extra_checks instead.
Expand Down
Loading