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

Allow unexpected values for ChoiceField/MultipleChoiceField representations #2940

Merged
merged 2 commits into from
May 15, 2015

Conversation

shangxiao
Copy link
Contributor

Closes #2839

try:
return self.choice_strings_to_values[six.text_type(value)]
except KeyError:
return value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably prefer to drop the indirection & just include this logic in the code as-is without the extra method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was actually my first attempt, although how would you inline a try/except inside a list comprehension? Was asking on #django and was told it may be possible with a generator expression & yielding?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yup, I see that now. Missed the wrapping list comprehension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just do if six.text_type(value) in self.choice_strings_to_values instead of relying on a try/except here?

(Not suggesting that we remove the extra method, just commenting on the try/except that is being used)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, perhaps a self.choice_strings_to_values.get(six.text_type(value), value) is better here?

@shangxiao
Copy link
Contributor Author

@tomchristie
Copy link
Member

Method call is simpler afterall :)

@tomchristie tomchristie added this to the 3.1.3 Release milestone May 15, 2015
tomchristie added a commit that referenced this pull request May 15, 2015
Allow unexpected values for ChoiceField/MultipleChoiceField representations
@tomchristie tomchristie merged commit e33fed7 into encode:master May 15, 2015
@tomchristie
Copy link
Member

Perfect!

@xordoquy xordoquy changed the title Allow unexpected values for ChoiceField/MultipleChoiceField representations Allow unexpected values for ChoiceField/MultipleChoiceField representations Jun 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow unexpected values for ChoiceField/MultipleChoiceField representations.
3 participants