python / python/mypy

wrong inference with nested unpacking

Open
#21,485 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

Bug Report

Unpacking a list into multiple variables when the list itself is (partially) created by unpacking another iterable the type inference is faulty

To Reproduce

from typing import reveal_type
text = "some\nstuff"
version, desc, *_ = [*text.split(sep="\n", maxsplit=1), None, None]
reveal_type(version)  # str
reveal_type(desc)      # None

Expected Behavior

Mypy infers version and desc and str | None (if you want to go very far you can even infer version as str because str.split never returns an empty list, but that is not my expectation)

Actual Behavior

Mypy infers version as str and desc as None

Your Environment

Mypy playground: https://mypy-play.net/?mypy=latest&python=3.12&flags=strict%2Cstrict-bytes%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-reexport%2Cno-strict-optional%2Cstrict-equality%2Cstrict-equality-for-none%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores%2Cextra-checks

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 nested-unpacking reproducer in the mypy playground and inspect how reveal_type reports version and desc. Trace the type inference for the starred list expression and tuple unpacking; done when both variables are inferred as str | None in the reported example.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.