typeddjango / djangorestframework-stubs

PEP-484 stubs for django-rest-framework
MIT License
454 stars 118 forks source link

expression has type "SlugRelatedField[<nothing>]" #168

Open Phil-Barber opened 3 years ago

Phil-Barber commented 3 years ago

I have the following serializer:

class ReportConfigurationSerializer(serializers.ModelSerializer[ReportScheduleConfiguration]):
    created_by = serializers.SlugRelatedField(read_only=True, slug_field="email")
    generation_settings = ReportGenerationSettingsJSONField(read_only=True)
    send_time_zone = TimeZoneSerializerField()

    class Meta:
        model = ReportScheduleConfiguration
        fields = (
            "id",
            "name",
            "send_time",
            "send_time_zone",
            "recipients",
            "report_type",
            "generation_settings",
        )

I would expect that mypy can detect the type of created_by, instead I have the error: Need type annotation for 'created_by' [var-annotated]

If I add a type annotation just for example eg: created_by: str = serializers.SlugRelatedField(read_only=True, slug_field="email") The error updates to: Incompatible types in assignment (expression has type "SlugRelatedField[<nothing>]", variable has type "str") [assignment]

I'm reasonably unfamiliar with typing, so trying to decipher what <nothing> meant took me forever as its not documented currently. My understanding is that somewhere in trying to decipher the type for SlugRelatedField mypy believes some code is undefined and so exits early. I believe this to be a problem with this library rather than my implementation, but I am more than happy to provide additional details!

Libraries

name = "djangorestframework", version = "3.12.4" name = "djangorestframework-stubs", version = "1.4.0" name = "django", version = "2.2.24" Python 3.9.7

intgr commented 3 years ago

Here's a testable example exhibiting the behavior:

from rest_framework import serializers
from django.contrib.auth.models import User, Group

class TestSerializer(serializers.ModelSerializer):
    username = serializers.CharField(max_length=150)
    group_ids = serializers.PrimaryKeyRelatedField(many=True, source="groups", read_only=True)
    group_names = serializers.SlugRelatedField(many=True, slug_field="name", source="groups", read_only=True)

    class Meta:
        model = User
        fields = ["username", "group_ids", "group_names"]

Output:

main:6: error: Need type annotation for 'group_ids'
main:7: error: Need type annotation for 'group_names'

What I don't understand is, why is it complaining about the group_ids, group_names fields but not username?

Anyway, one way to shut it up is just annotate it as field type:

from rest_framework.fields import Field
...
    group_ids: Field = serializers.PrimaryKeyRelatedField(many=True, source="groups", read_only=True)

or if you need a more precise type, you can quote it and hint the type vars too:

    group_ids: "PrimaryKeyRelatedField[Group, ...]" = PrimaryKeyRelatedField(many=True, source="groups", read_only=True)
sobolevn commented 3 years ago

Thanks a lot, this is a big help. PR is more than welcome! 👍

intgr commented 3 years ago

I can't submit a PR because my "fix" (more like workaround) can only be applied in user code. 😄

Do you have any ideas why CharField and similar simple fields don't cause this "Need type annotation" inspection, but RelatedFields do?

I skimmed the type definitions and plugin code, and couldn't figure it out. Getting an answer to that would probably help solve it.

niall-byrne commented 2 years ago

I encountered this same problem with StringRelatedField... is this a workaround or the intended pattern?

contributors: "serializers.StringRelatedField[Artist]" =\
    serializers.StringRelatedField(many=True)
sobolevn commented 2 years ago

This totally can be improved.

pablobuenaposada commented 1 year ago

nothing better to fix this?

sparrowt commented 1 year ago

Hitting this too - sadly we just have # type: ignore on the end of the line for all SlugRelatedField :(

ysmolski commented 11 months ago

This bug is rather frustrating.