Skip to content

Commit

Permalink
fix: constraints with a Falsy value should still return a truthful ha…
Browse files Browse the repository at this point in the history
…s_constraints (#1844)
  • Loading branch information
ericvandever authored Feb 13, 2024
1 parent 2d0f900 commit ab4a39c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datamodel_code_generator/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Config:

@cached_property
def has_constraints(self) -> bool:
return any(v for v in self.dict().values() if v is not None)
return any(v is not None for v in self.dict().values())

@staticmethod
def merge_constraints(
Expand Down
21 changes: 21 additions & 0 deletions tests/model/pydantic/test_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Optional, Union

import pytest

from datamodel_code_generator.model.pydantic.base_model import Constraints
from datamodel_code_generator.types import UnionIntFloat


@pytest.mark.parametrize(
'gt,expected',
[
(None, False),
(4, True),
(0, True),
(0.0, True),
],
)
def test_constraint(gt: Optional[Union[int, float]], expected: bool) -> None:
constraints = Constraints()
constraints.gt = UnionIntFloat(gt) if gt is not None else None
assert constraints.has_constraints == expected

0 comments on commit ab4a39c

Please sign in to comment.