From 518b6a3ad8f16602dc3f9ecfb0462a0c702b45ef Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 7 May 2019 09:02:34 -0400 Subject: [PATCH 1/3] narrow signature of __contains__ for str-like classes Fixes #2937 --- stdlib/2and3/builtins.pyi | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 41df94608cf0..559127138fc1 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -385,7 +385,7 @@ else: def __ge__(self, x: unicode) -> bool: ... def __len__(self) -> int: ... - def __contains__(self, s: object) -> bool: ... + def __contains__(self, s: Union[unicode, bytes]) -> bool: ... def __iter__(self) -> Iterator[unicode]: ... def __str__(self) -> str: ... def __repr__(self) -> str: ... @@ -520,7 +520,8 @@ class str(Sequence[str], _str_base): def __add__(self, s: str) -> str: ... else: def __add__(self, s: AnyStr) -> AnyStr: ... - def __contains__(self, o: object) -> bool: ... + # Incompatible with Sequence.__contains__ + def __contains__(self, o: Union[str, Text]) -> bool: ... # type: ignore def __eq__(self, x: object) -> bool: ... def __ge__(self, x: Text) -> bool: ... def __getitem__(self, i: Union[int, slice]) -> str: ... @@ -622,7 +623,8 @@ if sys.version_info >= (3,): def __rmul__(self, n: int) -> bytes: ... if sys.version_info >= (3, 5): def __mod__(self, value: Any) -> bytes: ... - def __contains__(self, o: object) -> bool: ... + # Incompatible with Sequence.__contains__ + def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore def __eq__(self, x: object) -> bool: ... def __ne__(self, x: object) -> bool: ... def __lt__(self, x: bytes) -> bool: ... @@ -754,7 +756,8 @@ class bytearray(MutableSequence[int], ByteString): def __imul__(self, n: int) -> bytearray: ... if sys.version_info >= (3, 5): def __mod__(self, value: Any) -> bytes: ... - def __contains__(self, o: object) -> bool: ... + # Incompatible with Sequence.__contains__ + def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore def __eq__(self, x: object) -> bool: ... def __ne__(self, x: object) -> bool: ... def __lt__(self, x: bytes) -> bool: ... From 5abb6e0e22abb0182ab6ac6237768fec167ca962 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 7 May 2019 09:21:37 -0400 Subject: [PATCH 2/3] change __builtin__.pyi too --- stdlib/2/__builtin__.pyi | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 41df94608cf0..559127138fc1 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -385,7 +385,7 @@ else: def __ge__(self, x: unicode) -> bool: ... def __len__(self) -> int: ... - def __contains__(self, s: object) -> bool: ... + def __contains__(self, s: Union[unicode, bytes]) -> bool: ... def __iter__(self) -> Iterator[unicode]: ... def __str__(self) -> str: ... def __repr__(self) -> str: ... @@ -520,7 +520,8 @@ class str(Sequence[str], _str_base): def __add__(self, s: str) -> str: ... else: def __add__(self, s: AnyStr) -> AnyStr: ... - def __contains__(self, o: object) -> bool: ... + # Incompatible with Sequence.__contains__ + def __contains__(self, o: Union[str, Text]) -> bool: ... # type: ignore def __eq__(self, x: object) -> bool: ... def __ge__(self, x: Text) -> bool: ... def __getitem__(self, i: Union[int, slice]) -> str: ... @@ -622,7 +623,8 @@ if sys.version_info >= (3,): def __rmul__(self, n: int) -> bytes: ... if sys.version_info >= (3, 5): def __mod__(self, value: Any) -> bytes: ... - def __contains__(self, o: object) -> bool: ... + # Incompatible with Sequence.__contains__ + def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore def __eq__(self, x: object) -> bool: ... def __ne__(self, x: object) -> bool: ... def __lt__(self, x: bytes) -> bool: ... @@ -754,7 +756,8 @@ class bytearray(MutableSequence[int], ByteString): def __imul__(self, n: int) -> bytearray: ... if sys.version_info >= (3, 5): def __mod__(self, value: Any) -> bytes: ... - def __contains__(self, o: object) -> bool: ... + # Incompatible with Sequence.__contains__ + def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore def __eq__(self, x: object) -> bool: ... def __ne__(self, x: object) -> bool: ... def __lt__(self, x: bytes) -> bool: ... From d3fab8e5b16f7840de04ce6a06eaa46b27987b27 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 7 May 2019 09:58:12 -0400 Subject: [PATCH 3/3] add another type ignore --- stdlib/2/__builtin__.pyi | 3 ++- stdlib/2and3/builtins.pyi | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 559127138fc1..a4d6b03625f4 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -385,7 +385,8 @@ else: def __ge__(self, x: unicode) -> bool: ... def __len__(self) -> int: ... - def __contains__(self, s: Union[unicode, bytes]) -> bool: ... + # The argument type is incompatible with Sequence + def __contains__(self, s: Union[unicode, bytes]) -> bool: ... # type: ignore def __iter__(self) -> Iterator[unicode]: ... def __str__(self) -> str: ... def __repr__(self) -> str: ... diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 559127138fc1..a4d6b03625f4 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -385,7 +385,8 @@ else: def __ge__(self, x: unicode) -> bool: ... def __len__(self) -> int: ... - def __contains__(self, s: Union[unicode, bytes]) -> bool: ... + # The argument type is incompatible with Sequence + def __contains__(self, s: Union[unicode, bytes]) -> bool: ... # type: ignore def __iter__(self) -> Iterator[unicode]: ... def __str__(self) -> str: ... def __repr__(self) -> str: ...