Skip to content

Commit bb3d1c7

Browse files
authored
Merge pull request #116 from NHSDigital/fix/gr-regression_fixes_post_pydantic_v2_release
Fix/gr regression fixes post pydantic v2 release
2 parents ee9b1bc + 4d6b082 commit bb3d1c7

3 files changed

Lines changed: 66 additions & 7 deletions

File tree

src/dve/core_engine/backends/utilities.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Necessary, otherwise uncategorised backend functionality."""
22

3+
import inspect
34
import sys
45
from dataclasses import is_dataclass
56
from datetime import date, datetime, time
@@ -41,10 +42,11 @@
4142

4243
def is_type_complex(type_: Any) -> bool:
4344
"""Check whether a type is a complex type or not."""
44-
if type_ in (str, int, float, bool, bytes):
45-
return False
45+
_simple_types = (str, int, float, bool, bytes, date, datetime, time, Decimal)
4646

47-
if type_ in (date, datetime, time, Decimal):
47+
if type_ in _simple_types or (
48+
inspect.isclass(type_) and any(issubclass(type_, st) for st in _simple_types)
49+
):
4850
return False
4951

5052
return True

src/dve/metadata_parser/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def get_type_and_validators(
231231
elif hasattr(possible_python_type, "get_type_and_validators"):
232232
possible_python_type: "FieldSpecification" # type: ignore
233233
nested_vals = possible_python_type.get_type_and_validators( # type: ignore
234-
field_name, *type_mappings, schemas=schemas, is_mandatory=False
234+
field_name, *type_mappings, schemas=schemas, is_mandatory=is_mandatory
235235
)
236236
python_type, nested_default, nested_validators = nested_vals # type: ignore
237237

tests/test_core_engine/test_backends/test_utilities.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1-
from datetime import date
1+
"""
2+
Test backend utility functions
3+
"""
4+
5+
# pylint: disable=C0115,C0116
6+
7+
from datetime import date, datetime
8+
from decimal import Decimal
29
from typing import Any
310

411
import pytest
512
from pydantic import BaseModel
613

7-
from dve.core_engine.backends.utilities import is_field_complex
14+
from dve.core_engine.backends.utilities import is_field_complex, stringify_model
15+
from dve.metadata_parser.domain_types import FormattedDatetime
16+
17+
18+
def model_to_schema(mdl: BaseModel) -> dict[str, Any]:
19+
schema = {}
20+
for field_name, field_md in mdl.model_fields.items():
21+
if issubclass(field_md.annotation, BaseModel):
22+
schema[field_name] = model_to_schema(field_md.annotation)
23+
else:
24+
schema[field_name] = field_md.annotation
25+
26+
return schema
827

928

1029
class AnotherTestModel(BaseModel):
@@ -21,6 +40,23 @@ class TestModel(BaseModel):
2140
simple_tuple: tuple[int, str]
2241
another_model: AnotherTestModel
2342
date_example: date
43+
test_fdatetime: FormattedDatetime
44+
45+
46+
class ChildMultiTypeModel(BaseModel):
47+
child_test_int: int
48+
49+
50+
class MultiTypeModel(BaseModel):
51+
test_str: str
52+
test_int: int
53+
test_dict: dict[str, int]
54+
test_list: list[int]
55+
test_decimal: Decimal
56+
test_date: date
57+
test_datetime: datetime
58+
test_fdatetime: FormattedDatetime
59+
test_child_model: ChildMultiTypeModel
2460

2561

2662
TEST_MODEL = TestModel(
@@ -32,7 +68,8 @@ class TestModel(BaseModel):
3268
simple_set={"a", "b", "c"},
3369
simple_tuple=(1, "wow"),
3470
another_model=AnotherTestModel(test_field="lemon"),
35-
date_example=date(2026,1,1)
71+
date_example=date(2026,1,1),
72+
test_fdatetime="2026-01-01T12:30:10",
3673
)
3774

3875

@@ -47,9 +84,29 @@ class TestModel(BaseModel):
4784
("simple_set", True),
4885
("simple_tuple", True),
4986
("another_model", True),
87+
("test_fdatetime", False),
5088
]
5189
)
5290
def test_is_field_complex(field_name: str, expected: bool):
5391
_field = TEST_MODEL.model_fields[field_name]
5492
_res = is_field_complex(_field)
5593
assert _res == expected, f"Expected {expected}. Got {_res}."
94+
95+
96+
def test_stringify_model():
97+
expected_dict = {
98+
"test_str": str,
99+
"test_int": str,
100+
"test_dict": dict[str, str],
101+
"test_list": list[str],
102+
"test_decimal": str,
103+
"test_date": str,
104+
"test_datetime": str,
105+
"test_fdatetime": str,
106+
"test_child_model": {
107+
"child_test_int": str
108+
},
109+
}
110+
serialised_model_result = model_to_schema(stringify_model(MultiTypeModel))
111+
112+
assert expected_dict == serialised_model_result

0 commit comments

Comments
 (0)