Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for drf ReturnList and ReturnDict type hint #917

Merged
merged 2 commits into from
Feb 21, 2023

Conversation

zengqiu
Copy link
Contributor

@zengqiu zengqiu commented Jan 16, 2023

While using SerialilzerMethodField to return ListSerializer data, the result type is ReturnList:

from rest_framework import serializers
from rest_framework.utils.serializer_helpers import ReturnList

class MySerializer(serializers.ModelSerializer):
    items = serializers.SerializerMethodField()

    class Meta:
        model = MyModel
        fields = '__all__'

    def get_items(self, obj) -> ReturnList:
        queryset = obj.items.all()[::-1]
        return ItemSerializer(queryset, many=True, read_only=True).data

And will got a waring:

Warning [MyViewSet > MySerializer]: unable to resolve type hint for function "get_items". Consider using a type hint or @extend_schema_field. Defaulting to string.

Because it can't resolve ReturnList type.

@zengqiu zengqiu changed the title Add support for drf ReturnList and ReturnDict hint Add support for drf ReturnList and ReturnDict type hint Jan 16, 2023
@codecov
Copy link

codecov bot commented Jan 20, 2023

Codecov Report

Base: 98.71% // Head: 98.71% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (2e94c28) compared to base (24b8a98).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #917   +/-   ##
=======================================
  Coverage   98.71%   98.71%           
=======================================
  Files          68       68           
  Lines        7994     7995    +1     
=======================================
+ Hits         7891     7892    +1     
  Misses        103      103           
Impacted Files Coverage Δ
drf_spectacular/plumbing.py 97.27% <100.00%> (+<0.01%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

lint: fix import
@tfranzel
Copy link
Owner

@zengqiu thank your for this excellent change. I still learning new things about DRF 😆

This is a good feature to support, but if you want ItemSerializer to be properly represented, do this instead:

@extend_schema_field(ItemSerializer(many=True))
def get_items(self, obj):
    ...

@tfranzel tfranzel merged commit 6300b12 into tfranzel:master Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants