expression has type "SlugRelatedField[<nothing>]"
See original GitHub issueI 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Here’s a testable example exhibiting the behavior:
Output:
What I don’t understand is, why is it complaining about the
group_ids
,group_names
fields but notusername
?Anyway, one way to shut it up is just annotate it as field type:
or if you need a more precise type, you can quote it and hint the type vars too:
This totally can be improved.