djangorestframework-stubs icon indicating copy to clipboard operation
djangorestframework-stubs copied to clipboard

ReturnDict annotations incorrect

Open 09F911029D74E35BD84156C5635688C0 opened this issue 3 years ago • 0 comments

Bug report

What's wrong

The stubs say that ReturnDict has serializer as first argument, but actually it's a keyword only argument. First argument should be a Mapping. (or whatever parent OrderedDict takes).

from rest_framework.utils.serializer_helpers import ReturnDict
from rest_framework.serializers import Serializer

d = ReturnDict({"key": "val"}, serializer=Serializer())

produces:

test.py:5: error: "ReturnDict" gets multiple values for keyword argument "serializer"  [misc]
test.py:5: error: Argument 1 to "ReturnDict" has incompatible type "Dict[str, str]"; expected "BaseSerializer[Any]"  [arg-type]

How is that should be

It should not give an error. That's the usage in drf itself

https://github.com/encode/django-rest-framework/blob/3.13.1/rest_framework/utils/serializer_helpers.py#L16 https://github.com/encode/django-rest-framework/blob/3.13.1/rest_framework/serializers.py#L553

@property
def data(self):
    ret = super().data
    return ReturnDict(ret, serializer=self)

System information

  • django-stubs version: 1.5.0
  • django-rest-framework version: 3.13.1