Skip to content

Commit c3d5ff1

Browse files
committed
Make mypy happy; change default arg list; udpate tests
1 parent 43fd4d4 commit c3d5ff1

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

pydantic_forms/utils/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def merge_json_schema(source_type: Any, target_type: Any) -> Any:
2121
source_schema = source_field_info.json_schema_extra
2222
target_schema = target_field_info.json_schema_extra
2323

24-
source_schema.update(target_schema)
24+
source_schema.update(target_schema) # type: ignore[union-attr,arg-type]
2525
return source_type

pydantic_forms/validators/components/read_only.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@ def _get_read_only_schema(default: Any) -> dict:
4646
return {"uniforms": {"disabled": True, "value": default}, "type": _get_json_type(default)}
4747

4848

49-
def read_only_list(default: list[Any]) -> Any:
49+
def read_only_list(default: list[Any] | None = None) -> Any:
5050
"""Create type with json schema of type array that is 'read only'."""
51+
if not isinstance(default, list):
52+
raise ValueError("Default argument must be a list")
53+
54+
# Empty list is valid, but needs a type
5155
if len(default) == 0:
52-
raise ValueError("Default list object must not be empty")
56+
default_item_type: Any = type(str)
5357

54-
item_types = {type(item) for item in default}
55-
if len(item_types) != 1:
56-
raise TypeError("All items in read_only_list must be of same type")
58+
else:
59+
item_types = {type(item) for item in default}
60+
if len(item_types) != 1:
61+
raise TypeError("All items in read_only_list must be of same type")
5762

58-
default_item_type: Any = list(item_types)[0]
59-
if default_item_type is type(None):
60-
raise TypeError("read_only_list item type cannot be 'NoneType'")
63+
default_item_type = list(item_types)[0]
64+
if default_item_type is type(None):
65+
raise TypeError("read_only_list item type cannot be 'NoneType'")
6166

6267
json_schema = _get_read_only_schema(default)
6368

tests/unit_tests/test_read_only_field.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import json
22
from uuid import UUID
33

4-
from more_itertools import first
54
from pydantic.config import JsonDict
65
import pytest
76
from pydantic import BaseModel, ValidationError
87

98
from pydantic_forms.core import FormPage
109
from pydantic_forms.types import strEnum
1110
from pydantic_forms.validators import read_only_field, read_only_list, LongText, OrganisationId
12-
from pydantic_forms.utils.schema import merge_json_schema, _get_field_info_with_schema
11+
from pydantic_forms.utils.schema import merge_json_schema
1312

1413

1514
class TestEnum(strEnum):
@@ -108,10 +107,20 @@ class Form(FormPage):
108107

109108

110109
def test_read_only_field_list_with_empty_default_raises_error():
111-
with pytest.raises(ValueError, match="Default list object must not be empty"):
110+
with pytest.raises(ValueError, match="Default argument must be a list"):
112111

113112
class Form(FormPage):
114-
read_only: read_only_list([])
113+
read_only: read_only_list()
114+
115+
116+
def test_read_only_list_empty_sets_default_to_array_string():
117+
class Form(FormPage):
118+
read_only_list: read_only_list([])
119+
120+
validated = Form(read_only_list=[])
121+
read_only_schema = validated.model_json_schema()["properties"]["read_only_list"]
122+
assert read_only_schema["items"]["type"] == "string"
123+
assert read_only_schema["default"] == []
115124

116125

117126
def test_read_only_field_list_with_mixed_types_raises_error():
@@ -227,3 +236,15 @@ def test_merge_json_schema():
227236

228237
with pytest.raises(TypeError, match="Target type has no json_schema_extra"):
229238
merge_json_schema(OrganisationId, test_uuid1)
239+
240+
241+
def test_stringy_types_import_works():
242+
import importlib
243+
244+
with pytest.MonkeyPatch.context() as mock:
245+
from pydantic_forms.validators.components import read_only
246+
247+
mock.setattr(read_only.sys, "version_info", (3, 9))
248+
importlib.reload(read_only)
249+
250+
assert read_only.STRINGY_TYPES == (strEnum, UUID)

0 commit comments

Comments
 (0)