Skip to content

Commit

Permalink
Merge pull request #215 from kwmsmith/bugfix/expand-equality
Browse files Browse the repository at this point in the history
Generalize `Mono.__eq__` to handle equality among subclasses.
  • Loading branch information
kwmsmith committed May 6, 2016
2 parents dce9474 + 99b24c2 commit 2bdf3ea
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
6 changes: 4 additions & 2 deletions datashape/coretypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def info(self):
return type(self), self.parameters

def __eq__(self, other):
return type(self) == type(other) and self.info() == other.info()
return (isinstance(other, Mono) and
self.shape == other.shape and
self.measure.info() == other.measure.info())

def __ne__(self, other):
return not self.__eq__(other)
Expand All @@ -96,7 +98,7 @@ def __hash__(self):
try:
h = self._hash
except AttributeError:
h = self._hash = hash(self.info())
h = self._hash = hash(self.shape) ^ hash(self.measure.info())
return h

@property
Expand Down
16 changes: 15 additions & 1 deletion datashape/tests/test_coretypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from datashape import (dshape, to_numpy_dtype, from_numpy, error, Units,
uint32, Bytes, var, timedelta_, datetime_, date_,
float64, Tuple, to_numpy)
float64, Tuple, to_numpy, string)
from datashape.py2help import unicode, OrderedDict


Expand Down Expand Up @@ -649,3 +649,17 @@ def test_unicode_record_names(names, typ):
assert record.names == names
assert record.types == types
assert all(isinstance(s, string_type) for s in record.names)


equiv_dshape_pairs = [(dshape('?string'), Option('string')),
(dshape('string'), string),
(dshape('datetime'), datetime_),
(dshape('?datetime'), Option(datetime_)),
(dshape('10 * int32'), 10 * int32),
(dshape('var * ?int32'), var * Option(int32)),
(dshape('10 * ?float64'), Fixed(10) * Option(float64))]

@pytest.mark.parametrize('a,b', equiv_dshape_pairs)
def test_hash_and_eq_consistency(a, b):
assert a == b
assert hash(a) == hash(b)
43 changes: 42 additions & 1 deletion datashape/tests/test_promote.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest

from datashape import (promote, Option, float64, int64, float32, optionify,
string, datetime_ as datetime)
string, datetime_ as datetime, dshape)


def test_simple():
x = int64
Expand Down Expand Up @@ -53,6 +54,26 @@ def test_option_in_parent():
[Option(string),
string,
False,
string],
[Option(string),
dshape('?string'),
True,
Option(string)],
[dshape('?string'),
Option(string),
False,
Option(string)],
[dshape('string'),
Option(string),
True,
Option(string)],
[dshape('string'),
Option(string),
False,
string]])
def test_promote_string_with_option(x, y, p, r):
assert (promote(x, y, promote_option=p) ==
Expand Down Expand Up @@ -82,6 +103,26 @@ def test_promote_string_with_option(x, y, p, r):
[Option(datetime),
datetime,
False,
datetime],
[Option(datetime),
dshape('?datetime'),
True,
Option(datetime)],
[dshape('?datetime'),
Option(datetime),
False,
Option(datetime)],
[dshape('datetime'),
Option(datetime),
True,
Option(datetime)],
[dshape('datetime'),
Option(datetime),
False,
datetime]])
def test_promote_datetime_with_option(x, y, p, r):
assert (promote(x, y, promote_option=p) ==
Expand Down
42 changes: 42 additions & 0 deletions docs/source/whatsnew/0.5.3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Release |version|
-----------------

:Release: |version|
:Date: TBD

New Features
------------

None

New Types
---------

None

Experimental Types
------------------

.. warning::

Experimental types are subject to change.

None

API Changes
-----------

None

Bug Fixes
---------

* Fixes :func:`~coretypes.Mono.__eq__` to ensure equality among
:class:`~coretypes.Mono` subclasses compare equal when it makes sense to do so.
For instance ``dshape("?string") == Option(string)`` now holds true
(:issue:`214`).

Miscellaneous
-------------

None

0 comments on commit 2bdf3ea

Please sign in to comment.