python / python/mypy

Support exhaustiveness checking of single enumerable properties

Open
#14,261 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature

Support exhaustiveness checking of single boolean and enum properties and attributes of objects.

Pitch

The proposal is to improve exhaustiveness checking by supporting properties/attributes on a matched object. While it would be amazing to have infinitely nested support for this, I think we can recognize that there is value in supporting just single-level enumerable attributes.

In this example mypy isn't currently recognizing that all possible variations of Foo are handled, and gives an error on the assert_never().

from typing import assert_never

class Sentinel: ...

class Foo:
    prop: bool

val: Foo | Sentinel

match val:
    case Sentinel():
        ...
    case Foo(prop=True):
        ...
    case Foo(prop=False):
        ...
    case no_match:
        assert_never(no_match)  # Currently a false positive here, no_match should be <nothing>.

https://mypy-play.net/?mypy=latest&python=3.11&gist=1edcb8f3ea56334662ed2036404563a1

Likewise, if the feature is accepted, we should expect an unreachable error for the third case in this example.

from typing import assert_never

class Foo:
    prop: bool

val: Foo

match val:
    case Foo(prop=True):
        ...
    case Foo(prop=False):
        ...
    case Foo():
        ...  # Currently a false negative here, should be unreachable.

https://mypy-play.net/?mypy=latest&python=3.11&gist=74b23b6868c0c52056be1ade612b3052

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 two Python match examples and their linked mypy-play reproductions. Define the exhaustiveness and unreachable-case behavior for single-level boolean and enum properties, then verify that the false-positive and false-negative outcomes described in the issue are corrected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.