Repository navigation
Closed
Description
| BPO | 7633 |
|---|---|
| Nosy | @rhettinger, @facundobatista, @mdickinson, @skrah |
| Files |
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/mdickinson'
closed_at = <Date 2010-02-18.14:53:06.918>
created_at = <Date 2010-01-04.13:01:12.082>
labels = ['easy', 'type-bug', 'library']
title = 'decimal.py: type conversion in context methods'
updated_at = <Date 2010-02-18.14:53:06.917>
user = 'https://github.com/skrah'bugs.python.org fields:
activity = <Date 2010-02-18.14:53:06.917>
actor = 'mark.dickinson'
assignee = 'mark.dickinson'
closed = True
closed_date = <Date 2010-02-18.14:53:06.918>
closer = 'mark.dickinson'
components = ['Library (Lib)']
creation = <Date 2010-01-04.13:01:12.082>
creator = 'skrah'
dependencies = []
files = ['15751', '15760', '16032', '16052', '16245', '16251']
hgrepos = []
issue_num = 7633
keywords = ['patch', 'easy']
message_count = 29.0
messages = ['97207', '97259', '97260', '97261', '97268', '97288', '97304', '97308', '97317', '98212', '98213', '98214', '98216', '98346', '98347', '98349', '98351', '98354', '98497', '98526', '98527', '98534', '98535', '98541', '98544', '99475', '99502', '99507', '99509']
nosy_count = 5.0
nosy_names = ['rhettinger', 'facundobatista', 'mark.dickinson', 'jjconti', 'skrah']
pr_nums = []
priority = 'normal'
resolution = 'accepted'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7633'
versions = ['Python 2.7', 'Python 3.2']
Activity
skrah commentedon Jan 4, 2010
I think that context methods should convert arguments regardless of position:
Also, perhaps c.add(9, 8) should be allowed, too.
jjconti commentedon Jan 5, 2010
The same happens with other Context methods, like divide:
mdickinson commentedon Jan 5, 2010
I agree that this would be desirable. And yes, c.add(9, 8) should be allowed, too.
Anyone interested in producing a patch?
jjconti commentedon Jan 5, 2010
I've been reading http://speleotrove.com/decimal and seems not to be an explicit definition about this. I'm thinking in a patch I could supply tomorrow.
What about the idea of changing the implementation from:
to
?
mdickinson commentedon Jan 5, 2010
I think it would be better to convert the arguments a and b to Decimal before doing the addition. In the case of addition, it doesn't make much difference: for integers a and b, Decimal(a+b) rounded to the current context should be the same as Decimal(a) + Decimal(b). But for e.g., division, Decimal(a/b) is potentially different from Decimal(a)/Decimal(b).
There's also the issue that the context methods can return 'NotImplemented'. For example:
It's possible that a TypeError would make more sense here: NotImplemented should really only be returned by direct invocation of the double underscore magic methods (add, etc.).
Any patch should include tests in Lib/test/test_decimal.py, of course!
jjconti commentedon Jan 6, 2010
The attached patch solves this issue.
Finally, only operand 'a' needs to be converted to Decimal if it's not. I discover this after writing my tests and start the implementation.
A Context.method is modified if it has more than one operand (for example a and b) and Decimal.method uses _convert_other
26 Context's methods were modified.
26 unit tets were added to ContextAPItests TestCase.
docstring was added to Context.divmod
mdickinson commentedon Jan 6, 2010
Thanks for the patch!
Rather than using the Decimal constructor, I think you should convert use _convert_other(..., raiseit=True): the Decimal constructor converts strings and tuples, as well as ints and longs, while _convert_other only converts ints and longs. Note also that the conversion shouldn't depend on the current context; only the operation itself needs that.
Maybe it would be worth adding some tests to ensure that e.g.,
raises TypeError as expected?
I agree with the observation that it's usually only necessary to convert the first argument (since the Decimal method itself converts the second).
If you like, you could also usefully deal with the NotImplemented return value by turning it into a TypeError (i.e., in the context method, check for a NotImplemented return value, and raise TypeError there if necessary). This is only needed for the double underscore methods __add__, __sub__, etc. associated with Python's binary operators; the other methods shouldn't ever return NotImplemented.
mdickinson commentedon Jan 6, 2010
Thanks for the missing divmod docstring, too: I've applied this separately, partly because it needs to go into 2.6 and 3.1 as well as 2.7 and 3.2, and partly as an excuse to check that the new py3k-cdecimal branch is set up correctly. See revisions r77324 through r77327.
jjconti commentedon Jan 6, 2010
New patch.
Fix Context.method that were returning NotImplemented.
Decimal.__methods__ don't use raiseit=True in _convert_other so I check in Context.method and raise TypeError if necessary.
Added tests for Context.divmod missed in first patch.
7 remaining items
mdickinson commentedon Jan 26, 2010
Juan, don't worry about the documentation if you don't want to. I can fix that up easily. (Of course, if you do want to, go ahead!)
facundobatista commentedon Jan 26, 2010
Juanjo, ping me in private if you want help with the doc toolchain, I can show you how to touch the .rst and see the changes after processing.
jjconti commentedon Jan 29, 2010
(also documented them properly as in 4)
copy_sing fixed and documented to have the same behaibour.
Ans a change in Doc/library/decimal.rst to reflec the new behaibour.
mdickinson commentedon Jan 29, 2010
The updated patch looks good---thank you! We're getting there... :)
I'm not sure about the extra 'Operand can be Decimal or int.' in the method docstrings; this just looks like extra clutter to me. Rather, I think it would be a surprise worthy of documentation if these methods *didn't* accept int or long; since they now do (with your patch), I don't think it's really worth mentioning in the docstring. And it's not quite accurate, either, since these methods should accepts longs as well as ints (and bools, and instances of other subclasses).
Would you be content to remove these from the docstrings? Or do others monitoring this issue think they should stay?
The extra doctests and test_decimal tests are nice.
mdickinson commentedon Jan 29, 2010
Hmm. Thanks for noticing this: it looks like Decimal.copy_sign is missing a _convert_other call. I think that should be fixed in the Decimal class rather than in the Context class (so Context.copy_sign and Decimal.copy_sign should make one _convert_other call each, instead of Context.copy_sign making two calls).
skrah commentedon Jan 29, 2010
I also think that the added docstrings are not really necessary.
Another thing: I forgot to mention 'canonical' in the list of functions
that should only accept Decimals. As with the other two (number_class
and is_canonical), this is a matter of taste. Personally I would not
expect them to work for integers.
mdickinson commentedon Jan 29, 2010
Re: canonical. Yes, this made me pause for a second, too. But I don't see the harm in allowing it to accept ints and longs. Actually, it then provides a nice public version of _convert_other.
I'd probably also allow is_canonical and number_class to accept ints and longs, just on the basis that that gives fewer special cases.
skrah commentedon Jan 29, 2010
Yes, indeed 'canonical' can be justified to take an integer, if we interpret the spec as:
'canonical' takes an operand and returns the preferred _decimal_
encoding of that operand.
But then 'is_canonical' should return false for an integer, and
this would create another special case: Accept an operand and
return something meaningful _without_ converting it first.
I think this is why I have problems with those two. 'number_class'
is less of a problem.
jjconti commentedon Jan 30, 2010
Yeah... I did't like that docstring either :) Removed!
Also fixed Decimal.copy_sign, changed Context.copy_sign and added tests.
mdickinson commentedon Feb 17, 2010
The latest patch looks fine. I've attached a slightly tweaked version:
Add conversions for number_class; without this, number_class is inconsistent with the various is_*** methods. "c.is_normal(3)" should be equivalent to "c.number_class(3) in ('+Normal', '-Normal)", for example.
Remove conversions for 'canonical' and 'is_canonical'; I agree with Stefan that these don't make a lot of sense. It's mostly academic, since I can't imagine either of these methods gets much use.
I've reworded the documentation slightly.
Remove lots of trailing whitespace (mostly on otherwise empty lines).
If this looks okay to everyone, I'll check it in.
skrah commentedon Feb 18, 2010
It looks good (I agree on number_class), but I'd change these:
Add raiseit=True to context.copy_decimal()
Remove wrong comment from context.is_infinite()
Add _convert_other(a, raiseit=True) to context.logical_invert()
More whitespace in test_decimal()
The new tests could be condensed quite a bit by using getattr(), but
that isn't so important.
mdickinson commentedon Feb 18, 2010
Thanks, Stefan. Applied to the trunk in r78217. I'll forward port to py3k.
mdickinson commentedon Feb 18, 2010
Tweaked some doctests in r78218:
methods, since they're illegal in Python 3.
Merged to py3k in r78219.
Thanks, everyone!