Skip to content

list of lists not inferred correctly #20420

@Dr-Irv

Description

@Dr-Irv

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featuretopic-inferenceWhen to infer types or require explicit annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions