Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Inet class does not work for equality check

#205

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.

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

State: invalid
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo
    • 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 Range objects cannot be sorted

    Can you provide a patch?

    April 4th, 2014 @ 04:19 PM

  2. Daniele Varrazzo
    Daniele Varrazzo

    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?

    April 4th, 2014 @ 06:39 PM

  3. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from open to invalid

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

    May 5th, 2014 @ 11:05 PM