-
Notifications
You must be signed in to change notification settings - Fork 309
[Fix(9559)]
- Validation fails for enum field with decimal type
#1324
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
Closed
mikeleppane
wants to merge
15
commits into
pydantic:main
from
mikeleppane:fix(9559)/validation-fails-for-enum-field-with-decimal-type
Closed
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d736610
Fix(9559): Validation of enum field with decimal fails
vai-mikkoleppanen 399245b
chore: Add .venv to .gitignore
vai-mikkoleppanen 36ce3ba
fix: Improve unit test case
vai-mikkoleppanen dd32fc0
Improve enum validation test cases for decimal values
vai-mikkoleppanen b327afa
Move validate_decimal call to the last one.
vai-mikkoleppanen b11d3e8
fix: make Decimal type work with StrEnum when strict mode is not enabled
vai-mikkoleppanen af0766c
fix: StrEnum not supported in all envs
vai-mikkoleppanen 6a393e4
chore: Refactor decimal validation to the decimal module
vai-mikkoleppanen beffb8f
refactor: simplify try_from_decimal_to_int function's return type
vai-mikkoleppanen 0e23f4a
refactor: Improve enum validation test case for decimal values
vai-mikkoleppanen 80308f3
refactor: improve validation logic to include any type that is equal …
vai-mikkoleppanen a5e1d3d
refactor: minor refactoring for try_validate_any
vai-mikkoleppanen 00b5346
refactor: make is_equal_to function generic and move it out of try_va…
vai-mikkoleppanen a6c2a30
chore: remove useless test case
vai-mikkoleppanen 5d6986c
fix: refactoring based on code review comment
vai-mikkoleppanen 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ docs/_build/ | |
htmlcov/ | ||
node_modules/ | ||
|
||
.venv | ||
/.benchmarks/ | ||
/.idea/ | ||
/.pytest_cache/ | ||
|
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import re | ||
import sys | ||
from decimal import Decimal | ||
from enum import Enum, IntEnum, IntFlag | ||
|
||
import pytest | ||
|
@@ -344,3 +345,163 @@ class ColorEnum(IntEnum): | |
|
||
assert v.validate_python(ColorEnum.GREEN) is ColorEnum.GREEN | ||
assert v.validate_python(1 << 63) is ColorEnum.GREEN | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'value', | ||
[-1, 0, 1], | ||
) | ||
def test_enum_int_validation_should_succeed_for_decimal(value: int): | ||
# GIVEN | ||
class MyEnum(Enum): | ||
VALUE = value | ||
|
||
class MyIntEnum(IntEnum): | ||
VALUE = value | ||
|
||
# WHEN | ||
v = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())), | ||
default=MyEnum.VALUE, | ||
) | ||
) | ||
|
||
v_int = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyIntEnum, list(MyIntEnum.__members__.values())), | ||
default=MyIntEnum.VALUE, | ||
) | ||
) | ||
|
||
# THEN | ||
assert v.validate_python(Decimal(value)) is MyEnum.VALUE | ||
assert v.validate_python(Decimal(float(value))) is MyEnum.VALUE | ||
|
||
assert v_int.validate_python(Decimal(value)) is MyIntEnum.VALUE | ||
assert v_int.validate_python(Decimal(float(value))) is MyIntEnum.VALUE | ||
|
||
|
||
def test_enum_int_validation_should_succeed_for_custom_type(): | ||
# GIVEN | ||
class AnyWrapper: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return self.value == other | ||
|
||
class MyEnum(Enum): | ||
VALUE = 999 | ||
SECOND_VALUE = 1000000 | ||
THIRD_VALUE = 'Py03' | ||
|
||
# WHEN | ||
v = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())), | ||
default=MyEnum.VALUE, | ||
) | ||
) | ||
|
||
# THEN | ||
assert v.validate_python(AnyWrapper(999)) is MyEnum.VALUE | ||
assert v.validate_python(AnyWrapper(1000000)) is MyEnum.SECOND_VALUE | ||
assert v.validate_python(AnyWrapper('Py03')) is MyEnum.THIRD_VALUE | ||
|
||
|
||
def test_enum_str_validation_should_succeed_for_decimal_with_strict_disabled(): | ||
# GIVEN | ||
class MyEnum(Enum): | ||
VALUE = '1' | ||
|
||
# WHEN | ||
v = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())), | ||
default=MyEnum.VALUE, | ||
) | ||
) | ||
|
||
# THEN | ||
assert v.validate_python(Decimal(1)) is MyEnum.VALUE | ||
|
||
|
||
def test_enum_str_validation_should_fail_for_decimal_with_strict_enabled(): | ||
# GIVEN | ||
class MyEnum(Enum): | ||
VALUE = '1' | ||
|
||
# WHEN | ||
v = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()), strict=True), | ||
default=MyEnum.VALUE, | ||
) | ||
) | ||
|
||
# THEN | ||
with pytest.raises(ValidationError): | ||
v.validate_python(Decimal(1)) | ||
|
||
|
||
def test_enum_int_validation_should_fail_for_incorrect_decimal_value(): | ||
# GIVEN | ||
class MyEnum(Enum): | ||
VALUE = 1 | ||
|
||
class MyStrEnum(Enum): | ||
VALUE = '2' | ||
|
||
# WHEN | ||
v = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())), | ||
default=MyEnum.VALUE, | ||
) | ||
) | ||
|
||
v_str = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyStrEnum, list(MyStrEnum.__members__.values())), | ||
default=MyStrEnum.VALUE, | ||
) | ||
) | ||
|
||
# THEN | ||
with pytest.raises(ValidationError): | ||
v.validate_python(Decimal(2)) | ||
|
||
with pytest.raises(ValidationError): | ||
v.validate_python((1, 2)) | ||
|
||
with pytest.raises(ValidationError): | ||
v.validate_python(Decimal(1.1)) | ||
|
||
with pytest.raises(ValidationError): | ||
v_str.validate_python(Decimal(1)) | ||
|
||
with pytest.raises(ValidationError): | ||
v_str.validate_python(Decimal(2.1)) | ||
|
||
|
||
def test_enum_int_validation_should_fail_for_plain_type_without_eq_checking(): | ||
# GIVEN | ||
class MyEnum(Enum): | ||
VALUE = 1 | ||
Comment on lines
+456
to
+458
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realise now that we probably also want to test this with e.g. |
||
|
||
class MyClass: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
# WHEN | ||
v = SchemaValidator( | ||
core_schema.with_default_schema( | ||
schema=core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())), | ||
default=MyEnum.VALUE, | ||
) | ||
) | ||
|
||
# THEN | ||
with pytest.raises(ValidationError): | ||
v.validate_python(MyClass(1)) |
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
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.
is_instance_of::<PyAny>()
I think will always betrue
, will probably be optimized away by the compiler but also not necessary at all IMO.