bug: StrEnum doesn't support optional#132
Merged
anntzer merged 5 commits intoanntzer:mainfrom Oct 11, 2025
Merged
Conversation
Owner
diff --git i/src/defopt.py w/src/defopt.py
index cb902b7..37dc654 100644
--- i/src/defopt.py
+++ w/src/defopt.py
@@ -1178,6 +1178,10 @@ def _is_constructible_from_str(type_):
def _make_enum_parser(enum, value=None):
if value is None:
return functools.partial(_make_enum_parser, enum)
+ if isinstance(value, enum):
+ # If the default is a StrEnum it will be passed to the converter, which
+ # must pass it through.
+ return value
try:
return enum[value]
except KeyError:looks like the fix? Can you make a PR? |
Contributor
Author
|
I can probably simplify the test case to just use an enum and not a strenum, so 3.7 works. |
Contributor
Author
|
@anntzer ready to go! |
Owner
|
I think the previously failing case occurred specifically when the enum class did also inherit from str, no? You can just directly create the double-inherited enum class yourself, class StrChoice(str, Enum):
A = "A"
B = "B"(or something along these lines). |
Contributor
Author
|
Good idea, done |
Owner
|
Thanks for the PR. |
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.
The new
test_str_enum_optionalunit test fails when the default value is set toStrChoice.Abut passes when the default value is set to"A". I was expecting the former to work, not the latter. CC: @geoffjentry