asweigart / asweigart/pyinputplus

Feature request: support `Enum`s in `inputMenu` and `inputChoice`

Open
#22 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
114
Forks
19
PR merge metrics
No merged PRs in 30d

Description

If I'm presenting a user with a list of choices, this works really nicely:

```
from pyinputplus import inputMenu

CHOICES = ('Go left', 'Go right')

inputMenu(choices=CHOICES, prompt='Pick a direction')
```

It would be really nice, however, to do this instead, which isn't supported:

```
from enum import Enum
from pyinputplus import inputMenu

class Choices(str, Enum):
one = 'Go left'
two = 'Go right'

inputMenu(choices=Choices, prompt='Pick a direction')
```

`Enum` classes are iterable, so will pass an `isinstance(, collections.abc.Iterable)` test. But they're not, strictly speaking, sequences, which means [this line](https://github.com/asweigart/pysimplevalidate/blob/98c17951be29689b5f5508c8ab9721dd44c55a9f/src/pysimplevalidate/__init__.py#L706) raises an error if I try to feed in an `Enum` of choices.

Would it be possible to support `Enum`s in `inputMenu` and `inputChoices`? I'd be happy to submit a PR if it would be helpful, but I don't know whether changing the validation function in `pysimplevalidate` would have unintended consequences.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the validation logic in src/pysimplevalidate/__init__.py around line 706, then trace how pyinputplus inputMenu and inputChoice pass their choices to it. Confirm how Enum classes and their values should be accepted without changing existing sequence behavior, and verify both entry points with the examples in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.