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