Allow type checkers to make serializers generic (#7385)

This commit is contained in:
Anton Agestam 2020-07-07 11:05:36 +02:00 committed by GitHub
parent d46d5cbaaa
commit 76232437d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View File

@ -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):
""" """

View File

@ -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):

View File

@ -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:
""" """