Proof of concept for replacing int in constants.py with IntEnums#56
Proof of concept for replacing int in constants.py with IntEnums#56
int in constants.py with IntEnums#56Conversation
|
I've finally had the chance to go through this and I do like it a lot. This approach to the enum AVPs should have been there from the start and I think we should merge this. Regarding backwards compatibility - it looks compatible enough to me, especially as you have kept the old enum constants still in The only thing this pull request doesn't really fulfill, is what you set out as one of your initial goals: "Possibility to make |
This branch is proof of concept for replacing
E_*intconstants inconstants.pywithenum.IntEnum-based classes.Pros:
constantsnamespace much smaller, my LSP is struggling when there is a star import from it right now.Cons:
IntEnumis based onintand inherits almost all behaviours from it, but not all, so if user is doing something stupid, liketype(...) == int- code might break.There are other possible implementations of this proposal - this PoC shows the easiest way to introduce it - by defining enums in
constants.py, making old names reference new names, and then updatingMessage.from_bytesandAvpEnumerated, so they always returnIntEnumvalues (it also defines things where they shouldn't just for simplicity).ints andIntEnums.IntEnums only when some flag is passed toMessage.from_bytesor globally set on library import. Same or separate flag can make it to throw exception when trying to decode message with unknown enumerated values.IntEnumclasses can be defined in separate files from currentconstants.py, completely separate from already definedintconstants, although this adds additional maintenance burden to keep them in sync.In my opinion first and third pros worth it to make decoding optionally convert ints to enums, but I'm open to other suggestions, there may be some nuances that I don't see.