This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incorrect Decimal-float behavior for += and *=
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: arigo Nosy List: arigo, connelly, facundobatista, georg.brandl, lemburg, nnorwitz
Priority: normal Keywords:

Created on 2005-11-13 11:17 by connelly, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (9)
msg26857 - (view) Author: Connelly (connelly) Date: 2005-11-13 11:17
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
msg26858 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2005-11-14 04:43
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.  
msg26859 - (view) Author: Connelly (connelly) Date: 2005-12-02 06:17
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
msg26860 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2005-12-22 05:52
Logged In: YES 
user_id=33168

Facundo, can you look into this?  Are you still working on
Decimal?
msg26861 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2005-12-22 17:10
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.
msg26862 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2005-12-22 21:31
Logged In: YES 
user_id=38388

Hi Facundo,

the problem you are seeing seems to be in the way new-style
classes implement number coercion. 

Apparently some part in the (not so new-style anymore)
coercion logic is masking an exception which then triggers
later during processing. 

The NotImplemented singleton should never make it to the
interactive shell since it is normally only used internally
by the number coercion logic to signal "object method
doesn't handle mixed type operation".
msg26863 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-12-26 16:31
Logged In: YES 
user_id=4771

See proposed patch: #1390657
msg26864 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-02-19 00:05
Logged In: YES 
user_id=1188172

The patch was committed and fixed this, but only in SVN
HEAD, not for 2.4.
msg26865 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2006-02-20 10:22
Logged In: YES 
user_id=4771

Backported as r42511.
History
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42587
2005-11-13 11:17:39connellycreate