python / python/mypy

detect invalid `Literal[Foo.foo | Foo.bar]` → `Literal[Foo.foo, Foo.bar]`

Open
#20,438 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

import enum
from typing import Literal


class Foo(enum.IntEnum):
    foo = 1
    bar = 2


def foo(bar: Literal[Foo.foo | Foo.bar]) -> None:
    pass

ty detect this as invalid-type-form:

$ ty check t.py 
t.py:10:22: error[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member

(https://github.com/astral-sh/ty/issues/876#issuecomment-3669957183)

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 example in t.py and running the shown type-check command, then trace how Literal arguments containing enum members and the | expression are validated. Done means the example is handled as Literal[Foo.foo, Foo.bar] rather than reported as invalid-type-form, with a regression test covering it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.