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