python / python/mypy

__add__ and __iadd__ are incompatible for list

Open
#9,279 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Hi everyone,

The current typeshed for list.__add__ is the following:

class list:
    def __add__(self, x: List[_T]) -> List[_T]: ...
    def __iadd__(self: _S, x: Iterable[_T]) -> _S: ...

I am trying to change this to:

class list:
    def __add__(self, x: List[_S]) -> List[Union[_T, _S]]: ...
    # __iadd__ unchanged

Since __add__ creates a copy, it is safe to concatenate lists of different types.

However, I am getting the following mypy error when running typeshed tests:

stdlib/2and3/builtins.pyi:984: error: Signatures of "__iadd__" and "__add__" are incompatible

Is this a bug in mypy? That seems perfectly safe to me.

Thanks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stdlib/2and3/builtins.pyi and reproduce the typeshed test error for the proposed add and iadd signatures. Trace the signature-compatibility check that reports the error, then determine whether the behavior is a mypy bug; done means the compatibility test reflects the intended result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.