Debian Bug report logs - #653177
Model __eq__ does not compare correctly between normal objects and deferred objects

version graph

Package: python-django; Maintainer for python-django is Debian Python Modules Team <python-modules-team@lists.alioth.debian.org>; Source for python-django is src:python-django.

Reported by: Enrico Zini <enrico@debian.org>

Date: Sat, 24 Dec 2011 17:57:05 UTC

Severity: normal

Tags: confirmed, upstream

Found in version python-django/1.3.1-3

Forwarded to https://code.djangoproject.com/ticket/11892

Reply or subscribe to this bug.

Toggle useless messages

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, Chris Lamb <lamby@debian.org>:
Bug#653177; Package python-django. (Sat, 24 Dec 2011 17:57:08 GMT) Full text and rfc822 format available.

Acknowledgement sent to Enrico Zini <enrico@debian.org>:
New Bug report received and forwarded. Copy sent to Chris Lamb <lamby@debian.org>. (Sat, 24 Dec 2011 17:57:08 GMT) Full text and rfc822 format available.

Message #5 received at submit@bugs.debian.org (full text, mbox):

From: Enrico Zini <enrico@debian.org>
To: Debian Bug Tracking System <submit@bugs.debian.org>
Subject: Model __eq__ does not compare correctly between normal objects and deferred objects
Date: Sat, 24 Dec 2011 18:55:12 +0100
Package: python-django
Version: 1.3.1-3
Severity: normal

Hello,

thank you for packaging django.

I noticed that there is a problem with the default model object __eq__
implementation when working with deferred objects.

The default implementation should be something like:

	def __eq__(self, other):
	    return isinstance(other, self.__class__) and self._get_pk_val() == other._get_pk_val() 

Now, given a simple model...

	class Package(models.Model):
		name = models.TextField(null=False, unique=True)
		# Lots of other fields

...this code will show the problem:

	# Get a normal object
        p = Package.objects.get(name="debtags")
	# Get a deferred object
	dp = Package.objects.filter(name="debtags").only("name")[0]
	print object.__repr__(p), hash(p)
	print object.__repr__(dp), hash(dp)
	# This assert succeeds
	assert hash(p) == hash(dp)
	# This assert fails
	assert p == dp

Its output is:

	<Package: debtags> 11
	<Package_Deferred_ldesc_popcon_sdesc_source_version: debtags> 11

It looks like the isinstance bit of the default __eq__ model is failing
here.

This causes suprising behaviour:

	allpkgs = set(Package.objects.all().only("name"))
	# This assert fails
	assert Package.objects.get(name="debtags") in allpkgs

I'll now work around this by implementing my own version of __eq__
methods in my model objects. Something like this works:

	def __eq__(self, other):
	    return self.name == other.name

although I'll have to pay attention not to end up with code that
compares Packages with something else.


Ciao,

Enrico


-- System Information:
Debian Release: wheezy/sid
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: amd64 (x86_64)

Kernel: Linux 3.1.0-1-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_GB.UTF-8, LC_CTYPE=en_GB.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages python-django depends on:
ii  python     2.7.2-9
ii  python2.6  2.6.7-4
ii  python2.7  2.7.2-8

Versions of packages python-django recommends:
ii  libjs-jquery  1.7-1

Versions of packages python-django suggests:
ii  geoip-database-contrib  <none>
ii  python-flup             <none>
ii  python-mysqldb          <none>
ii  python-psycopg          <none>
ii  python-psycopg2         2.4.2-1
ii  python-sqlite           1.0.1-8
ii  python-yaml             3.10-2

-- no debconf information




Information forwarded to debian-bugs-dist@lists.debian.org, Chris Lamb <lamby@debian.org>:
Bug#653177; Package python-django. (Tue, 27 Dec 2011 09:15:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to Raphael Hertzog <hertzog@debian.org>:
Extra info received and forwarded to list. Copy sent to Chris Lamb <lamby@debian.org>. (Tue, 27 Dec 2011 09:15:05 GMT) Full text and rfc822 format available.

Message #10 received at 653177@bugs.debian.org (full text, mbox):

From: Raphael Hertzog <hertzog@debian.org>
To: Enrico Zini <enrico@debian.org>, 653177@bugs.debian.org
Subject: Re: Bug#653177: Model __eq__ does not compare correctly between normal objects and deferred objects
Date: Tue, 27 Dec 2011 10:12:31 +0100
forwarded 653177 https://code.djangoproject.com/ticket/17466
thanks

On Sat, 24 Dec 2011, Enrico Zini wrote:
> I noticed that there is a problem with the default model object __eq__
> implementation when working with deferred objects.

I forwarded your report upstream and I took the liberty to CC you as
you're probably more qualified than me to answer upstream's queries, if
any.

Cheers,
-- 
Raphaël Hertzog ◈ Debian Developer

Pre-order a copy of the Debian Administrator's Handbook and help
liberate it: http://debian-handbook.info/liberation/




Set Bug forwarded-to-address to 'https://code.djangoproject.com/ticket/17466'. Request was from Raphael Hertzog <hertzog@debian.org> to control@bugs.debian.org. (Tue, 27 Dec 2011 09:15:22 GMT) Full text and rfc822 format available.

Changed Bug forwarded-to-address to 'https://code.djangoproject.com/ticket/11892' from 'https://code.djangoproject.com/ticket/17466' Request was from Raphaël Hertzog <hertzog@debian.org> to control@bugs.debian.org. (Tue, 27 Dec 2011 13:30:08 GMT) Full text and rfc822 format available.

Added tag(s) upstream and confirmed. Request was from Raphaël Hertzog <hertzog@debian.org> to control@bugs.debian.org. (Tue, 27 Dec 2011 13:30:09 GMT) Full text and rfc822 format available.

Information forwarded to debian-bugs-dist@lists.debian.org, Chris Lamb <lamby@debian.org>:
Bug#653177; Package python-django. (Wed, 06 Nov 2013 15:48:04 GMT) Full text and rfc822 format available.

Acknowledgement sent to Luke Faraone <lfaraone@debian.org>:
Extra info received and forwarded to list. Copy sent to Chris Lamb <lamby@debian.org>. (Wed, 06 Nov 2013 15:48:04 GMT) Full text and rfc822 format available.

Message #21 received at 653177@bugs.debian.org (full text, mbox):

From: Luke Faraone <lfaraone@debian.org>
To: 653177@bugs.debian.org
Subject: Fixed upstream
Date: Wed, 6 Nov 2013 10:45:27 -0500
[Message part 1 (text/plain, inline)]
Just a FYI, this bug appears to be fixed upstream and the commit[1] will
be included in the 1.7 release.

[1]: https://github.com/django/django/commit/4668c142dce77c6f29fb75532c1acfa1b2d322ff

-- 
Luke Faraone;; Debian & Ubuntu Developer; Sugar Labs, Systems
lfaraone on irc.[freenode,oftc].net -- http://luke.faraone.cc
PGP fprint: 5189 2A7D 16D0 49BB 046B DC77 9732 5DD8 F9FD D506
[signature.asc (application/pgp-signature, inline)]

Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Thu Apr 24 20:10:07 2014; Machine Name: beach.debian.org

Debian Bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.