As a final note: I chose a deliberately simple example here with a pastry shop, and likewise the types were simple.
At PyCon in the US this week, Lukas Llanga gave the keynote on type annotations, and one of his controversial points was that:
str
is often glorifiedAny
I realise when reviewing the notes above that I'm guilty of that myself!
flour
could have been Literal["plain", "self-raising", "lard"]
, which is one way to avoid using an enum.
If using Pydantic, you may want to use constrained types
for the temperature value. There's lots more that can be gotten out of a principled approach,
but this is the bedrock on which it sits so I wanted to record my thoughts while fresh.
Let me know your thoughts on Twitter! @permutans
This post is the last of Designing with dataclasses, a series on using Python dataclasses for clarity about where state lives and ease of reasoning about program behaviour. Read on for discussion of how you can use them with inheritance