Opened 6 years ago

Closed 6 years ago

#26026 closed Bug (fixed)

EmptyQuerySet isinstance check broken with not QuerySet datatypes

Reported by: Vladimir Prokhoda Owned by: Anderson Resende
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

isinstance check on any instance, that is not QuerySet (actually anything without .query attribute) and django.db.models.query.EmptyQuerySet throws AttributeError.

Code to reproduce.

In [9]: from django.db.models.query import EmptyQuerySet

In [10]: isinstance(1, EmptyQuerySet)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-79a6a197988a> in <module>()
----> 1 isinstance(1, EmptyQuerySet)

/home/vprokhoda/Envs/ADMIN2/local/lib/python2.7/site-packages/django/db/models/query.pyc in __instancecheck__(self, instance)
   1171 class InstanceCheckMeta(type):
   1172     def __instancecheck__(self, instance):
-> 1173         return instance.query.is_empty()
   1174
   1175

AttributeError: 'int' object has no attribute 'query'

In [11]:

In [11]: import django

In [12]: django.__version__
Out[12]: '1.9'

Quick and dirty fix is

(django)vprokhoda@tests$ git diff
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 45c0320..0cc5f94 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1171,7 +1171,7 @@ class QuerySet(object):

 class InstanceCheckMeta(type):
     def __instancecheck__(self, instance):
-        return instance.query.is_empty()
+        return hasattr(instance, 'query') and instance.query.is_empty()

Change History (9)

comment:1 Changed 6 years ago by Vladimir Prokhoda

Type: UncategorizedBug

comment:2 Changed 6 years ago by Simon Charette

Component: UncategorizedDatabase layer (models, ORM)
Easy pickings: set
Triage Stage: UnreviewedAccepted

I think the appropriate fix should involve making sure isinstance(instance, QuerySet) instead of checking for query attribute existence which fails if not hasattr(instance.query, 'is_empty') or not callable(instance.query.is_empty).

comment:3 Changed 6 years ago by Anderson Resende

Owner: changed from nobody to Anderson Resende
Status: newassigned

comment:4 Changed 6 years ago by Simon Charette

Version: 1.9master

comment:6 Changed 6 years ago by Anderson Resende

Has patch: set

comment:7 Changed 6 years ago by Simon Charette

Patch needs improvement: set

Left some comments on the PR, please uncheck Patch needs improvement once they are addressed.

comment:8 Changed 6 years ago by Anderson Resende

Patch needs improvement: unset

comment:9 Changed 6 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In b5f8c81c:

Fixed #26026 -- Fixed isinstance crash comparing EmptyQuerySet to non-QuerySet.

Note: See TracTickets for help on using tickets.
Back to Top