Given the following definition:
class MyEnum(IntEnum):
a = 1
b = 2
c = 3
d = 4
I am looking for an expression which replaces SomeExpression
which will both statically assert that the value myEnum
is either MyEnum.c | MyEnum.d
in that branch – such that the ‘never’ case at the bottom correctly infers type never
– and will, of course, ensure at runtime that the correct branch is taken such that ‘matched’ is output to the console.
def check(myEnum: MyEnum):
match myEnum:
case SomeExpression:
print("matched")
case MyEnum.a:
pass
case MyEnum.b:
pass
case never:
assert_never(never)
Is there any such way to achieve this in python? I’ve tried using simpler methods like a predicate inside the match which does not narrow the type statically as desired. I’ve also investigated __class_getitem__
but not been able to get it working as expected.