Skip to content

Commit

Permalink
fix: type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 15, 2025
1 parent cc0dd32 commit 6efdffa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
20 changes: 7 additions & 13 deletions codeforlife/serializers/model_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,10 @@ def non_none_instance(self):
"""Casts the instance to not None."""
return t.cast(t.List[AnyModel], self.instance)

@classmethod
def get_model_class(cls) -> t.Type[AnyModel]:
"""Get the model view set's class.
Returns:
The model view set's class.
"""
return get_arg(cls, 0)
@property
def model_class(self) -> t.Type[AnyModel]:
"""Shorthand to model class."""
return self.view.model_class

def __init__(self, *args, **kwargs):
instance = args[0] if args else kwargs.pop("instance", None)
Expand All @@ -95,9 +91,8 @@ def create(self, validated_data: t.List[DataDict]) -> t.List[AnyModel]:
Returns:
The models.
"""
model_class = self.get_model_class()
return model_class.objects.bulk_create( # type: ignore[attr-defined]
objs=[model_class(**data) for data in validated_data],
return self.model_class.objects.bulk_create( # type: ignore[attr-defined]
objs=[self.model_class(**data) for data in validated_data],
batch_size=self.batch_size,
)

Expand All @@ -122,8 +117,7 @@ def update(
for field, value in data.items():
setattr(model, field, value)

model_class = self.get_model_class()
model_class.objects.bulk_update( # type: ignore[attr-defined]
self.model_class.objects.bulk_update( # type: ignore[attr-defined]
objs=instance,
fields={field for data in validated_data for field in data.keys()},
batch_size=self.batch_size,
Expand Down
2 changes: 1 addition & 1 deletion codeforlife/tests/model_view_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BaseModelViewSetTestCase(

@property
def model_class(self) -> t.Type[AnyModel]:
"""Shorthand to request-user class."""
"""Shorthand to model class."""
return self.model_view_set_class.model_class

@property
Expand Down
20 changes: 9 additions & 11 deletions codeforlife/views/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,16 @@ class Meta:
# pylint: disable-next=import-outside-toplevel
from ..serializers import ModelListSerializer

model_class = self.get_model_class()

# pylint: disable-next=too-few-public-methods
class _ModelListSerializer(
ModelListSerializer[
RequestUser, model_class # type: ignore[valid-type]
]
):
pass

# TODO: delete this
# # pylint: disable-next=too-few-public-methods
# class _ModelListSerializer(
# ModelListSerializer[
# RequestUser, self.model_class # type: ignore[valid-type]
# ]
# ):
# pass
# Set list_serializer_class to default if not set.
setattr(meta, "list_serializer_class", _ModelListSerializer)
setattr(meta, "list_serializer_class", ModelListSerializer)

# Get default list_serializer_class.
serializer = super().get_serializer(*args, **kwargs)
Expand Down

0 comments on commit 6efdffa

Please sign in to comment.