Skip to content

Commit

Permalink
added optional list tests
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebv committed Jan 23, 2024
1 parent 4faac21 commit 6aa096b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/drf_pydantic/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import pydantic
import pydantic.fields
import pydantic_core

from pydantic_core import PydanticUndefined, Url
from rest_framework import serializers
from rest_framework import serializers # type: ignore

from drf_pydantic.errors import FieldConversionError, ModelConversionError
from drf_pydantic.utils import get_union_members, is_scalar
Expand All @@ -27,14 +27,14 @@
# String fields
str: serializers.CharField,
pydantic.EmailStr: serializers.EmailField,
# Regex implemented as a special case
# WARN This is what pydantic converts pydantic.HttpUrl to
Url: serializers.URLField,
# * Regex implemented as a special case
# WARN pydantic converts pydantic.HttpUrl to pydantic_core.Url
pydantic_core.Url: serializers.URLField,
uuid.UUID: serializers.UUIDField,
# Numeric fields
int: serializers.IntegerField,
float: serializers.FloatField,
# Decimal implemented as a special case
# * Decimal implemented as a special case
# Date and time fields
datetime.datetime: serializers.DateTimeField,
datetime.date: serializers.DateField,
Expand Down Expand Up @@ -116,7 +116,7 @@ def _convert_field(
"required": field.is_required(),
}
_default_value = field.get_default(call_default_factory=True)
if _default_value is not PydanticUndefined:
if _default_value is not pydantic_core.PydanticUndefined:
drf_field_kwargs["default"] = _default_value

# Process constraints
Expand Down
24 changes: 23 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ class Person(BaseModel):

assert isinstance(serializer.fields["friends"], serializers.ListField)
assert isinstance(serializer.fields["friends"].child, serializers.CharField)
assert serializer.fields["friends"].allow_null is False

def test_optional_list_with_optional(self):
class Person(BaseModel):
friends: typing.Optional[list[str]]

serializer = Person.drf_serializer()

assert isinstance(serializer.fields["friends"], serializers.ListField)
assert isinstance(serializer.fields["friends"].child, serializers.CharField)
assert serializer.fields["friends"].allow_null is True

@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10+")
def test_optional_list_with_pipe(self):
class Person(BaseModel):
friends: list[str] | None # type: ignore

serializer = Person.drf_serializer()

assert isinstance(serializer.fields["friends"], serializers.ListField)
assert isinstance(serializer.fields["friends"].child, serializers.CharField)
assert serializer.fields["friends"].allow_null is True

def test_tuple_with_ellipsis(self):
class Person(BaseModel):
Expand Down Expand Up @@ -288,7 +310,7 @@ class Person(BaseModel):
@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10+")
def test_optional_type_with_pipe(self):
class Person(BaseModel):
name: str | None
name: str | None # type: ignore

serializer = Person.drf_serializer()

Expand Down

0 comments on commit 6aa096b

Please sign in to comment.