Skip to content

Commit 3e785d7

Browse files
committed
[0.1.11] Enforce null type checks for SchemaField
Support for plain methods/arbitrary objects in `default=` argument
1 parent e378a34 commit 3e785d7

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

django_pydantic_field/fields.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,33 @@ def _deconstruct_config(self, kwargs):
148148
kwargs.update(self.export_params, config=self.config)
149149

150150

151+
if t.TYPE_CHECKING:
152+
OptSchemaT = t.Optional[base.SchemaT]
153+
151154
@t.overload
152155
def SchemaField(
153-
schema: t.Union[t.Type["base.ST"], "t.ForwardRef", str] = ...,
156+
schema: "t.Union[t.Type[base.ST], t.ForwardRef, str]" = ...,
154157
config: "base.ConfigType" = ...,
155-
default: t.Union["base.ST", t.Type[NOT_PROVIDED], None] = ...,
156-
null: t.Literal[True] = ...,
158+
default: "t.Union[OptSchemaT, t.Callable[[], OptSchemaT]]" = ...,
157159
*args,
160+
null: "t.Literal[True]",
158161
**kwargs,
159-
) -> t.Any:
162+
) -> "t.Optional[base.ST]":
160163
...
161164

162-
163165
@t.overload
164166
def SchemaField(
165-
schema: t.Union[t.Type["base.ST"], "t.ForwardRef", str] = ...,
167+
schema: "t.Union[t.Type[base.ST], t.ForwardRef, str]" = ...,
166168
config: "base.ConfigType" = ...,
167-
default: t.Union["base.ST", t.Type[NOT_PROVIDED]] = ...,
168-
null: t.Literal[False] = ...,
169+
default: "t.Union[base.SchemaT, t.Callable[[], base.SchemaT]]" = ...,
169170
*args,
171+
null: "t.Literal[False]" = ...,
170172
**kwargs,
171-
) -> t.Any:
173+
) -> "base.ST":
172174
...
173175

174176

175-
def SchemaField(
176-
schema: t.Union[t.Type["base.ST"], "t.ForwardRef", str] = None,
177-
config: "base.ConfigType" = None,
178-
default: t.Union["base.ST", t.Type[NOT_PROVIDED], None] = NOT_PROVIDED,
179-
*args,
180-
**kwargs,
181-
) -> t.Any:
177+
def SchemaField(schema=None, config=None, default=NOT_PROVIDED, *args, **kwargs) -> t.Any:
182178
kwargs.update(schema=schema, config=config, default=default)
183179
return PydanticSchemaField(*args, **kwargs)
184180

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = django-pydantic-field
3-
version = 0.1.10
3+
version = 0.1.11
44
url = https://github.com/surenkov/django-pydantic-field
55

66
description = Django JSONField with Pydantic models as a Schema

0 commit comments

Comments
 (0)