Skip to content

Update mypy to 2.4.0 - #16502

Merged
srittau merged 20 commits into
python:mainfrom
srittau:mypy-2.4.0
Oct 8, 2026
Merged

srittau merged 20 commits into
python:mainfrom
srittau:mypy-2.4.0

Conversation

@srittau

@srittau srittau commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread stdlib/@tests/stubtest_allowlists/common.txt Outdated
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread stdlib/datetime.pyi Outdated
Comment thread stdlib/builtins.pyi
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@srittau

srittau commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author

This was more complicated than expected, but finally ready for a final review.

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment on lines +46 to +47
# The following reverse operators are present in the C implementation
# (probably incidental), but missing from the Python implementation.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Collaborator Author

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.

Comment thread stdlib/builtins.pyi
Comment on lines +1465 to +1468
@overload
def __rand__(self, value: set[_S], /) -> set[_S]: ...
@overload
def __rand__(self, value: frozenset[_S], /) -> frozenset[_S]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 set.__and__. They look incorrect -- it doesn't seem like set.__and__ really takes any abstract set for its second argument. It seems like it might also only take sets and frozensets, similar to set.__rand__:

>>> from collections.abc import Set
>>> class Foo(Set):
...     __contains__ = __iter__ = __len__ = lambda *args: ...
...
>>> {1, 2}.__and__(Foo())
NotImplemented

Possibly the same also goes for other set dunders?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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, set checks explicitly for set or frozenset (or derived classes) using PyAnySet_Check:

https://github.com/python/cpython/blob/2eb2af805c156ca52b316bb4ced5e0ab3971d0ae/Objects/setobject.c#L1876-L1889

static PyObject *
set_and(PyObject *self, PyObject *other)
{
    if (!PyAnySet_Check(self) || !PyAnySet_Check(other))
        Py_RETURN_NOTIMPLEMENTED;
    /* ... */
}

Comment thread stdlib/builtins.pyi
@@ -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]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the nonreflected frozenset dunders have the same issue:

>>> from collections.abc import Set
>>> class Foo(Set):
...     __contains__ = __iter__ = __len__ = lambda *args: ...
...
>>> frozenset({1, 2}).__and__(Foo())
NotImplemented

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, they would have.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy-stubs (https://github.com/scipy/scipy-stubs)
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

@srittau
srittau merged commit c333d5d into python:main Oct 8, 2026
127 checks passed
@srittau
srittau deleted the mypy-2.4.0 branch October 8, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants