Skip to content

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

@Phil-Barber

Description

@Phil-Barber

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions