-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Allow decimal.localcontext to accept keyword arguments to set context attributes #91291
Comments
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. |
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) |
I'm looking into adding this
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 |
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. |
I'm not sure what the implementation uses to enforce this, but decimal >>> from decimal import getcontext
>>> ctx = getcontext()
>>> setattr(ctx, 'precision', 10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'decimal.Context' object has no attribute 'precision' But you are absolutely correct that however we enforce it, we should |
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
AttributeError: 'decimal.Context' object has no attribute 'precision'
>>> with decimal.localcontext(precision=10) as ctx:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'precision' is an invalid keyword argument for this function
>>> |
…ext (#32242) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Implemented in #32242. Thanks for the PR! |
It appears this change is also causing issues on the entire buildbot fleet |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: