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

Incorrect Decimal-float behavior for += and *= #42587

Closed
connelly mannequin opened this issue Nov 13, 2005 · 9 comments
Closed

Incorrect Decimal-float behavior for += and *= #42587

connelly mannequin opened this issue Nov 13, 2005 · 9 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@connelly
Copy link
Mannequin

connelly mannequin commented Nov 13, 2005

BPO 1355842
Nosy @malemburg, @arigo, @birkenfeld, @facundobatista

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/arigo'
closed_at = <Date 2006-02-20.10:22:06.000>
created_at = <Date 2005-11-13.11:17:39.000>
labels = ['library']
title = 'Incorrect Decimal-float behavior for += and *='
updated_at = <Date 2006-02-20.10:22:06.000>
user = 'https://bugs.python.org/connelly'

bugs.python.org fields:

activity = <Date 2006-02-20.10:22:06.000>
actor = 'arigo'
assignee = 'arigo'
closed = True
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2005-11-13.11:17:39.000>
creator = 'connelly'
dependencies = []
files = []
hgrepos = []
issue_num = 1355842
keywords = []
message_count = 9.0
messages = ['26857', '26858', '26859', '26860', '26861', '26862', '26863', '26864', '26865']
nosy_count = 6.0
nosy_names = ['lemburg', 'nnorwitz', 'arigo', 'georg.brandl', 'facundobatista', 'connelly']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1355842'
versions = []

@connelly
Copy link
Mannequin Author

connelly mannequin commented Nov 13, 2005

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

@connelly connelly mannequin closed this as completed Nov 13, 2005
@connelly connelly mannequin assigned arigo Nov 13, 2005
@connelly connelly mannequin added the stdlib Python modules in the Lib dir label Nov 13, 2005
@connelly connelly mannequin closed this as completed Nov 13, 2005
@connelly connelly mannequin assigned arigo Nov 13, 2005
@connelly connelly mannequin added the stdlib Python modules in the Lib dir label Nov 13, 2005
@nnorwitz
Copy link
Mannequin

nnorwitz mannequin commented Nov 14, 2005

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.

@connelly
Copy link
Mannequin Author

connelly mannequin commented Dec 2, 2005

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

@nnorwitz
Copy link
Mannequin

nnorwitz mannequin commented Dec 22, 2005

Logged In: YES
user_id=33168

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

@facundobatista
Copy link
Member

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.

@malemburg
Copy link
Member

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".

@arigo
Copy link
Mannequin

arigo mannequin commented Dec 26, 2005

Logged In: YES
user_id=4771

See proposed patch: bpo-1390657

@birkenfeld
Copy link
Member

Logged In: YES
user_id=1188172

The patch was committed and fixed this, but only in SVN
HEAD, not for 2.4.

@arigo
Copy link
Mannequin

arigo mannequin commented Feb 20, 2006

Logged In: YES
user_id=4771

Backported as r42511.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

3 participants