8000 Add "DECIMAL_CONSTRAINTS" to allow field constraints on "Optional[Dec… · pydantic/pydantic@f024d03 · GitHub
[go: up one dir, main page]

Skip to content

Commit f024d03

Browse files
Add "DECIMAL_CONSTRAINTS" to allow field constraints on "Optional[Decimal]" (#9754)
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
1 parent baf4edc commit f024d03

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pydantic/_internal/_known_annotated_metadata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
GENERATOR_CONSTRAINTS = {*SEQUENCE_CONSTRAINTS, *STRICT}
3939

4040
FLOAT_CONSTRAINTS = {*NUMERIC_CONSTRAINTS, *STRICT}
41+
DECIMAL_CONSTRAINTS = {'max_digits', 'decimal_places', *FLOAT_CONSTRAINTS}
4142
INT_CONSTRAINTS = {*NUMERIC_CONSTRAINTS, *STRICT}
4243
BOOL_CONSTRAINTS = STRICT
4344
UUID_CONSTRAINTS = STRICT
@@ -101,6 +102,8 @@
101102
CONSTRAINTS_TO_ALLOWED_SCHEMAS[constraint].update(('lax-or-strict',))
102103
for constraint in ENUM_CONSTRAINTS:
103104
CONSTRAINTS_TO_ALLOWED_SCHEMAS[constraint].update(('enum',))
105+
for constraint in DECIMAL_CONSTRAINTS:
106+
CONSTRAINTS_TO_ALLOWED_SCHEMAS[constraint].update(('decimal',))
104107

105108

106109
def add_js_update_schema(s: cs.CoreSchema, f: Callable[[], dict[str, Any]]) -> None:

tests/test_types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3390,13 +3390,18 @@ class Model(BaseModel):
33903390
),
33913391
],
33923392
)
3393-
@pytest.mark.parametrize('mode', ['Field', 'condecimal'])
3393+
@pytest.mark.parametrize('mode', ['Field', 'condecimal', 'optional'])
33943394
def test_decimal_validation(mode, type_args, value, result):
33953395
if mode == 'Field':
33963396

33973397
class Model(BaseModel):
33983398
foo: Decimal = Field(**type_args)
33993399

3400+
elif mode == 'optional':
3401+
3402+
class Model(BaseModel):
3403+
foo: Optional[Decimal] = Field(**type_args)
3404+
34003405
else:
34013406

34023407
class Model(BaseModel):

0 commit comments

Comments
 (0)
0