mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Allow type checkers to make serializers generic (#7385)
This commit is contained in:
parent
d46d5cbaaa
commit
76232437d4
|
@ -121,6 +121,10 @@ class BaseSerializer(Field):
|
||||||
return cls.many_init(*args, **kwargs)
|
return cls.many_init(*args, **kwargs)
|
||||||
return super().__new__(cls, *args, **kwargs)
|
return super().__new__(cls, *args, **kwargs)
|
||||||
|
|
||||||
|
# Allow type checkers to make serializers generic.
|
||||||
|
def __class_getitem__(cls, *args, **kwargs):
|
||||||
|
return cls
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def many_init(cls, *args, **kwargs):
|
def many_init(cls, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import inspect
|
import inspect
|
||||||
import pickle
|
import pickle
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
from collections import ChainMap
|
from collections import ChainMap
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
|
||||||
|
@ -204,6 +205,13 @@ class TestSerializer:
|
||||||
exceptions.ErrorDetail(string='Raised error', code='invalid')
|
exceptions.ErrorDetail(string='Raised error', code='invalid')
|
||||||
]}
|
]}
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
sys.version_info < (3, 7),
|
||||||
|
reason="subscriptable classes requires Python 3.7 or higher",
|
||||||
|
)
|
||||||
|
def test_serializer_is_subscriptable(self):
|
||||||
|
assert serializers.Serializer is serializers.Serializer["foo"]
|
||||||
|
|
||||||
|
|
||||||
class TestValidateMethod:
|
class TestValidateMethod:
|
||||||
def test_non_field_error_validate_method(self):
|
def test_non_field_error_validate_method(self):
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
from django.utils.datastructures import MultiValueDict
|
from django.utils.datastructures import MultiValueDict
|
||||||
|
@ -55,6 +57,13 @@ class TestListSerializer:
|
||||||
assert serializer.is_valid()
|
assert serializer.is_valid()
|
||||||
assert serializer.validated_data == expected_output
|
assert serializer.validated_data == expected_output
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
sys.version_info < (3, 7),
|
||||||
|
reason="subscriptable classes requires Python 3.7 or higher",
|
||||||
|
)
|
||||||
|
def test_list_serializer_is_subscriptable(self):
|
||||||
|
assert serializers.ListSerializer is serializers.ListSerializer["foo"]
|
||||||
|
|
||||||
|
|
||||||
class TestListSerializerContainingNestedSerializer:
|
class TestListSerializerContainingNestedSerializer:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user