[Django] #26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes

27 views
Skip to first unread message

Django

unread,
Jan 3, 2016, 7:36:36 AM1/3/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
--------------------------------+--------------------
Reporter: vladimirbright | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------
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()

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26026>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 3, 2016, 8:29:42 AM1/3/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
--------------------------------+--------------------------------------
Reporter: vladimirbright | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by vladimirbright):

* needs_better_patch: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:1>

Django

unread,
Jan 3, 2016, 1:42:34 PM1/3/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* component: Uncategorized => Database layer (models, ORM)
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

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)`.

--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:2>

Django

unread,
Jan 4, 2016, 12:13:20 PM1/4/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: assigned

Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by andersonresende):

* status: new => assigned
* owner: nobody => andersonresende


--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:3>

Django

unread,
Jan 4, 2016, 9:52:33 PM1/4/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: assigned
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* version: 1.9 => master


--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:4>

Django

unread,
Jan 5, 2016, 9:53:25 PM1/5/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by andersonresende):

https://github.com/django/django/pull/5935

--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:5>

Django

unread,
Jan 5, 2016, 9:53:44 PM1/5/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by andersonresende):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:6>

Django

unread,
Jan 5, 2016, 10:00:37 PM1/5/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* needs_better_patch: 0 => 1


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:7>

Django

unread,
Jan 5, 2016, 10:53:30 PM1/5/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by andersonresende):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:8>

Django

unread,
Jan 7, 2016, 10:58:49 AM1/7/16
to django-...@googlegroups.com
#26026: EmptyQuerySet isinstance check broken with not QuerySet datatypes
-------------------------------------+-------------------------------------
Reporter: vladimirbright | Owner:
| andersonresende
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"b5f8c81ce18528ecf7cb4605dca864c961da9373" b5f8c81c]:
{{{
#!CommitTicketReference repository=""
revision="b5f8c81ce18528ecf7cb4605dca864c961da9373"
Fixed #26026 -- Fixed isinstance crash comparing EmptyQuerySet to non-
QuerySet.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26026#comment:9>

Reply all
Reply to author
Forward
0 new messages