Open Phil-Barber opened 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)
Thanks a lot, this is a big help. PR is more than welcome! 👍
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 RelatedField
s 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.
I encountered this same problem with StringRelatedField... is this a workaround or the intended pattern?
contributors: "serializers.StringRelatedField[Artist]" =\
serializers.StringRelatedField(many=True)
This totally can be improved.
nothing better to fix this?
Hitting this too - sadly we just have # type: ignore
on the end of the line for all SlugRelatedField :(
This bug is rather frustrating.
I have the following serializer:
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 forSlugRelatedField
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