Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow float(quantity) and int(quantity) #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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