python / python/mypy

mypy is unable to narrow a union type that includes a `StrEnum` subclass and a `Literal`

Open
#20,915 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-enum topic-type-narrowing
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

As per the title, see the gist for an example. The issue does not occur when using Enum instead of StrEnum.

To Reproduce

https://mypy-play.net/?gist=cf9926c276e8fe0f48d7d20b7ebf2b6e

Expected Behavior

The output should be

main.py:12: note: Revealed type is "Literal['none']"
main.py:14: note: Revealed type is "__main__.Color"

Actual Behavior

The output is

main.py:12: note: Revealed type is "__main__.Color | Literal['none']"
main.py:14: note: Revealed type is "__main__.Color | Literal['none']"

Your Environment

  • Mypy version used: 1.19.1

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 linked mypy-play.net reproduction and compare its revealed types with the expected and actual output for main.py lines 12 and 14. Trace the union narrowing behavior for a StrEnum subclass combined with Literal, then add or update coverage for this case and verify the revealed types using the project’s test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.