Skip to content

Commit d53a008

Browse files
committed
fix: 🐛 Ensure UnionType is handled correctly on Python >= 3.10
1 parent 9f9a4fb commit d53a008

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

pydantic_partial/partial.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def _partial_annotation_arg(field_name_: str, field_annotation: type) -> type:
9595
if recursive or sub_fields_requested:
9696
field_annotation_origin = get_origin(field_annotation)
9797
if field_annotation_origin in (Union, UnionType, tuple, list, set, dict):
98+
if field_annotation_origin is UnionType:
99+
field_annotation_origin = Union
98100
field_annotation = field_annotation_origin[ # type: ignore
99101
tuple( # type: ignore
100102
_partial_annotation_arg(field_name, field_annotation_arg)

tests/test_partial_without_mixin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import json
1+
import sys
22
from typing import Union
33

44
import pydantic
@@ -24,6 +24,13 @@ class SomethingWithMixin(PartialModelMixin, pydantic.BaseModel):
2424
name: str
2525

2626

27+
class SomethingWithUnionTypes(PartialModelMixin, pydantic.BaseModel):
28+
name_as_union: Union[str, int]
29+
30+
if sys.version_info >= (3, 10):
31+
name_as_uniontype: str | int
32+
33+
2734
def test_setup_is_sane():
2835
assert _field_is_required(Something, "name") is True
2936
assert _field_is_required(Something, "age") is True
@@ -67,3 +74,7 @@ def test_partial_class_name_can_be_overridden():
6774
partial_cls_name = "SomethingWithOptionalName"
6875
SomethingWithOptionalName = create_partial_model(Something, "name", partial_cls_name=partial_cls_name)
6976
assert SomethingWithOptionalName.__name__ == partial_cls_name
77+
78+
79+
def test_recursive_on_unions_work(): # see https://github.com/team23/pydantic-partial/issues/52
80+
create_partial_model(SomethingWithUnionTypes, recursive=True)

tox.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ envlist =
1010
[testenv]
1111
deps =
1212
pytest
13+
fastapi
14+
anyio
15+
httpx
16+
trio
1317
commands = pytest

0 commit comments

Comments
 (0)