Skip to content

Update error message for enum validator#673

Merged
samuelcolvin merged 3 commits intopydantic:masterfrom
dmontagu:enum_error_msg
Jul 24, 2019
Merged

Update error message for enum validator#673
samuelcolvin merged 3 commits intopydantic:masterfrom
dmontagu:enum_error_msg

Conversation

@dmontagu
Copy link
Copy Markdown
Contributor

@dmontagu dmontagu commented Jul 17, 2019

Change Summary

Modify the EnumError message to be a little clearer (in line with the WrongConstantError).

Related issue number

#671

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • include your github username @<whomever>

Comment thread tests/test_types.py
'loc': ('tool',),
'msg': 'value is not a valid enumeration member; permitted: 1, 2',
'type': 'type_error.enum',
'ctx': {'enum_type': ToolEnum},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

putting the type in the context (rather than the permitted values, as in WrongConstantError), means that the type is visible in the traceback for debugging purposes, while only the permitted values are visible in the error message (which may be served externally).

Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin Jul 19, 2019

Choose a reason for hiding this comment

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

makes sense, the idea of ctx was something like "things you might need when rendering your own error message".

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 17, 2019

Codecov Report

Merging #673 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #673   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2712   2716    +4     
  Branches      537    538    +1     
=====================================
+ Hits         2712   2716    +4

@samuelcolvin
Copy link
Copy Markdown
Member

LGTM, will merge when I get this far through 😓

Comment thread pydantic/validators.py


def enum_validator(v: Any, field: 'Field', config: 'BaseConfig') -> Enum:
with change_exception(errors.EnumError, ValueError):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why don't just add kwargs support to context manager?
and then:

with change_exception(errors.EnumError, ValueError, enum_type=field.type_):
    ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing that would be better placed in a separate PR; not sure it would be worth it if it was only used in this instance. But I haven't looked to see if it could simplify the error handling anywhere else.

@samuelcolvin samuelcolvin merged commit b702eb8 into pydantic:master Jul 24, 2019
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
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