[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-04-01 Thread Sam Ezeh


Change by Sam Ezeh :


--
pull_requests: +30313
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/32242

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-04-01 Thread Sam Ezeh


Sam Ezeh  added the comment:

This is what functionality looks like when supplying incorrect attribute names 
with the patch.

Python 3.11.0a6+ (heads/bpo-47135-dirty:d4bb38f82b, Apr  1 2022, 20:01:56) [GCC 
11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _pydecimal
>>> ctx = _pydecimal.getcontext()
>>> ctx.precision = 10
Traceback (most recent call last):
  File "", line 1, in 
  File "/run/media/sam/OS/Git/cpython/Lib/_pydecimal.py", line 3974, in 
__setattr__
raise AttributeError(
^
AttributeError: 'decimal.Context' object has no attribute 'precision'
>>> with _pydecimal.localcontext(precision=10) as ctx:
... pass
... 
Traceback (most recent call last):
  File "", line 1, in 
  File "/run/media/sam/OS/Git/cpython/Lib/_pydecimal.py", line 506, in 
localcontext
setattr(ctx, key, value)

  File "/run/media/sam/OS/Git/cpython/Lib/_pydecimal.py", line 3974, in 
__setattr__
raise AttributeError(
^
AttributeError: 'decimal.Context' object has no attribute 'precision'
>>> import decimal
>>> ctx = decimal.getcontext()
>>> ctx.precision = 10
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'decimal.Context' object has no attribute 'precision'
>>> with decimal.localcontext(precision=10) as ctx:
... pass
... 
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'precision' is an invalid keyword argument for this function
>>>

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-04-01 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

I'm not sure what the implementation uses to enforce this, but decimal 
contexts already seem to reject arbitrary attributes. So a naive 
implementation that just setattr()'s the keyword arguments will 
automatically fail:

>>> from decimal import getcontext
>>> ctx = getcontext()
>>> setattr(ctx, 'precision', 10)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'decimal.Context' object has no attribute 'precision'

But you are absolutely correct that however we enforce it, we should 
avoid allowing typos to silently fail.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-04-01 Thread Sam Ezeh


Sam Ezeh  added the comment:

I've uploaded a patch and it seems to work, which I'm very proud of.

I'll create some tests, amend documentation and create a news entry. After 
that, I'll create a pull request on GitHub.

--
keywords: +patch
Added file: https://bugs.python.org/file50713/sam_ezeh.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-04-01 Thread Sam Ezeh


Sam Ezeh  added the comment:

I'm looking into adding this

> Seems reasonable to me, but I think a full implementation would want to throw 
> an error for keyword args that don't already exist as context attributes 
> (otherwise typos would fail silently)

For _pydecimal, I think this would automatically happen automatically as 
Context.__setattr__ raises AttributeError when it's passed a name that isn't a 
context attribute.

For _decimal this can be done with keyword arguments and 
`PyArg_ParseTupleAndKeywords`.

--
nosy: +sam_ezeh

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-03-30 Thread Nick Coghlan


Nick Coghlan  added the comment:

Seems reasonable to me, but I think a full implementation would want to throw 
an error for keyword args that don't already exist as context attributes 
(otherwise typos would fail silently)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-03-29 Thread Dong-hee Na


Change by Dong-hee Na :


--
nosy: +corona10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-03-28 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
nosy: +ncoghlan

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47135] Allow decimal.localcontext to accept keyword arguments to set context attributes

2022-03-27 Thread Steven D'Aprano


New submission from Steven D'Aprano :

Whenever I use decimal and need to change the context temporarily, without fail 
I try to write

with decimal.localcontext(prec=10):
...

or similar. Then I get surprised that it fails, and re-write it as

with decimal.localcontext() as ctx:
ctx.prec = 10
...

Let's make the first version work. localcontext should accept keyword arguments 
corresponding to the same arguments accepted by Context, and set them on the 
context given.

A proof-of-concept wrapper function:

def localcontext(ctx=None, **kwargs):
if ctx is None:
ctx = decimal.getcontext().copy()
for key, value in kwargs.items():
setattr(ctx, key, value)
return decimal.localcontext(ctx)

I think this would be a valuable, and useful, improvement to the decimal API.

--
components: Library (Lib)
messages: 416114
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Allow decimal.localcontext to accept keyword arguments to set context 
attributes
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com