list of lists not inferred correctly
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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