Feature: Markers: SelectionFromEnumValues
-
Add configuration marker: SelectionFromEnumValues
-
Refactor instantiator production logic for types with automatically-populated choices. Encapsulation to keep related variables tracked togther.
Resolves #166 .
@brentyi : All linter checks and tests are passing. Not sure if you want anything additional for documentation; looks like you have an automatic generator which I did not look closely at.
Also, manual proof that it works as described and intended:
>>> import dataclasses, enum, typing_extensions, tyro
>>> class Formats( enum.StrEnum ):
... JSON = enum.auto( )
... PRETTY = enum.auto( )
...
>>> @dataclasses.dataclass
... class Args:
... format: typing_extensions.Annotated[ Formats, tyro.conf.SelectFromEnumValues ] = Formats.PRETTY
...
>>> tyro.cli( Args, args = [ '--format', 'json' ] )
Args(format=<Formats.JSON: 'json'>)
>>> tyro.cli( Args, args = [ '--format', 'pretty' ] )
Args(format=<Formats.PRETTY: 'pretty'>)
>>> tyro.cli( Args, args = [ ] )
Args(format=<Formats.PRETTY: 'pretty'>)
>>> tyro.cli( Args, args = [ '--help' ] )
usage: [-h] [--format {json,pretty}]
╭─ options ───────────────────────────────────────────────╮
│ -h, --help show this help message and exit │
│ --format {json,pretty} (default: pretty) │
╰─────────────────────────────────────────────────────────╯
Code coverage error appears to be a Codecov rate-limiting issue.
However, I just realized that I missed the Literal case that you mentioned in the issue. I need to cover that and make tests accordingly. So, please do not merge yet.
@brentyi : I just added the enum values case for literal lists that I forgot earlier. Should be good to go.
Thanks @emcd for the PR!! Just wanted to note that reviewing/merging this is on my to-do list, unfortunately this week has been busier than most...
No worries, @brentyi . Life (Ph.D. theses, etc...) comes first. I've been using a fork of your repo with my patch, so I'm not blocked.
I am going to file another feature request soon, though, but no pressure in responding to it.
Thanks for understanding @emcd! I just pushed some minor changes to your branch, could you confirm that they look reasonable?
- You had defined some classes for handling the different ways of generating choices. I refactored a bit to make it more succinct / self-contained. It might be less type-safe.
- I relaxed the constraint that values need to be strings + added a test.
The changes seem good to me, @brentyi . I actually introduced the classes to make things a bit more self-contained so that we weren't working with two or three different related variables, side by side, in multiple places. But, I have no objection to what you have done.
I just added a commit to fix the example to match the renamed marker.
Thanks for the fix! And the help with this feature. For the classes your explanation makes sense, seems like just a matter of personal taste. October is feeling like a "limit abstractions" month for me 😛