python / python/mypy

[1.20 regression] value pattern match statement narrowing broken for typevars with values

Open
#21,356 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-match-statement topic-reachability topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report
I think I've found a regression in 1.20 - was recently trying to migrate a larger codebase from 1.18 and noticed this issue in a few places which didn't make sense. I've isolated it down to a small example which is a bit silly, but hopefully clear. I think this is a consequence of a deliberate change in https://github.com/python/mypy/pull/20908. Happy to try and put together a PR to resolve if useful, but I'm not exactly across the mypy internals, so might need a bit of guidance.

To Reproduce
https://mypy-play.net/?mypy=1.20.2&python=3.12&gist=ae5055b7e174f0ffd34db390a192aef5


import builtins
from typing import TypeVar

T = TypeVar("T", int, str)

def f(x: T) -> T:
    match type(x):
        case builtins.int:
            return 42        # mypy 1.20: error: Incompatible return value type (got "int", expected "str")
        case builtins.str:
            return "hello"   # mypy 1.20: error: Incompatible return value type (got "str", expected "int")
        case _:
            raise TypeError

Expected Behavior
Expected there to be no issues
Actual Behavior

uv run --with mypy==1.20.2 mypy mre.py 
mre.py:24: error: Incompatible return value type (got "int", expected "str")  [return-value]
mre.py:26: error: Incompatible return value type (got "str", expected "int")  [return-value]

edit: I did a bit more testing, there's nothing special about builtins here, but it needs to be a value pattern and not a class pattern for this example to even work in 1.19

Your Environment

  • Mypy version used: 1.20.2 fails, 1.19 works, a quick bisect suggests afb99ebbc is where this changes
  • Mypy command-line flags: None in minimal example
  • Mypy configuration options from mypy.ini (and other config files): None in minimal example
  • Python version used: 3.13 / 3.12 in playground link above

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 minimal example in mre.py and compare mypy 1.19 with 1.20.2, using commit afb99ebbc as the reported regression point. Trace value-pattern narrowing for constrained TypeVars and add a regression test; done means the example produces no incompatible return-value errors.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.