-
Notifications
You must be signed in to change notification settings - Fork 312
Raise validation error when unhashable items added to a set #1619
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
Merged
davidhewitt
merged 9 commits into
pydantic:main
from
mikeedjones:mj/unhashable-items-in-set
Feb 6, 2025
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7ec3d1c
add hashable validation before adding item to set
mikeedjones 6634ba4
linting
mikeedjones 0c023ac
linting
mikeedjones 6717e9e
fix error schema change
mikeedjones e45c8cf
bind type and use .hash
mikeedjones fe6851c
fmt
mikeedjones 4eecd19
catch errors on build_add explicitly
mikeedjones 5a0b968
use canonical way of defining unhashable type
mikeedjones 3d924d5
Merge branch 'main' into mj/unhashable-items-in-set
mikeedjones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,23 @@ def test_set_multiple_errors(): | |
| ] | ||
|
|
||
|
|
||
| def test_list_with_unhashable_items(): | ||
| v = SchemaValidator({'type': 'set'}) | ||
|
|
||
| class Unhashable: | ||
| def __hash__(self): | ||
| raise TypeError('unhashable type') | ||
|
||
|
|
||
| unhashable = Unhashable() | ||
|
|
||
| with pytest.raises(ValidationError) as exc_info: | ||
| v.validate_python([{'a': 'b'}, unhashable]) | ||
| assert exc_info.value.errors(include_url=False) == [ | ||
| {'type': 'set_item_not_hashable', 'loc': (0,), 'msg': 'Set items should be hashable', 'input': {'a': 'b'}}, | ||
| {'type': 'set_item_not_hashable', 'loc': (1,), 'msg': 'Set items should be hashable', 'input': unhashable}, | ||
| ] | ||
|
|
||
|
|
||
| def generate_repeats(): | ||
| for i in 1, 2, 3: | ||
| yield i | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making the change like this will have the cost of running hash twice for every element added to the set. I suggest instead the code is adjusted to only try calling
item.hash()ifset.build_add(item)fails, that way we're only adding overhead on the unhappy path.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
set.build_settakes ownership ofitem, instead of cloningitemto calculate the hash ifset.build_setfails, I thought it would be better to instead inspect thePyErrand return theSetItemNotHashableif it's aTypeError. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with that 👍