This project is archived and is in readonly mode.

#205 ✓invalid
Psycopg website

Inet class does not work for equality check

Reported by Psycopg website | April 4th, 2014 @ 03:35 PM

Submitted by: mtaylor@arbor.net

The Inet class does not implement __eq__, and the standard "object" class seems to always return False.

python2.7:

import psycopg2, psycopg2.extras
psycopg2.__version__
  -> '2.5.2 (dt dec mx pq3 ext)'
psycopg2.extras.Inet('1.2.3.4') == psycopg2.extras.Inet('1.2.3.4')
   -> False
>>> psycopg2.extras.Inet('1.2.3.4') in [psycopg2.extras.Inet('1.2.3.4'), psycopg2.extras.Inet('1.2.5.6')]
  -> False
>>> psycopg2.extras.Inet('1.2.3.4').addr == psycopg2.extras.Inet('1.2.3.4').addr
True

Fix:

psycopg2.extras.Inet.__eq__ = lambda self, other: isinstance(other, psycopg2.extras.Inet) and self.addr == other.addr or False

psycopg2.extras.Inet.__ne__ = lambda self, other: not self.__eq__(other)
>>> psycopg2.extras.Inet('1.2.3.4') == psycopg2.extras.Inet('1.2.3.4') -> True >>> psycopg2.extras.Inet('1.2.3.4') in [psycopg2.extras.Inet('1.2.3.4'), psycopg2.extras.Inet('1.2.5.6')] -> True

I'd rather see the __eq__ being implemented, versus checking .addr equality.
FWIW, the NumericRange (ala Range) defines an __eq__ operator.

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo April 4th, 2014 @ 04:19 PM

    • State changed from “new” to “open”

    I'm expecting we should fix the comparison operators as well, for the same reason we did it recently for Range, see ticket #193

    Can you provide a patch?

  • Daniele Varrazzo

    Daniele Varrazzo April 4th, 2014 @ 06:39 PM

    Thinking again about that, I don't like the Inet object, I don't think it provides anything additional compared to just a string. So I'm tempted to avoid adding extra features and maintaining it, except for serious bugs.

    Can you provide a reason to use Inet instead of a string?

  • Daniele Varrazzo

    Daniele Varrazzo May 5th, 2014 @ 11:05 PM

    • State changed from “open” to “invalid”

    Closing as I have no interest in adding feature to a class that has no reason to exist.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="https://github.com/psycopg/psycopg2/issues">the new tracker</a>.
<br/>
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins

Pages