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

Fixed literal validator errors for unhashable values #6194

Merged
merged 1 commit into from Jun 20, 2023

Conversation

markus1978
Copy link

@markus1978 markus1978 commented Jun 20, 2023

Change Summary

If a value is not hashable and used on the dict of expected values a TypeError is raised. The code now also catches the TypeError along side the KeyError to cause the validator to fail with a WrongConstantError.

Related issue number

fix #6188

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@markus1978 markus1978 force-pushed the 1.10.X-fixes branch 2 times, most recently from 91208b6 to 4d422d8 Compare June 20, 2023 10:58
@@ -486,6 +486,9 @@ def make_literal_validator(type_: Any) -> Callable[[Any], Any]:
allowed_choices = {v: v for v in permitted_choices}

def literal_validator(v: Any) -> Any:
if not isinstance(v, Hashable):
Copy link
Author

Choose a reason for hiding this comment

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

Wasn't really sure what you prefer here. The alternative to this guard if, would be catching TypeError. Less readable, but would only cause overhead in the error case.

Copy link
Author

Choose a reason for hiding this comment

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

Catching TypeError as well, would also remove the more or less duplicated core for raising the error. I changed it to catch a TypeError now.

@hramezani hramezani merged commit d7afe99 into pydantic:1.10.X-fixes Jun 20, 2023
49 checks passed
@hramezani
Copy link
Member

Thanks @markus1978

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.

None yet

2 participants