From 2ff812f5a72c475ce3d25a78a7e053b9b3cd730b Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 27 Oct 2022 13:47:56 -0700 Subject: [PATCH 1/5] Add options to disable bytes promotion It might be useful to run mypy_primer without promotions in typeshed. This would give us more confidence in changes stemming from https://github.com/python/typeshed/issues/9001 --- mypy/main.py | 6 ++++++ mypy/options.py | 16 +++++++++++++--- mypy/semanal_classprop.py | 10 ++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 360a8ed1df17..40aaa110ae1f 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -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( diff --git a/mypy/options.py b/mypy/options.py index b89ad97708c1..efe9c8d63439 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -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" @@ -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: diff --git a/mypy/semanal_classprop.py b/mypy/semanal_classprop.py index b5a702592144..27828e34a744 100644 --- a/mypy/semanal_classprop.py +++ b/mypy/semanal_classprop.py @@ -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 From 8aab1af87e6f9a2b307768d06849841f3a5c15d6 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 27 Oct 2022 14:03:30 -0700 Subject: [PATCH 2/5] remove underscore --- mypy/main.py | 4 ++-- mypy/options.py | 8 ++++---- mypy/semanal_classprop.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 40aaa110ae1f..405596c20991 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1122,10 +1122,10 @@ def add_invertible_flag( "--enable-incomplete-features", action="store_true", help=argparse.SUPPRESS ) parser.add_argument( - "--_disable-bytearray-promotion", action="store_true", help=argparse.SUPPRESS + "--disable-bytearray-promotion", action="store_true", help=argparse.SUPPRESS ) parser.add_argument( - "--_disable-memoryview-promotion", action="store_true", help=argparse.SUPPRESS + "--disable-memoryview-promotion", action="store_true", help=argparse.SUPPRESS ) # options specifying code to check diff --git a/mypy/options.py b/mypy/options.py index efe9c8d63439..3a08ff9455ee 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -62,8 +62,8 @@ class BuildType: "platform", "bazel", "plugins", - "_disable_bytearray_promotion", - "_disable_memoryview_promotion", + "disable_bytearray_promotion", + "disable_memoryview_promotion", } ) - {"debug_cache"} @@ -336,8 +336,8 @@ 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 + self.disable_bytearray_promotion = False + self.disable_memoryview_promotion = False # To avoid breaking plugin compatibility, keep providing new_semantic_analyzer @property diff --git a/mypy/semanal_classprop.py b/mypy/semanal_classprop.py index 27828e34a744..e61a22b71b26 100644 --- a/mypy/semanal_classprop.py +++ b/mypy/semanal_classprop.py @@ -167,12 +167,12 @@ def add_type_promotion( target_sym = module_names.get(TYPE_PROMOTIONS[defn.fullname]) if ( defn.fullname == "builtins.bytearray" - and options._disable_bytearray_promotion + and options.disable_bytearray_promotion ): target_sym = None elif ( defn.fullname == "builtins.memoryview" - and options._disable_memoryview_promotion + and options.disable_memoryview_promotion ): target_sym = None # With test stubs, the target may not exist. From 5147c09ca36e13b60935c96e1a4305ba69c89a51 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 27 Oct 2022 14:05:08 -0700 Subject: [PATCH 3/5] add test --- test-data/unit/check-flags.test | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 03c2d1f38b82..8f02198df213 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2128,3 +2128,16 @@ 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" +[builtins fixtures/primitives.pyi] + +[case testDisableMemoryviewPromotion] +# flags: --disable-memoryview-promotion +def f(x: bytes) -> None: ... +f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes" +[builtins fixtures/primitives.pyi] From b78484443cabd9e831bc5210353ded0c5f8124c7 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 27 Oct 2022 14:16:28 -0700 Subject: [PATCH 4/5] expand test --- test-data/unit/check-flags.test | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 8f02198df213..5a075dd6efef 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2134,10 +2134,12 @@ Ts = TypeVarTuple("Ts") # OK # 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] From 714fc027c16e09667accb15cb5cc3c7da2de2269 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 27 Oct 2022 14:25:59 -0700 Subject: [PATCH 5/5] LINT --- mypy/semanal_classprop.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mypy/semanal_classprop.py b/mypy/semanal_classprop.py index e61a22b71b26..5d21babcc597 100644 --- a/mypy/semanal_classprop.py +++ b/mypy/semanal_classprop.py @@ -165,15 +165,9 @@ 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 - ): + if defn.fullname == "builtins.bytearray" and options.disable_bytearray_promotion: target_sym = None - elif ( - defn.fullname == "builtins.memoryview" - and options.disable_memoryview_promotion - ): + elif defn.fullname == "builtins.memoryview" and options.disable_memoryview_promotion: target_sym = None # With test stubs, the target may not exist. if target_sym: