fix(cli-generate-config): ensure configuration types are always toml parsable #785
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.
Purpose
Ensure that when converting pydantic configuration objects into TOML, the TOML comes out parsable.
Rationale
When working on #782, I ran into a problem where the TOML generated was unparsable due to the use of a subclass of IntEnum. The
LevelBump(IntEnum)
caused the string produced by the__str__()
to not be surrounded in quotations in the resulting TOML.This led to discovering that the Tomlkit developer refuses to attempt to address serialization of enums due to the shifting position of Python implementation of enums across versions of python (python-poetry/tomlkit#237). Since Tomlkit will not support enums, we must prevent enum objects from being passed to
tomlkit.dumps()
. Pydantic saves the day with the parametermode
for themodel_dumps()
function which can be set tojson
and it will force the returning dictionary to always use types that are JSON serializable instead of Python classes which may or may not be serializable when passed totomlkit
. I also looked for a possible custom TOML serialize function that could be overrided buttomlkit
does not have one. The effect on IntEnums such asLevelBump
caused the result to be the numeric value of the enum as opposed to the string. The numeric value is effectively written out viatomlkit.dumps()
and then throughpydantic
coercion it is parsed back into the proper enum value.How I tested
I was developing on #782, when the default AngularParserOptions included the
default_level_bump: LevelBump
, it would not generate a parsable toml. You can prove this by adding thedefault_level_bump: LevelBump.NO_RELEASE
to the defaultcommit_parser_options
ofRawConfig
and then attempt to run the dump command. You will see adefault_level_bump = no_release
without quotes! Try to parse withtomlkit
again and it will fail on then
ofno_release
.Outside of the manual discovery above using the
default_level_bump: LevelBump
as acommit_parser_option
, I updated the cli generate-config command testing. This particular testing isn't quite apparent of the problem as there are not any current default values which are non-json serializable, but with #782, there will be!How to verify
And of course, all tests work as with the CI results below.