Mypy can't correctly infer nested union types in some situations
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the issue's good, bad, and good2 examples with mypy and compare the inferred types for the list literal and concatenation. Trace the nested union and list inference path, then add a regression test showing that bad() is accepted without the cast while existing type-checking behavior remains intact.
Written by the indexing model from the issue text.
Description
I'm not sure what the cause is here, but I documented some simple examples of this issue. I would expect all of these examples to pass the type checker, but one fails peculiarly. I wouldn't be surprised if this is the result of an existing issue, but I couldn't find it.
from typing import cast, List, Union
Scalar = Union[str, float]
def good() -> List[Scalar]:
return ["foo", 1.0]
def bad(params: List[Scalar]) -> List[Scalar]:
# error: Incompatible return value type (got List[object], expected
# List[Union[str, float]])
# error: Unsupported operand types for + (List[object] and
# List[Union[str, float]])
return ["foo", 1.0] + params
def good2(params: List[Scalar]) -> List[Scalar]:
return cast(List[Scalar], ["foo", 1.0]) + params
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
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.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100