python / python/mypy

erroneous type mismatch with `Union` and `Literal` (false positive)

Open
#17,151 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

Mypy does not sufficiently simplify Unions and Literals before some type checks (the reproduction case uses assert_type(), but assignments and calls will also trigger the issue). The result is false positive errors.

To Reproduce

[mypy-play.net]

from __future__ import annotations
from enum import (
        Enum,
        auto,
        )
from typing import (
        Literal,
        Union,
        )
from typing_extensions import (
        assert_type,
        reveal_type,
        )

class Color(Enum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()

RedKwds = Literal['RED', 'R']
GreenKwds = Literal['GREEN', 'G']
BlueKwds = Literal['BLUE', 'B']
ColorKwds = Union[RedKwds, GreenKwds, BlueKwds]

RedSpec = Union[Literal[Color.RED], RedKwds]
GreenSpec = Union[Literal[Color.GREEN], GreenKwds]
BlueSpec = Union[Literal[Color.BLUE], BlueKwds]

ColorSpec = Union[Color, ColorKwds]             # ⎱ these are entirely
RGBSpec = Union[RedSpec, GreenSpec, BlueSpec]   # ⎰ equivalent types


rgb_spec: RGBSpec
assert_type(rgb_spec, ColorSpec)  # fails; should be silent             # L34
        # Expression is of type...
        #   Union[
        #       Union[Literal[Color.RED], Literal['RED', 'R']],
        #       Union[Literal[Color.GREEN], Literal['GREEN', 'G']],
        #       Union[Literal[Color.BLUE], Literal['BLUE', 'B']]
        #       ]
        # ... not ...
        #   Union[
        #       Color,
        #       Union[
        #           Literal['RED', 'R'],
        #           Literal['GREEN', 'G'],
        #           Literal['BLUE', 'B']
        #           ]
        #       ]
reveal_type(rgb_spec)                                                   # L50
        # Union[
        #       Literal[Color.RED], Literal['RED'], Literal['R'],
        #       Literal[Color.GREEN], Literal['GREEN'], Literal['G'],
        #       Literal[Color.BLUE], Literal['BLUE'], Literal['B']
        #       ]


# "shaking" the variable (where "shaking" is clearly a no-op) causes
# things to work as expected:
rgb_spec_shaken = (
        rgb_spec
        if isinstance(rgb_spec, Color) else
        rgb_spec
        )
assert_type(rgb_spec_shaken, ColorSpec)  # passes!
reveal_type(rgb_spec_shaken)                                            # L66
        # Union[
        #       Color,
        #       Literal['RED'], Literal['R'],
        #       Literal['GREEN'], Literal['G'],
        #       Literal['BLUE'], Literal['B']
        #       ]

Expected Behavior

The types are provably equivalent; no error should be raised.

Actual Behavior

Mypy does not sufficiently simplify the types, resulting in an erroneous "mismatch".

Your Environment

  • Mypy version used: 1.9.0, master 2024-04-22
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8, 3.12

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 running the issue's mypy-play reproduction with the shown assert_type() and reveal_type() cases, especially lines 34 and 50, then inspect the type simplification and compatibility paths involved in those checks. Done means equivalent Union and Literal types no longer produce a mismatch, while the displayed revealed type is appropriately simplified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.