Skip to content

Remove list.__add__ overloads. #14282

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1115,10 +1115,6 @@ class list(MutableSequence[_T]):
@overload
def __setitem__(self, key: slice, value: Iterable[_T], /) -> None: ...
def __delitem__(self, key: SupportsIndex | slice, /) -> None: ...
# Overloading looks unnecessary, but is needed to work around complex mypy problems
@overload
def __add__(self, value: list[_T], /) -> list[_T]: ...
@overload
def __add__(self, value: list[_S], /) -> list[_S | _T]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe something like this could work?

Suggested change
def __add__(self, value: list[_S], /) -> list[_S | _T]: ...
def __add__(self: list[_S], value: list[_S], /) -> list[_S]: ...

Copy link
Contributor Author

@randolf-scholz randolf-scholz Jun 26, 2025

Choose a reason for hiding this comment

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

With this, wouldn't we get list[str] + list[int] = list[object] when checking with mypy, rather then list[str | int]?

I'd prefer python/mypy#19304 be fixed; then I think we would see most mypy-primer issues here go away. If you have seen this before and know and easier way to reproduce, please comment it.

Copy link
Contributor

Choose a reason for hiding this comment

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

With this, wouldn't we get list[str] + list[int] = list[object] when checking with mypy, rather then list[str | int]?

With this, wouldn't we get list[str] + list[int] = list[object] when checking with mypy, rather then list[str | int]?

I'd prefer python/mypy#19304 be fixed; then I think we would see most mypy-primer issues here go away. If you have seen this before and know and easier way to reproduce, please comment it.

That's what I indeed expect here, given mypy's join vs union behavior. But if that's how they decide to behave, then that's up to them, afaik. At least it won't be rejected this way (is the hope)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, my goal with this PR was to reduce type-checker divergence, not to cement it.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough

def __iadd__(self, value: Iterable[_T], /) -> Self: ... # type: ignore[misc]
def __mul__(self, value: SupportsIndex, /) -> list[_T]: ...
Expand Down