Skip to content

Commit 7781027

Browse files
authored
Recommend to use mypy error codes if applicable (#6305)
1 parent ccc09aa commit 7781027

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ Some further tips for good type hints:
389389
* in Python 2, whenever possible, use `unicode` if that's the only
390390
possible type, and `Text` if it can be either `unicode` or `bytes`;
391391
* use platform checks like `if sys.platform == 'win32'` to denote
392-
platform-dependent APIs.
392+
platform-dependent APIs;
393+
* use mypy error codes for mypy-specific `# type: ignore` annotations,
394+
e.g. `# type: ignore[override]` for Liskov Substitution Principle violations.
393395

394396
Imports in stubs are considered private (not part of the exported API)
395397
unless:

stdlib/builtins.pyi

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class str(Sequence[str]):
419419
def maketrans(__x: str, __y: str, __z: str | None = ...) -> dict[int, int | None]: ...
420420
def __add__(self, __s: str) -> str: ...
421421
# Incompatible with Sequence.__contains__
422-
def __contains__(self, __o: str) -> bool: ... # type: ignore
422+
def __contains__(self, __o: str) -> bool: ... # type: ignore[override]
423423
def __eq__(self, __x: object) -> bool: ...
424424
def __ge__(self, __x: str) -> bool: ...
425425
def __getitem__(self, __i: int | slice) -> str: ...
@@ -528,7 +528,7 @@ class bytes(ByteString):
528528
def __rmul__(self, __n: SupportsIndex) -> bytes: ...
529529
def __mod__(self, __value: Any) -> bytes: ...
530530
# Incompatible with Sequence.__contains__
531-
def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore
531+
def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore[override]
532532
def __eq__(self, __x: object) -> bool: ...
533533
def __ne__(self, __x: object) -> bool: ...
534534
def __lt__(self, __x: bytes) -> bool: ...
@@ -620,7 +620,7 @@ class bytearray(MutableSequence[int], ByteString):
620620
def __iter__(self) -> Iterator[int]: ...
621621
def __str__(self) -> str: ...
622622
def __repr__(self) -> str: ...
623-
__hash__: None # type: ignore
623+
__hash__: None # type: ignore[assignment]
624624
@overload
625625
def __getitem__(self, __i: SupportsIndex) -> int: ...
626626
@overload
@@ -637,7 +637,7 @@ class bytearray(MutableSequence[int], ByteString):
637637
def __imul__(self, __n: SupportsIndex) -> bytearray: ...
638638
def __mod__(self, __value: Any) -> bytes: ...
639639
# Incompatible with Sequence.__contains__
640-
def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore
640+
def __contains__(self, __o: SupportsIndex | bytes) -> bool: ... # type: ignore[override]
641641
def __eq__(self, __x: object) -> bool: ...
642642
def __ne__(self, __x: object) -> bool: ...
643643
def __lt__(self, __x: bytes) -> bool: ...
@@ -728,7 +728,7 @@ class slice(object):
728728
def __init__(self, __stop: Any) -> None: ...
729729
@overload
730730
def __init__(self, __start: Any, __stop: Any, __step: Any = ...) -> None: ...
731-
__hash__: None # type: ignore
731+
__hash__: None # type: ignore[assignment]
732732
def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ...
733733

734734
class tuple(Sequence[_T_co], Generic[_T_co]):
@@ -785,7 +785,7 @@ class list(MutableSequence[_T], Generic[_T]):
785785
def __len__(self) -> int: ...
786786
def __iter__(self) -> Iterator[_T]: ...
787787
def __str__(self) -> str: ...
788-
__hash__: None # type: ignore
788+
__hash__: None # type: ignore[assignment]
789789
@overload
790790
def __getitem__(self, __i: SupportsIndex) -> _T: ...
791791
@overload
@@ -850,12 +850,12 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
850850
if sys.version_info >= (3, 8):
851851
def __reversed__(self) -> Iterator[_KT]: ...
852852
def __str__(self) -> str: ...
853-
__hash__: None # type: ignore
853+
__hash__: None # type: ignore[assignment]
854854
if sys.version_info >= (3, 9):
855855
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
856856
def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
857857
def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
858-
def __ior__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ... # type: ignore
858+
def __ior__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ... # type: ignore[misc]
859859

860860
class set(MutableSet[_T], Generic[_T]):
861861
def __init__(self, __iterable: Iterable[_T] = ...) -> None: ...
@@ -892,7 +892,7 @@ class set(MutableSet[_T], Generic[_T]):
892892
def __lt__(self, __s: AbstractSet[object]) -> bool: ...
893893
def __ge__(self, __s: AbstractSet[object]) -> bool: ...
894894
def __gt__(self, __s: AbstractSet[object]) -> bool: ...
895-
__hash__: None # type: ignore
895+
__hash__: None # type: ignore[assignment]
896896
if sys.version_info >= (3, 9):
897897
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
898898

@@ -938,7 +938,7 @@ class range(Sequence[int]):
938938
@overload
939939
def __init__(self, __start: SupportsIndex, __stop: SupportsIndex, __step: SupportsIndex = ...) -> None: ...
940940
def count(self, __value: int) -> int: ...
941-
def index(self, __value: int) -> int: ... # type: ignore
941+
def index(self, __value: int) -> int: ... # type: ignore[override]
942942
def __len__(self) -> int: ...
943943
def __contains__(self, __o: object) -> bool: ...
944944
def __iter__(self) -> Iterator[int]: ...
@@ -968,10 +968,10 @@ class property(object):
968968
def __delete__(self, __obj: Any) -> None: ...
969969

970970
@final
971-
class _NotImplementedType(Any): # type: ignore
971+
class _NotImplementedType(Any): # type: ignore[misc]
972972
# A little weird, but typing the __call__ as NotImplemented makes the error message
973973
# for NotImplemented() much better
974-
__call__: NotImplemented # type: ignore
974+
__call__: NotImplemented # type: ignore[valid-type]
975975

976976
NotImplemented: _NotImplementedType
977977

tests/mypy_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_types
207207
"--no-implicit-optional",
208208
"--disallow-any-generics",
209209
"--warn-incomplete-stub",
210+
"--show-error-codes",
210211
"--no-error-summary",
211212
]
212213
if custom_typeshed:

0 commit comments

Comments
 (0)