Checks
Feature
Python 3.10 introduces the new match syntax. https://peps.python.org/pep-0636/
Example:
num = 5
match num:
case 5:
print('is five')
case default:
print('is not five')
They support matching on custom classes too, with the __match_args__ attribute.
class MyClass:
__match_args__ = ('num1', 'num2')
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
myinstance = MyClass(5, 10)
match myinstance:
case MyClass(5, num2):
print(f'is 5 and {num2}')
case MyClass(_, num2):
print(f'is not 5 and is {num2}')
I've found myself adding this attribute to pydantic classes by hand, eventhough it seems pretty intuitive to me that the default behavior should match fields in the order that they were specified. So my code looks kind of like this:
class UpdateThing(BaseModel):
__match_args__ = ("thing", "value")
thing: Thing
value: str
class AddThing(BaseModel):
__match_args__ = ("index", "thing")
index: int
thing: Thing
Might as well add it natively?
I'm opening this issue to couple it with a PR :)
Checks
Feature
Python 3.10 introduces the new
matchsyntax. https://peps.python.org/pep-0636/Example:
They support matching on custom classes too, with the
__match_args__attribute.I've found myself adding this attribute to pydantic classes by hand, eventhough it seems pretty intuitive to me that the default behavior should match fields in the order that they were specified. So my code looks kind of like this:
Might as well add it natively?
I'm opening this issue to couple it with a PR :)