diff --git a/datamodel_code_generator/model/base.py b/datamodel_code_generator/model/base.py index 462051dcb..559dd144b 100644 --- a/datamodel_code_generator/model/base.py +++ b/datamodel_code_generator/model/base.py @@ -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( diff --git a/tests/model/pydantic/test_constraint.py b/tests/model/pydantic/test_constraint.py new file mode 100644 index 000000000..1c68fa36c --- /dev/null +++ b/tests/model/pydantic/test_constraint.py @@ -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