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

Add hidden options to disable bytes promotion #13952

Merged
merged 5 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions mypy/main.py
Expand Up @@ -1121,6 +1121,12 @@ def add_invertible_flag(
parser.add_argument(
"--enable-incomplete-features", action="store_true", help=argparse.SUPPRESS
)
parser.add_argument(
"--disable-bytearray-promotion", action="store_true", help=argparse.SUPPRESS
)
parser.add_argument(
"--disable-memoryview-promotion", action="store_true", help=argparse.SUPPRESS
)

# options specifying code to check
code_group = parser.add_argument_group(
Expand Down
16 changes: 13 additions & 3 deletions mypy/options.py
Expand Up @@ -56,9 +56,16 @@ class BuildType:
"warn_unused_ignores",
}

OPTIONS_AFFECTING_CACHE: Final = (PER_MODULE_OPTIONS | {"platform", "bazel", "plugins"}) - {
"debug_cache"
}
OPTIONS_AFFECTING_CACHE: Final = (
PER_MODULE_OPTIONS
| {
"platform",
"bazel",
"plugins",
"disable_bytearray_promotion",
"disable_memoryview_promotion",
}
) - {"debug_cache"}

# Features that are currently incomplete/experimental
TYPE_VAR_TUPLE: Final = "TypeVarTuple"
Expand Down Expand Up @@ -329,6 +336,9 @@ def __init__(self) -> None:
# Deprecated reverse version of the above, do not use.
self.enable_recursive_aliases = False

self.disable_bytearray_promotion = False
self.disable_memoryview_promotion = False

# To avoid breaking plugin compatibility, keep providing new_semantic_analyzer
@property
def new_semantic_analyzer(self) -> bool:
Expand Down
10 changes: 10 additions & 0 deletions mypy/semanal_classprop.py
Expand Up @@ -165,6 +165,16 @@ def add_type_promotion(
if not promote_targets:
if defn.fullname in TYPE_PROMOTIONS:
target_sym = module_names.get(TYPE_PROMOTIONS[defn.fullname])
if (
defn.fullname == "builtins.bytearray"
and options.disable_bytearray_promotion
):
target_sym = None
elif (
defn.fullname == "builtins.memoryview"
and options.disable_memoryview_promotion
):
target_sym = None
# With test stubs, the target may not exist.
if target_sym:
target_info = target_sym.node
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-flags.test
Expand Up @@ -2128,3 +2128,18 @@ Ts = TypeVarTuple("Ts") # E: "TypeVarTuple" support is experimental, use --enab
from typing_extensions import TypeVarTuple
Ts = TypeVarTuple("Ts") # OK
[builtins fixtures/tuple.pyi]


[case testDisableBytearrayPromotion]
# flags: --disable-bytearray-promotion
def f(x: bytes) -> None: ...
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
f(memoryview(b"asdf"))
[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]