HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extending QuerySetType further

SHxKM opened this issue · comments

commented

I've adopted QuerySetType from this project's queryset_type.py and it works great for the most part. It was missing support for the logical OR opertaor (|) so I added the following method:

class QuerySetType(Generic[DjangoModel], extra=Iterable):
    ...
    def __or__(self, other) -> Optional[DjangoModel]:
        ...

I also noticed it's missing things like support for slicing (qs[:20]). Is there a deliberate reason for that? is your actual implementation using a more updated/fuller version? trying to understand if this is intentional and I should be wary of implementing it.

For example, to support slicing we may do this?

class QuerySetType(Generic[DjangoModel], extra=Iterable):
    ...
    #old
    # def __getitem__(self, index: int) -> DjangoModel: ...
    # new
    def __getitem__(self, index) -> Union[DjangoModel, "QuerySetType[DjangoModel]"]:
        ...

Hi @SHxKM
Thank you for the feedback 🙂

We don't use the logical operators | and & too much so we didn't add typings for them - it's not intentional :) . I just updated the QuerySetType.

Just one missmatch with the proposed implementation - the return type of __or__ should be QuerySetType[DjangoModel] (non-optional) since:

asd: QuerySetType[SomeModel] = SomeModel.objects.filter(...) | SomeModel.objects.filter(...)

You can see the last changes here - #20 🙂

commented

@Ivo-Donchev. Thanks. you’re right, it may return an empty QuerySet but not None.