Skip to content

Conversation

@nh13
Copy link
Contributor

@nh13 nh13 commented Oct 3, 2025

The new test_str_enum_optional unit test fails when the default value is set to StrChoice.A but passes when the default value is set to "A". I was expecting the former to work, not the latter. CC: @geoffjentry

@anntzer
Copy link
Owner

anntzer commented Oct 8, 2025

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?

@nh13
Copy link
Contributor Author

nh13 commented Oct 9, 2025

I can probably simplify the test case to just use an enum and not a strenum, so 3.7 works.

@nh13
Copy link
Contributor Author

nh13 commented Oct 9, 2025

@anntzer ready to go!

@anntzer
Copy link
Owner

anntzer commented Oct 9, 2025

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).

@nh13
Copy link
Contributor Author

nh13 commented Oct 9, 2025

Good idea, done

@anntzer anntzer merged commit 0ffa53b into anntzer:main Oct 11, 2025
9 checks passed
@anntzer
Copy link
Owner

anntzer commented Oct 11, 2025

Thanks for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants