python / python/mypy

__getnewargs__ for a subclass incompatible with return type of supertype

Open
#13,309 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Here is an example commit with the issue: https://github.com/eli-schwartz/meson/commit/4d204bb81869422a102264611d9aea314a1ac2ca

mypy.ini: https://github.com/mesonbuild/meson/blob/27626124d0bdd89f5f276f70a73b4713aa3aac3d/.mypy.ini

$ mypy --version
mypy 0.961 (compiled: yes)

$ mypy mesonbuild/interpreter/primitives/string.py
mesonbuild/interpreter/primitives/string.py:211:5: error: Return type "Tuple[str, str]" of "__getnewargs__" incompatible with return type "Tuple[str]" in supertype "str"

I subclassed str and treat it specially with its own incompatible __new__() to enforce a specific creation pattern. After discovering I need to pickle it as well, I added a __getnewargs__() which is also incompatible. mypy is happy with my incompatible new, but not with the getnewargs that needs to match the argument contract of new.

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

Reproduce the report with mypy 0.961 using mesonbuild/interpreter/primitives/string.py and the linked .mypy.ini configuration. Inspect how the subclass’s new and getnewargs signatures are checked against str, then verify the intended pickle case is handled without the reported incompatible-return diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.