Skip to content

Make some mixin methods generic #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2021

Conversation

henribru
Copy link
Contributor

@henribru henribru commented Feb 1, 2021

This change makes certain mixin methods generic using a protocol for the self type. This ensures that the queryset and instance arguments for the methods have specific types based on the model instead of BaseSerializer[Any] and Model. For CreateModelMixin and UpdateModelMixin this isn't a huge deal, because you were already allowed to override BaseSerializer with e.g. BaseSerializer[MyModel] (the tests I've included for those actually pass even without the change). It can probably still be useful in some niche cases though. For DestroyModelMixin it's a big win since overriding Model with MyModel would produce an error without this change.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks!

@sobolevn sobolevn merged commit 0b2a417 into typeddjango:master Feb 1, 2021
@henribru henribru deleted the generic-mixins branch February 1, 2021 21:28
class CreateModelMixin:
def create(self, request: Request, *args: Any, **kwargs: Any) -> Response: ...
def perform_create(self, serializer: BaseSerializer) -> None: ...
def perform_create(self: UsesQuerySet[_MT], serializer: BaseSerializer[_MT]) -> None: ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I didn't know you could do that 😮 nice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@intgr mypy has a section on "Advanced self-types". Pretty cool feature! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants