[ python-Bugs-1355842 ] Incorrect Decimal-float behavior for += and *=

SourceForge.net noreply at sourceforge.net
Thu Dec 22 18:10:53 CET 2005


Bugs item #1355842, was opened at 2005-11-13 08:17
Message generated for change (Comment added) made by facundobatista
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1355842&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Connelly (connelly)
Assigned to: Facundo Batista (facundobatista)
Summary: Incorrect Decimal-float behavior for += and *=

Initial Comment:
The += and *= operators have strange behavior when the
LHS is a Decimal and the RHS is a float (as of
2005-11-13 CVS decimal.py).

Example:

>>> d = Decimal('1.02')
>>> d += 2.1
>>> d
NotImplemented

A blatant violation of "Errors should never pass silently."

Also, a bad error description is produced for the *=
operator:

>>> d = Decimal('1.02')
>>> d *= 2.9
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can't multiply sequence by non-int


----------------------------------------------------------------------

>Comment By: Facundo Batista (facundobatista)
Date: 2005-12-22 14:10

Message:
Logged In: YES 
user_id=752496

Regarding problem 1:

Nick also detected this behaviour, back in March
(http://mail.python.org/pipermail/python-dev/2005-March/051834.html),
in python-dev discussions about how integrate better the
Decimal behaviour into Python framework.

Even knowing this, Raymond Hettinger and I made a patch
(almost exactly the same), and corrected another behaviour.
Will this issue be resolved somewhen? Raymond said that this
problem is also present in sets.py and datetime objects
(http://mail.python.org/pipermail/python-dev/2005-March/051825.html),
and that should be addressed in a larger context than decimal.

As Neil Schemenauer proposed
(http://mail.python.org/pipermail/python-dev/2005-March/051829.html),
Decimal now returns NotImplemented instead of raise
TypeError, which should be the correct way to deal with
operation capabilities in the numbers.

And look at this:

>>> d
Decimal("1")   # using decimal.py rev. 39328 from svn
>>> d + 1.2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'Decimal' and
'float'
>>> d += 1.2
>>> d
NotImplemented
>>>

Why this happens? Really don't know, it's beyond my actual
knowledge, I'll keep searching. But I'm not so sure that
this is a Decimal issue.


Regarding problem 2:

I'll fix that.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2005-12-22 02:52

Message:
Logged In: YES 
user_id=33168

Facundo, can you look into this?  Are you still working on
Decimal?

----------------------------------------------------------------------

Comment By: Connelly (connelly)
Date: 2005-12-02 03:17

Message:
Logged In: YES 
user_id=1039782

The += and *= operations also give the same strange behavior
when the LHS is a Decimal and the RHS is str or unicode:

>>> d = Decimal("1.0")
>>> d += "5"
>>> d
NotImplemented

>>> d = Decimal("1.0")
>>> d *= "1.0"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can't multiply sequence by non-int


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2005-11-14 01:43

Message:
Logged In: YES 
user_id=33168

Hmmm.  __add__ returns NotImplemented which works with
classic classes, but not new-style classes.  I wonder if
NotImplementedError is supposed to be raised for new-style
classes.  

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1355842&group_id=5470


More information about the Python-bugs-list mailing list