Repository navigation
Generate StrEnum and IntEnum for typed enumerations - #1242
Merged
Merged
Conversation
Generated enums extend StrEnum when every member is a str, IntEnum when every member is an int, and Enum otherwise. Mixin enums list str/int before Enum in their mro, so the runtime treated them as plain values: - The converter resolves enum types before walking the mro - Compound fields match enum values by their exact type only - The dict and xml serializers encode enums before primitives - The pycode serializer renders Class.MEMBER instead of str(member) Closes #1223
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1242 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 117 117
Lines 9412 9428 +16
Branches 1446 1451 +5
=======================================
+ Hits 9410 9426 +16
Misses 1 1
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #1223
Generated enums now pick their base class from their members:
str→StrEnumint→IntEnumEnumThere is no config option.
Flag/IntFlagare left out: their bitwise semantics don't fit XSD enumerations.Runtime fixes
Mixin enums list
str/intbeforeEnumin their mro, so the runtime treated their members as plain values:ConverterFactory.type_converterresolves enum types before walking the mro. Before,"blue"parsed into aStrEnumfield came back as a plainstrwith no validation.StrEnumvalue matched an earlierstrchoice, so netex wrote<ComparisonOperator>as<AttributeName>.DictEncoderandEventGenerator.encode_primitiveencode enums before primitives, so they output plain values instead of members.PycodeSerializerrendersClass.MEMBERfrom the qualname instead ofstr(member). That also fixes nested enums, which printedInner.Xwhile onlyOuterwas imported.Existing plain
Enummodels parse and serialize exactly as before.Behaviour change for generated code
StrEnummembers compare equal to their string value,str(member)and f-strings return the value, and a member and its raw string share a dict/set key.IntEnummembers compare, sort and do arithmetic with plain ints.Regression suites
.xmloutput changes. The diff is 2589StrEnumand 9IntEnumbase classes plus enum imports.main), no.xmloutput changes. The diff is 198StrEnumand 98IntEnumbase classes plus enum imports.The suite output branches (
typed-enums) are ready locally.