Skip to content

[match-case] Allow matching Union types #106246

@randolf-scholz

Description

@randolf-scholz

Feature or enhancement

Since python 3.10, isinstance and issubclass allow testing against Union-types (PEP604).
However, the same is not true for match-case.

Pitch

case klass(): should probably be 1:1 equivalent with isinstance(x, klass):.

from typing import TypeAlias

Numeric: TypeAlias = int | float

assert isinstance(1.2, Numeric)  # ✔

match 1.2:
     case Numeric():  # TypeError: called match pattern must be a type
          pass

This carries extra benefits, as TypeAliases are kind of necessary to write legible code when working with large unions, such as e.g. PythonScalar: TypeAlias = None | bool | int | float | complex | bool | str | datetime | timedelta.

Linked PRs

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy