First I define some types
from dataclasses import dataclass
from typing import Tuple,Union
@dataclass
class Point:
x: float
y: float
myPoint= Tuple[int,int]
otherPoint = Union[Tuple[int,int,int] , Tuple[int,int,str]]
Shape = Union[Point , myPoint ,otherPoint]
AS you can see with print(Shape)
, Shape is an union type of 4 elements
typing.Union[main.Point, typing.Tuple[int, int], typing.Tuple[int, int, int], typing.Tuple[int, int, str]]
But the following code doesn't work.
def print_shape(shape: Shape):
match shape:
case Point(x, y):
print(f"Point {x} {y}")
case myPoint as mp:
print(f"{mp[0]}{mp[1]}")
case _ :
print("point 3d")
print(Shape)
print_shape(Point(1, 2))
print_shape((5, 7))
case myPoint as mp: ^^^^^^^ SyntaxError: name capture 'myPoint' makes remaining patterns unreachable
There is 2 others pattern normaly after matching mypoint.
N.B. If you comment case _: , the works fine and as expected.
What is printed
Point 1 2
57