Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure sorted in In works with container of data types #463

Closed
wants to merge 4 commits into from

Conversation

spacegaier
Copy link
Collaborator

Alternative to #452

I would prefer to keep the sorting, since that IMHO improves the error message we return, especially when utilized in user facing use cases.

This PR nonetheless ensures we properly handle containers with data types.

@Avorthoren
Copy link

Do you really want to sort, for example, ints by their str representantion?

>>> print(sorted([1, 11, 2], key=str))
[1, 11, 2]

@beastd
Copy link

beastd commented Apr 26, 2022

def __call__(self, v):
        try:
            check = v not in self.container
        except TypeError:
            check = True
        if check:
            if self.msg:
                raise InInvalid(self.msg)

            try:
                allowed = sorted(self.container)
            except:
                allowed = sorted(self.container, key=str)

            raise InInvalid('value must be one of {}'.format(allowed))
        return v

Maybe something like this would be acceptable?

Not strictly related to the issue at hand: Maybe containers with lots of items should neither be sorted nor rendered in the help message. One could e.g. just say "value must be one of 123456 allowed values" instead.

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.

3 participants