feat: add choices to strings to enable literal types#1580
feat: add choices to strings to enable literal types#1580MaximSrour wants to merge 2 commits intoormar-orm:masterfrom
Conversation
Merging this PR will degrade performance by 23.72%
Performance Changes
Comparing Footnotes |
|
Any particural reason why you dont want to use EnumField here? |
I wrote about it a bit in the linked issue, but it mostly comes down to flexibility - with an enum, I'd need to push a migration to update the column type (which can take a while when there are a lot of records), after which I'll be able to use the new value. With a string column instead, the code can immediately start using the new values. |
|
We used to have If we would like to restore it as Note also that you now overwrite the |
Summary
Enables
ormar.Stringto preserve string literal typing whenchoicesare provided, while keeping the underlying database column assqlalchemy.String.Closes #1579.
Changes
ormar.Stringto project atyping.Literal[...]pydantic type when stringchoicesare providedString(..., choices=..., nullable=True)by wrapping the projected type inOptional[...]Stringbehavior unchanged whenchoicesare not providedString(..., choices=...)StringandString(..., choices=...)in a single sectionTesting
Built and ran targetted tests for the new changes, and ran tests against the entire suite. All passing.
Installed a local version of ORMAR into a personal repository that uses it, and tested against existing type errors.
Screenshots
Error before:

Using the new literal inference:
