python / python/mypy

Generic NamedTuples and overloads

Open
#13,902 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

mypy doesn't infer the right type, when using a generic namedtuple and overloads.

To Reproduce

https://gist.github.com/mypy-play/60399580f537f8a7c80bb18266ab357f

Expected Behavior

Revealed type is int (pyright infers the right type).

Actual Behavior

Mypy infers type "< nothing >". Removing "NamedTuple" as a base class fixes the problem. So this seems to be related to the way mypy handles generic namedtuples.

Your Environment

  • Mypy version used: 0.981
  • Python version used: 3.10.7

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 the linked gist reproduction and run it against mypy 0.981 with Python 3.10.7, then compare the inferred type with the expected int result. Investigate the generic NamedTuple and overload behavior shown there; done means mypy infers int rather than without removing NamedTuple.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.