I'm trying to use the FieldMixin
from this answer in my project, but I'm having trouble getting it to pass mypy
checks. The current code:
class DynamicFieldsMixin(Serializer):
context: Dict[str, Any]
def get_field_names(
self, declared_fields: OrderedDict, info: FieldInfo
) -> Set[str]:
field_names: Set[str] = self.context.get(
"fields",
super().get_field_names(declared_fields, info)
)
return field_names
Inheriting from rest_framework.serializers.Serializer
seems weird, and I wonder if there's a way to avoid that, since it's meant to be mixed into actual Serializer
classes. Just removing the superclass results in this error:
error: "get_field_names" undefined in superclass
mypy
configuration:
[mypy]
check_untyped_defs = true
disallow_untyped_defs = true
ignore_missing_imports = true
no_implicit_optional = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_ignores = true