Skip to content

Commit

Permalink
Add hidden options to disable bytes promotion (#13952)
Browse files Browse the repository at this point in the history
It might be useful to run mypy_primer without promotions in typeshed.
This would give us more confidence in changes stemming from
python/typeshed#9001
  • Loading branch information
hauntsaninja authored Oct 27, 2022
1 parent 507fc5c commit 2d70ac0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
6 changes: 6 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
4 changes: 4 additions & 0 deletions mypy/semanal_classprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ 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
Original file line number Diff line number Diff line change
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]

0 comments on commit 2d70ac0

Please sign in to comment.