Add `--strict-bytes` flag by hamdanal · Pull Request #18263 · python/mypy · GitHub
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
29 changes: 29 additions & 0 deletions docs/source/command_line.rst
8 changes: 8 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ section of the command line docs.
Prohibit equality checks, identity checks, and container checks between
non-overlapping types.

.. confval:: strict_bytes

:type: boolean
:default: False

Disable treating ``bytearray`` and ``memoryview`` as subtypes of ``bytes``.
This will be enabled by default in *mypy 2.0*.

.. confval:: strict

:type: boolean
Expand Down
13 changes: 13 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,14 @@ def add_invertible_flag(
group=strictness_group,
)

add_invertible_flag(
"--strict-bytes",
default=False,
strict_flag=False,
help="Disable treating bytearray and memoryview as subtypes of bytes",
group=strictness_group,
)

add_invertible_flag(
"--extra-checks",
default=False,
Expand Down Expand Up @@ -1386,6 +1394,11 @@ 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

# An explicitly specified cache_fine_grained implies local_partial_types
# (because otherwise the cache is not compatible with dmypy)
if options.cache_fine_grained:
Expand Down
4 changes: 4 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class BuildType:
"plugins",
"disable_bytearray_promotion",
"disable_memoryview_promotion",
"strict_bytes",
}
) - {"debug_cache"}

Expand Down Expand Up @@ -215,6 +216,9 @@ def __init__(self) -> None:
# This makes 1 == '1', 1 in ['1'], and 1 is '1' errors.
self.strict_equality = False

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

# Deprecated, use extra_checks instead.
self.strict_concatenate = False

Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-flags.test