Skip to content

Commit

Permalink
Don't allow float(quantity) and int(quantity)
Browse files Browse the repository at this point in the history
  • Loading branch information
twmr committed Aug 25, 2017
1 parent acba339 commit 3cb1953
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion quantities/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def __setitem__(self, key, value):
value = Quantity(value)
if self._dimensionality != value._dimensionality:
value = value.rescale(self._dimensionality)
self.magnitude[key] = value
self.magnitude[key] = value.magnitude

@with_doc(np.ndarray.__lt__)
@wrap_comparison
Expand Down Expand Up @@ -413,6 +413,12 @@ def __ge__(self, other):
def __gt__(self, other):
return self.magnitude > other

def __float__(self):
raise TypeError("Quantities can't be converted to floats")

def __int__(self):
raise TypeError("Quantities can't be converted to ints")

#I don't think this implementation is particularly efficient,
#perhaps there is something better
@with_doc(np.ndarray.tolist)
Expand Down
10 changes: 10 additions & 0 deletions quantities/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def test_compound_reduction(self):
delta=1e83
)

def test_float_conversion(self):
with self.assertRaisesRegexp(
TypeError, "Quantities can't be converted to floats"):
float(3 * pq.um)

def test_int_conversion(self):
with self.assertRaisesRegexp(
TypeError, "Quantities can't be converted to ints"):
int(3 * pq.um)


class TestDefaultUnits(TestCase):

Expand Down

0 comments on commit 3cb1953

Please sign in to comment.