diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 2466dbaf619a..af4540eda05c 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -2103,8 +2103,9 @@ def dangerous_comparison(self, left: Type, right: Type, right = remove_optional(right) if (original_container and has_bytes_component(original_container) and has_bytes_component(left)): - # We need to special case bytes, because both 97 in b'abc' and b'a' in b'abc' - # return True (and we want to show the error only if the check can _never_ be True). + # We need to special case bytes and bytearray, because 97 in b'abc', b'a' in b'abc', + # b'a' in bytearray(b'abc') etc. all return True (and we want to show the error only + # if the check can _never_ be True). return False if isinstance(left, Instance) and isinstance(right, Instance): # Special case some builtin implementations of AbstractSet. @@ -4136,11 +4137,12 @@ def custom_equality_method(typ: Type) -> bool: def has_bytes_component(typ: Type) -> bool: - """Is this the builtin bytes type, or a union that contains it?""" + """Is this one of builtin byte types, or a union that contains it?""" typ = get_proper_type(typ) if isinstance(typ, UnionType): return any(has_bytes_component(t) for t in typ.items) - if isinstance(typ, Instance) and typ.type.fullname() == 'builtins.bytes': + if isinstance(typ, Instance) and typ.type.fullname() in {'builtins.bytes', + 'builtins.bytearray'}: return True return False diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 234837091bf9..4a15a0778be5 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -2059,6 +2059,13 @@ x in b'abc' [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] +[case testStrictEqualityByteArraySpecial] +# flags: --strict-equality +b'abc' in bytearray(b'abcde') +bytearray(b'abc') in b'abcde' # OK on Python 3 +[builtins fixtures/primitives.pyi] +[typing fixtures/typing-full.pyi] + [case testStrictEqualityNoPromotePy3] # flags: --strict-equality 'a' == b'a' # E: Non-overlapping equality check (left operand type: "Literal['a']", right operand type: "Literal[b'a']") diff --git a/test-data/unit/check-type-promotion.test b/test-data/unit/check-type-promotion.test index ccbf7308046f..b0ee0a2ec9d8 100644 --- a/test-data/unit/check-type-promotion.test +++ b/test-data/unit/check-type-promotion.test @@ -23,7 +23,7 @@ f(1) [case testPromoteBytearrayToByte] def f(x: bytes) -> None: pass -f(bytearray()) +f(bytearray(b'')) [builtins fixtures/primitives.pyi] [case testNarrowingDownFromPromoteTargetType] diff --git a/test-data/unit/fixtures/primitives.pyi b/test-data/unit/fixtures/primitives.pyi index 07f1daf14193..460ab4f17122 100644 --- a/test-data/unit/fixtures/primitives.pyi +++ b/test-data/unit/fixtures/primitives.pyi @@ -29,7 +29,11 @@ class bytes(Sequence[int]): def __iter__(self) -> Iterator[int]: pass def __contains__(self, other: object) -> bool: pass def __getitem__(self, item: int) -> int: pass -class bytearray: pass +class bytearray(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 function: pass class ellipsis: pass pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy