Repository navigation
Update mypy to 2.4.0 #16502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update mypy to 2.4.0 #16502
Changes from all commits
26d38e3
a76c3d7
3d46254
0baa462
76f9e63
cf4f4c8
5e6a43f
7c15377
37ed093
0a7e0b5
862354f
a9e0c18
df8d2a0
59932c4
5554338
0cd86c0
09f47fb
4546dad
6602f46
2967a57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,7 +66,7 @@ from typing import ( # noqa: Y022 # ruff: ignore[deprecated-import] | |
|
|
||
| # we can't import `Literal` from typing or mypy crashes: see #11247 | ||
| # ruff: ignore[deprecated-import] | ||
| from typing_extensions import Literal, LiteralString, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023 | ||
| from typing_extensions import Buffer, Literal, LiteralString, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023 | ||
|
|
||
| if sys.version_info >= (3, 14): | ||
| from _typeshed import AnnotateFunc | ||
|
|
@@ -711,7 +711,9 @@ class str(Sequence[str]): | |
| @overload | ||
| def __mod__(self: LiteralString, value: LiteralString | tuple[LiteralString, ...], /) -> LiteralString: ... | ||
| @overload | ||
| def __mod__(self, value: Any, /) -> str: ... | ||
| def __mod__(self, value: object, /) -> str: ... | ||
|
|
||
| def __rmod__(self, value: str, /) -> str: ... | ||
|
|
||
| @overload | ||
| def __mul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ... | ||
|
|
@@ -826,7 +828,8 @@ class bytes(Sequence[int]): | |
| def __add__(self, value: ReadableBuffer, /) -> bytes: ... | ||
| def __mul__(self, value: SupportsIndex, /) -> bytes: ... | ||
| def __rmul__(self, value: SupportsIndex, /) -> bytes: ... | ||
| def __mod__(self, value: Any, /) -> bytes: ... | ||
| def __mod__(self, value: Buffer | SupportsBytes | tuple[Buffer | SupportsBytes, ...], /) -> bytes: ... | ||
| def __rmod__(self, value: bytes, /) -> bytes: ... | ||
| # Incompatible with Sequence.__contains__ | ||
| def __contains__(self, key: SupportsIndex | ReadableBuffer, /) -> bool: ... # type: ignore[override] | ||
| def __eq__(self, value: object, /) -> bool: ... | ||
|
|
@@ -957,7 +960,8 @@ class bytearray(MutableSequence[int]): | |
| def __mul__(self, value: SupportsIndex, /) -> bytearray: ... | ||
| def __rmul__(self, value: SupportsIndex, /) -> bytearray: ... | ||
| def __imul__(self, value: SupportsIndex, /) -> Self: ... | ||
| def __mod__(self, value: Any, /) -> bytes: ... | ||
| def __mod__(self, value: Buffer | SupportsBytes | tuple[Buffer | SupportsBytes, ...], /) -> bytearray: ... | ||
| def __rmod__(self, value: bytearray, /) -> bytearray: ... | ||
| # Incompatible with Sequence.__contains__ | ||
| def __contains__(self, key: SupportsIndex | ReadableBuffer, /) -> bool: ... # type: ignore[override] | ||
| def __eq__(self, value: object, /) -> bool: ... | ||
|
|
@@ -1457,12 +1461,36 @@ class set(MutableSet[_T]): | |
| def __iter__(self) -> Iterator[_T]: ... | ||
| def __and__(self, value: AbstractSet[object], /) -> set[_T]: ... | ||
| def __iand__(self, value: AbstractSet[object], /) -> Self: ... | ||
|
|
||
| @overload | ||
| def __rand__(self, value: set[_S], /) -> set[_S]: ... | ||
| @overload | ||
| def __rand__(self, value: frozenset[_S], /) -> frozenset[_S]: ... | ||
|
Comment on lines
+1465
to
+1468
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not an issue to be fixed in this PR, but we might want to revisit the annotations for >>> from collections.abc import Set
>>> class Foo(Set):
... __contains__ = __iter__ = __len__ = lambda *args: ...
...
>>> {1, 2}.__and__(Foo())
NotImplementedPossibly the same also goes for other
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll actually fix this here, since I've also fixed a few other dunder annotations. Similar to the other builtins, static PyObject *
set_and(PyObject *self, PyObject *other)
{
if (!PyAnySet_Check(self) || !PyAnySet_Check(other))
Py_RETURN_NOTIMPLEMENTED;
/* ... */
} |
||
|
|
||
| def __or__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... | ||
| def __ior__(self, value: AbstractSet[_T], /) -> Self: ... # type: ignore[override,misc] | ||
|
|
||
| @overload | ||
| def __ror__(self, value: set[_S], /) -> set[_T | _S]: ... | ||
| @overload | ||
| def __ror__(self, value: frozenset[_S], /) -> frozenset[_T | _S]: ... | ||
|
|
||
| def __sub__(self, value: AbstractSet[object], /) -> set[_T]: ... | ||
| def __isub__(self, value: AbstractSet[object], /) -> Self: ... | ||
|
|
||
| @overload | ||
| def __rsub__(self, value: set[_S], /) -> set[_S]: ... | ||
| @overload | ||
| def __rsub__(self, value: frozenset[_S], /) -> frozenset[_S]: ... | ||
|
|
||
| def __xor__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... | ||
| def __ixor__(self, value: AbstractSet[_T], /) -> Self: ... # type: ignore[override,misc] | ||
|
|
||
| @overload | ||
| def __rxor__(self, value: set[_S], /) -> set[_T | _S]: ... | ||
| @overload | ||
| def __rxor__(self, value: frozenset[_S], /) -> frozenset[_T | _S]: ... | ||
|
|
||
| def __le__(self, value: AbstractSet[object], /) -> bool: ... | ||
| def __lt__(self, value: AbstractSet[object], /) -> bool: ... | ||
| def __ge__(self, value: AbstractSet[object], /) -> bool: ... | ||
|
|
@@ -1490,9 +1518,33 @@ class frozenset(AbstractSet[_T_co]): | |
| def __contains__(self, o: object, /) -> bool: ... | ||
| def __iter__(self) -> Iterator[_T_co]: ... | ||
| def __and__(self, value: AbstractSet[object], /) -> frozenset[_T_co]: ... | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the nonreflected >>> from collections.abc import Set
>>> class Foo(Set):
... __contains__ = __iter__ = __len__ = lambda *args: ...
...
>>> frozenset({1, 2}).__and__(Foo())
NotImplemented
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, they would have.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, I'm going to revert the last two commits for now and commit. This seems to open a whole new can of worms.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good |
||
|
|
||
| @overload | ||
| def __rand__(self, value: set[_S], /) -> set[_S]: ... | ||
| @overload | ||
| def __rand__(self, value: frozenset[_S], /) -> frozenset[_S]: ... | ||
|
AlexWaygood marked this conversation as resolved.
|
||
|
|
||
| def __or__(self, value: AbstractSet[_S], /) -> frozenset[_T_co | _S]: ... | ||
|
|
||
| @overload | ||
| def __ror__(self, value: set[_S], /) -> set[_T_co | _S]: ... | ||
| @overload | ||
| def __ror__(self, value: frozenset[_S], /) -> frozenset[_T_co | _S]: ... | ||
|
|
||
| def __sub__(self, value: AbstractSet[object], /) -> frozenset[_T_co]: ... | ||
|
|
||
| @overload | ||
| def __rsub__(self, value: set[_S], /) -> set[_S]: ... | ||
| @overload | ||
| def __rsub__(self, value: frozenset[_S], /) -> frozenset[_S]: ... | ||
|
|
||
| def __xor__(self, value: AbstractSet[_S], /) -> frozenset[_T_co | _S]: ... | ||
|
|
||
| @overload | ||
| def __rxor__(self, value: set[_S], /) -> set[_T_co | _S]: ... | ||
| @overload | ||
| def __rxor__(self, value: frozenset[_S], /) -> frozenset[_T_co | _S]: ... | ||
|
|
||
| def __le__(self, value: AbstractSet[object], /) -> bool: ... | ||
| def __lt__(self, value: AbstractSet[object], /) -> bool: ... | ||
| def __ge__(self, value: AbstractSet[object], /) -> bool: ... | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still personally lean towards just following the C implementation here, since it's the only one that will be relevant to the vast majority of type-checker users. And I think that's what we've done elsewhere in a few other places. But don't feel strongly; this is fine for now. And as you say, these aren't particularly useful, so it probably just doesn't really matter for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to support only what's supported by both versions.