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

TypeError: unsupported operand type(s) for *: 'float' and 'Decimal' #46

Closed
danielsokolowski opened this issue Aug 16, 2013 · 5 comments
Closed

Comments

@danielsokolowski
Copy link

I have only traced the bug on surface so I do not understand why this method is called UnitRegistry.convert(...). In a nut shell, using Decimal magnitude as opposed to float causes this method to fail as it tried to multiply Decimal and floats together - below is the stack trace.

>>> Offer(product=Product.objects.get(pk=1), amount_min='1000 kg', user=User.objects.get(pk=1), company=Company.objects.get(pk=1)).full_clean()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-ticket_20625-tailored/django/db/models/base.py", line 923, in full_clean
    self.clean_fields(exclude=exclude)
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-ticket_20625-tailored/django/db/models/base.py", line 965, in clean_fields
    setattr(self, f.attname, f.clean(raw_value, self))
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-ticket_20625-tailored/django/db/models/fields/__init__.py", line 256, in clean
    self.run_validators(value)
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-ticket_20625-tailored/django/db/models/fields/__init__.py", line 202, in run_validators
    if value in self.empty_values:
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/quantity.py", line 347, in __eq__
    return self.dimensionless and _eq(self.magnitude, other)
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/quantity.py", line 153, in dimensionless
    tmp = copy.copy(self).to_base_units()
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/quantity.py", line 210, in to_base_units
    ret.ito_base_units()
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/quantity.py", line 201, in ito_base_units
    self._magnitude = self._REGISTRY.convert(self._magnitude, self._units, other)
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/unit.py", line 674, in convert
    # Clone and put on Python path: https://github.com/tenXer/PyDevSrc, place this in your settings_development.py, and place your 'break' points in Eclipse
TypeError: unsupported operand type(s) for *: 'float' and 'Decimal'

@hgrecco
Copy link
Owner

hgrecco commented Aug 16, 2013

This was always very confusing for me but is the standad in Python 2

Python 2.7.2 |CUSTOM| (default, Jul 27 2011, 13:29:32)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
>>> decimal.Decimal(10) * 10.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'Decimal' and 'float'

and in Python 3

Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
>>> decimal.Decimal(10) * 10.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'Decimal' and 'float'

@hgrecco hgrecco closed this as completed Aug 16, 2013
danielsokolowski pushed a commit to danielsokolowski/pint that referenced this issue Aug 16, 2013
…imal magnitudes no longer throws a TypeError caused by attempting to multiply a float with Decimal. Further edge cases for other magnitude types might be required.
@danielsokolowski
Copy link
Author

Hi @hgrecco, yes I am aware of that - but the issue is with pint itself - I'll clarify below and the tentative fix is to follow:

>>> import pint
>>> from decimal import Decimal
>>> q = pint.UnitRegistry().Quantity(Decimal('1000'), 'kg')
>>> q
<Quantity(1000, 'kilogram')>
>>> q.to_base_units()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/quantity.py", line 210, in to_base_units
    ret.ito_base_units()
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/quantity.py", line 201, in ito_base_units
    self._magnitude = self._REGISTRY.convert(self._magnitude, self._units, other)
  File "/srv/www/django/development.<hidden>/src/github-danielsokolowski-django-unitofmeasure/unitofmeasure/danielsokolowski-pint/pint/unit.py", line 677, in convert
    """Builds dimensionless quantities using the Buckingham Ï theorem
TypeError: unsupported operand type(s) for *: 'float' and 'Decimal'

danielsokolowski pushed a commit to danielsokolowski/pint that referenced this issue Aug 16, 2013
…imal magnitudes no longer throws a TypeError caused by attempting to multiply a float with Decimal. Further edge cases for other magnitude types might be required.
@danielsokolowski
Copy link
Author

Here is the pull request: #48

@hgrecco
Copy link
Owner

hgrecco commented Aug 19, 2013

You are right. I should not have closed the issue. Thanks!

@hgrecco hgrecco closed this as completed Aug 19, 2013
@danielsokolowski
Copy link
Author

Sweet - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants