From 580f3d7313a5b53ebf7f41aa0a0472e476ff6823 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 15 Oct 2019 13:08:35 +0200 Subject: [PATCH] Promote memoryview to bytes Fixes --- mypy/semanal_classprop.py | 2 ++ test-data/unit/check-type-promotion.test | 5 +++++ test-data/unit/fixtures/primitives.pyi | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/mypy/semanal_classprop.py b/mypy/semanal_classprop.py index 8f8ccd91fc6b5..c9772276d7d8a 100644 --- a/mypy/semanal_classprop.py +++ b/mypy/semanal_classprop.py @@ -30,6 +30,7 @@ TYPE_PROMOTIONS_PYTHON3 = TYPE_PROMOTIONS.copy() # type: Final TYPE_PROMOTIONS_PYTHON3.update({ 'builtins.bytearray': 'bytes', + 'builtins.memoryview': 'bytes', }) # Hard coded type promotions for Python 2. @@ -41,6 +42,7 @@ TYPE_PROMOTIONS_PYTHON2.update({ 'builtins.str': 'unicode', 'builtins.bytearray': 'str', + 'builtins.memoryview': 'str', }) diff --git a/test-data/unit/check-type-promotion.test b/test-data/unit/check-type-promotion.test index b0ee0a2ec9d8a..f477a9f2b390e 100644 --- a/test-data/unit/check-type-promotion.test +++ b/test-data/unit/check-type-promotion.test @@ -26,6 +26,11 @@ def f(x: bytes) -> None: pass f(bytearray(b'')) [builtins fixtures/primitives.pyi] +[case testPromoteMemoryviewToBytes] +def f(x: bytes) -> None: pass +f(memoryview(b'')) +[builtins fixtures/primitives.pyi] + [case testNarrowingDownFromPromoteTargetType] y = 0.0 y = 1 diff --git a/test-data/unit/fixtures/primitives.pyi b/test-data/unit/fixtures/primitives.pyi index 6043c6210d5ac..5e3b7ab32f7a5 100644 --- a/test-data/unit/fixtures/primitives.pyi +++ b/test-data/unit/fixtures/primitives.pyi @@ -35,6 +35,11 @@ class bytearray(Sequence[int]): def __iter__(self) -> Iterator[int]: pass def __contains__(self, other: object) -> bool: pass def __getitem__(self, item: int) -> int: pass +class memoryview(Sequence[int]): + def __init__(self, x: bytes) -> None: pass + def __iter__(self) -> Iterator[int]: pass + def __contains__(self, other: object) -> bool: pass + def __getitem__(self, item: int) -> int: pass class tuple(Generic[T]): pass class list(Sequence[T]): def __iter__(self) -> Iterator[T]: pass