-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
expression has type "SlugRelatedField[<nothing>]" #168
Comments
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:
What I don't understand is, why is it complaining about the 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 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 |
This bug is rather frustrating. |
Remove the data type TypeVar in favor of Any. Then subclasses don't have to deal with this rule anymore. Ref typeddjango#168
Remove the data type TypeVar in favor of Any. Then subclasses don't have to deal with this rule anymore. Ref typeddjango#168
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 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
The text was updated successfully, but these errors were encountered: