python / python/mypy

list of lists not inferred correctly

Open
#20,420 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-inference topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

This came up in testing for pandas-stubs.

If you have a list of lists of different types, mypy infers it as a list[object] rather than a list of the union of the lists of the inner types.

To Reproduce

class NAType:
    pass

class NaTType:
    pass

NA: NAType = NAType()
NaT: NaTType =  NaTType()

foo = [[None], [NA], [NaT]]
reveal_type(foo)

Expected Behavior

Revealed type is list[list[None] | list[NAType] | list[NaTType]]

This is what pyright returns with the setting strictListInference=True

Actual Behavior

Revealed type is "builtins.list[builtins.object]

Your Environment

  • Mypy version used: 1.19.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11

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 by running the issue's Python reproduction with mypy 1.19.0 and inspect the revealed type for foo. Trace the list-inference path responsible for combining nested list types, then verify that the result is list[list[None] | list[NAType] | list[NaTType]] without changing the existing command-line or configuration assumptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.