typeddjango / djangorestframework-stubs

PEP-484 stubs for django-rest-framework
MIT License
450 stars 117 forks source link

Assignment error on serializer fields named as Field properties #158

Open oystein-beaufort opened 3 years ago

oystein-beaufort commented 3 years ago

When some of the fields in a ModelSerializer (but probably any serializer) has the same name as the annotated properties of the class Field in rest_framework-stubs.fields.pyi, e.g. source, required, etc., mypy raises an assignment error.

Example:

class MatchSerializer(serializers.ModelSerializer):
    source = MatchSourceSerializer()
    ...

This results in the following mypy error:

Incompatible types in assignment (expression has type "MatchSourceSerializer", base class "Field" defined the type as "Union[Callable[..., Any], str, None]")

If I change the type of source in fields.pyi like this:

class Field(Generic[_VT, _DT, _RP, _IN]):
    ...
    source: Literal["example"]  # Optional[Union[Callable, str]]
    ...

mypy now raises the following error

Incompatible types in assignment (expression has type "MatchSourceSerializer", base class "Field" defined the type as "Literal['example']")

System information

NOTE: This is similar to https://github.com/typeddjango/djangorestframework-stubs/issues/78, but not the same issue

hongquan commented 2 years ago

Any workaround for this issue?

oystein-beaufort commented 2 years ago

Current workaround: # type: ignore ;)