django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

MultipleObjectMixin and SingleObjectMixin has incompatible types for queryset and context_object_name

Open PetrDlouhy opened this issue 3 years ago • 1 comments

Bug report

What's wrong

I have view class in django-hordak defined as:

class AccountTransactionsView(LoginRequiredMixin, SingleObjectMixin, ListView):
    ...

Which gives the following mypy errors:

# mypy .
hordak/views/accounts.py:86: error: Definition of "context_object_name" in base class "SingleObjectMixin" is incompatible with definition in base class "MultipleObjectMixin"
hordak/views/accounts.py:86: error: Definition of "queryset" in base class "SingleObjectMixin" is incompatible with definition in base class "MultipleObjectMixin"
Found 2 errors in 1 file (checked 56 source files)

How is that should be

Types for queryset and context_object_name should be made less strict to be compatible similar to solution of issue #152

System information

  • OS: Ubuntu
  • python version: 3.9.7
  • django version: 4.0.2
  • mypy version: 0.931
  • django-stubs version: 1.9.0
  • django-stubs-ext version: 0.3.1

PetrDlouhy avatar Mar 15 '22 12:03 PetrDlouhy

Probably I misunderstand something, but it looks like unsafe code. SingleObjectMixin.queryset is a queryset to get_object from, it should be QuerySet[Account] (if it is "view listing transactions for account"). MultipleObjectMixin.queryset is queryset you operate on (e.g. render in ListView) - it should be QuerySet[Transaction]. These two bases are really incompatible.

sterliakov avatar Apr 02 '22 23:04 sterliakov