From 77810277d5fc7e6c22d6bbb718698a28f46217de Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 18 Nov 2021 06:58:05 +0100 Subject: [PATCH] Recommend to use mypy error codes if applicable (#6305) --- CONTRIBUTING.md | 4 +++- stdlib/builtins.pyi | 24 ++++++++++++------------ tests/mypy_test.py | 1 + 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cbba0d5b2..cd13e6962 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -389,7 +389,9 @@ Some further tips for good type hints: * in Python 2, whenever possible, use `unicode` if that's the only possible type, and `Text` if it can be either `unicode` or `bytes`; * use platform checks like `if sys.platform == 'win32'` to denote - platform-dependent APIs. + platform-dependent APIs; +* use mypy error codes for mypy-specific `# type: ignore` annotations, + e.g. `# type: ignore[override]` for Liskov Substitution Principle violations. Imports in stubs are considered private (not part of the exported API) unless: diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 112da9bb1..d6fa9d22c 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -419,7 +419,7 @@ class str(Sequence[str]): def maketrans(__x: str, __y: str, __z: str | None = ...) -> dict[int, int | None]: ... def __add__(self, __s: str) -> str: ... # Incompatible with Sequence.__contains__ - def __contains__(self, __o: str) -> bool: ... # type: ignore + def __contains__(self, __o: str) -> bool: ... # type: ignore[override] def __eq__(self, __x: object) -> bool: ... def __ge__(self, __x: str) -> bool: ... def __getitem__(self, __i: int | slice) -> str: ... @@ -528,7 +528,7 @@ class bytes(ByteString): def __rmul__(self, __n: SupportsIndex) -> bytes: ... def __mod__(self, __value: Any) -> bytes: ... # Incompatible with Sequence.__contains__ - def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore + def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore[override] def __eq__(self, __x: object) -> bool: ... def __ne__(self, __x: object) -> bool: ... def __lt__(self, __x: bytes) -> bool: ... @@ -620,7 +620,7 @@ class bytearray(MutableSequence[int], ByteString): def __iter__(self) -> Iterator[int]: ... def __str__(self) -> str: ... def __repr__(self) -> str: ... - __hash__: None # type: ignore + __hash__: None # type: ignore[assignment] @overload def __getitem__(self, __i: SupportsIndex) -> int: ... @overload @@ -637,7 +637,7 @@ class bytearray(MutableSequence[int], ByteString): def __imul__(self, __n: SupportsIndex) -> bytearray: ... def __mod__(self, __value: Any) -> bytes: ... # Incompatible with Sequence.__contains__ - def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore + def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore[override] def __eq__(self, __x: object) -> bool: ... def __ne__(self, __x: object) -> bool: ... def __lt__(self, __x: bytes) -> bool: ... @@ -728,7 +728,7 @@ class slice(object): def __init__(self, __stop: Any) -> None: ... @overload def __init__(self, __start: Any, __stop: Any, __step: Any = ...) -> None: ... - __hash__: None # type: ignore + __hash__: None # type: ignore[assignment] def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ... class tuple(Sequence[_T_co], Generic[_T_co]): @@ -785,7 +785,7 @@ class list(MutableSequence[_T], Generic[_T]): def __len__(self) -> int: ... def __iter__(self) -> Iterator[_T]: ... def __str__(self) -> str: ... - __hash__: None # type: ignore + __hash__: None # type: ignore[assignment] @overload def __getitem__(self, __i: SupportsIndex) -> _T: ... @overload @@ -850,12 +850,12 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): if sys.version_info >= (3, 8): def __reversed__(self) -> Iterator[_KT]: ... def __str__(self) -> str: ... - __hash__: None # type: ignore + __hash__: None # type: ignore[assignment] if sys.version_info >= (3, 9): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... - def __ior__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ... # type: ignore + def __ior__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ... # type: ignore[misc] class set(MutableSet[_T], Generic[_T]): def __init__(self, __iterable: Iterable[_T] = ...) -> None: ... @@ -892,7 +892,7 @@ class set(MutableSet[_T], Generic[_T]): def __lt__(self, __s: AbstractSet[object]) -> bool: ... def __ge__(self, __s: AbstractSet[object]) -> bool: ... def __gt__(self, __s: AbstractSet[object]) -> bool: ... - __hash__: None # type: ignore + __hash__: None # type: ignore[assignment] if sys.version_info >= (3, 9): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... @@ -938,7 +938,7 @@ class range(Sequence[int]): @overload def __init__(self, __start: SupportsIndex, __stop: SupportsIndex, __step: SupportsIndex = ...) -> None: ... def count(self, __value: int) -> int: ... - def index(self, __value: int) -> int: ... # type: ignore + def index(self, __value: int) -> int: ... # type: ignore[override] def __len__(self) -> int: ... def __contains__(self, __o: object) -> bool: ... def __iter__(self) -> Iterator[int]: ... @@ -968,10 +968,10 @@ class property(object): def __delete__(self, __obj: Any) -> None: ... @final -class _NotImplementedType(Any): # type: ignore +class _NotImplementedType(Any): # type: ignore[misc] # A little weird, but typing the __call__ as NotImplemented makes the error message # for NotImplemented() much better - __call__: NotImplemented # type: ignore + __call__: NotImplemented # type: ignore[valid-type] NotImplemented: _NotImplementedType diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 3538b7bda..64d6c2cdf 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -207,6 +207,7 @@ def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_types "--no-implicit-optional", "--disallow-any-generics", "--warn-incomplete-stub", + "--show-error-codes", "--no-error-summary", ] if custom_typeshed: