python / python/mypy

Literal infers tuple literal as Union of the items though this syntax is forbidden by PEP 586

Open
#7,783 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-literal-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

The section Illegal parameters for Literal at type check time describes that tuple literals inside Literal are forbidden:

Tuples containing valid literal types like Literal[(1, "foo", "bar")]. The user could always express this type as Tuple[Literal[1], Literal["foo"], Literal["bar"]] instead. Also, tuples are likely to be confused with the Literal[1, 2, 3] shortcut.

Nevertheless mypy seems to be taking this shortcut since it doesn't flag the following code:

from typing_extensions import Literal

a : Literal[(1, 2)] = 1 

Python 3.7, mypy 0.740, typing-extensions 3.7.4.

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 reproducing the reported Python snippet using Literal[(1, 2)] and confirm whether mypy accepts it. Locate the Literal type-checking path and its existing tests; done means tuple literals in Literal are rejected in accordance with PEP 586.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.