Skip to content

gh-162491: Accept a list of signals in _pydecimal.localcontext() - #162497

Open
skdas20 wants to merge 2 commits into
python:mainfrom
skdas20:gh-162491-pydecimal-localcontext
Open

skdas20 wants to merge 2 commits into
python:mainfrom
skdas20:gh-162491-pydecimal-localcontext

Conversation

@skdas20

@skdas20 skdas20 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

_pydecimal.localcontext() passed flags and traps straight to setattr on the new context, which only accepts a signal dict, so localcontext(traps=[Overflow]) raised TypeError. The C implementation accepts a list here. This converts a list the same way Context.__init__ does.

As with the C version, only lists are converted: other types still raise TypeError, and a list with a non-signal raises KeyError. The new assertions in test_localcontext_kwargs run for both implementations. I checked them against the C _decimal too, since my local build doesn't have it.

🤖 Generated with Claude Code

localcontext() set the flags and traps keyword arguments directly on the
new context, which only accepts a signal dict, so passing a list of
signals raised TypeError. Convert a list the same way the Context
constructor does, matching the C implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread Lib/_pydecimal.py Outdated
Comment on lines +423 to +424
if key in ('flags', 'traps') and isinstance(value, list):
# Accept a list of signals, like the Context constructor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if key in ('flags', 'traps') and isinstance(value, list):
# Accept a list of signals, like the Context constructor.
if key in ('flags', 'traps') and not isinstance(value, dict):

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@skdas20

skdas20 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @skirpichev, applied in a619c64. It matches the Context constructor, and strings, None and tuples still raise TypeError (from the list concatenation), so the existing tests are unaffected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants