Skip to content

Commit bdb1de5

Browse files
narrow signature of __contains__ for str-like classes (#2967)
Fixes #2937
1 parent 4990830 commit bdb1de5

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

stdlib/2/__builtin__.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ else:
385385
def __ge__(self, x: unicode) -> bool: ...
386386

387387
def __len__(self) -> int: ...
388-
def __contains__(self, s: object) -> bool: ...
388+
# The argument type is incompatible with Sequence
389+
def __contains__(self, s: Union[unicode, bytes]) -> bool: ... # type: ignore
389390
def __iter__(self) -> Iterator[unicode]: ...
390391
def __str__(self) -> str: ...
391392
def __repr__(self) -> str: ...
@@ -520,7 +521,8 @@ class str(Sequence[str], _str_base):
520521
def __add__(self, s: str) -> str: ...
521522
else:
522523
def __add__(self, s: AnyStr) -> AnyStr: ...
523-
def __contains__(self, o: object) -> bool: ...
524+
# Incompatible with Sequence.__contains__
525+
def __contains__(self, o: Union[str, Text]) -> bool: ... # type: ignore
524526
def __eq__(self, x: object) -> bool: ...
525527
def __ge__(self, x: Text) -> bool: ...
526528
def __getitem__(self, i: Union[int, slice]) -> str: ...
@@ -622,7 +624,8 @@ if sys.version_info >= (3,):
622624
def __rmul__(self, n: int) -> bytes: ...
623625
if sys.version_info >= (3, 5):
624626
def __mod__(self, value: Any) -> bytes: ...
625-
def __contains__(self, o: object) -> bool: ...
627+
# Incompatible with Sequence.__contains__
628+
def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore
626629
def __eq__(self, x: object) -> bool: ...
627630
def __ne__(self, x: object) -> bool: ...
628631
def __lt__(self, x: bytes) -> bool: ...
@@ -754,7 +757,8 @@ class bytearray(MutableSequence[int], ByteString):
754757
def __imul__(self, n: int) -> bytearray: ...
755758
if sys.version_info >= (3, 5):
756759
def __mod__(self, value: Any) -> bytes: ...
757-
def __contains__(self, o: object) -> bool: ...
760+
# Incompatible with Sequence.__contains__
761+
def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore
758762
def __eq__(self, x: object) -> bool: ...
759763
def __ne__(self, x: object) -> bool: ...
760764
def __lt__(self, x: bytes) -> bool: ...

stdlib/2and3/builtins.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ else:
385385
def __ge__(self, x: unicode) -> bool: ...
386386

387387
def __len__(self) -> int: ...
388-
def __contains__(self, s: object) -> bool: ...
388+
# The argument type is incompatible with Sequence
389+
def __contains__(self, s: Union[unicode, bytes]) -> bool: ... # type: ignore
389390
def __iter__(self) -> Iterator[unicode]: ...
390391
def __str__(self) -> str: ...
391392
def __repr__(self) -> str: ...
@@ -520,7 +521,8 @@ class str(Sequence[str], _str_base):
520521
def __add__(self, s: str) -> str: ...
521522
else:
522523
def __add__(self, s: AnyStr) -> AnyStr: ...
523-
def __contains__(self, o: object) -> bool: ...
524+
# Incompatible with Sequence.__contains__
525+
def __contains__(self, o: Union[str, Text]) -> bool: ... # type: ignore
524526
def __eq__(self, x: object) -> bool: ...
525527
def __ge__(self, x: Text) -> bool: ...
526528
def __getitem__(self, i: Union[int, slice]) -> str: ...
@@ -622,7 +624,8 @@ if sys.version_info >= (3,):
622624
def __rmul__(self, n: int) -> bytes: ...
623625
if sys.version_info >= (3, 5):
624626
def __mod__(self, value: Any) -> bytes: ...
625-
def __contains__(self, o: object) -> bool: ...
627+
# Incompatible with Sequence.__contains__
628+
def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore
626629
def __eq__(self, x: object) -> bool: ...
627630
def __ne__(self, x: object) -> bool: ...
628631
def __lt__(self, x: bytes) -> bool: ...
@@ -754,7 +757,8 @@ class bytearray(MutableSequence[int], ByteString):
754757
def __imul__(self, n: int) -> bytearray: ...
755758
if sys.version_info >= (3, 5):
756759
def __mod__(self, value: Any) -> bytes: ...
757-
def __contains__(self, o: object) -> bool: ...
760+
# Incompatible with Sequence.__contains__
761+
def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore
758762
def __eq__(self, x: object) -> bool: ...
759763
def __ne__(self, x: object) -> bool: ...
760764
def __lt__(self, x: bytes) -> bool: ...

0 commit comments

Comments
 (0)