Django QuerySet does not implement __contains__
but Stubs does
#1924
Labels
bug
Something isn't working
__contains__
but Stubs does
#1924
What's wrong
Stubs implement
__contains__
forQuerySet
but Django does not.How is that should be
Django does not implement
__contains__
forQuerySet
. It relies on the fallback described in the docs at https://docs.python.org/3/reference/expressions.html#membership-test-details. I do not know why this was introduced in #33. It is not mentioned there.This Minimal example proves that this is not necessary to type it that way.
Example
does not implement__contains__
butin
is usable. So42 in myQuerySet
will work butmyQuerySet.__contains__(42)
not, because it is not defined (you can check withdir
).QuerySet
will be type checked correctly by mypy but will not have the same behavior at Runtime becauseQuerySet
does not implement__contains__
. A DjangoQuerySet
is not a container because it does not implement__contains__
as described athttps://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes and https://mypy.readthedocs.io/en/stable/protocols.html#container-t. The docs are clear there. No
__contains__
noContainer
. A static type checker will think that a Django StubsQuerySet
is aContainer
but a runtime type checker will not recognize a DjangoQuerySet
as a Container. Also, using__contains__(42)
will be allowed by Django Stubs but fail on Django at runtime.Why
__contains__
is not implemented withcontains
forQuerySet
in Django, I do not know. But since this project is for typing Django, this is on their site to do. I am planning to open an issue there as well.System information
python
: 3.10django
: 4.2.7mypy
: 1.8django-stubs
: 4.2.6django-stubs-ext
: NoneThe text was updated successfully, but these errors were encountered: