Skip to content

Allow eval and exec to take keyword arguments #105879

Closed
@rtpg

Description

@rtpg

Feature or enhancement

Allow for globals and locals to be passed in as keyword arguments to exec and eval.

Pitch

exec and eval both can take globals and locals as positional arguments.
The built-in functions documentation shows the argument defaults, and every time I have the documentation open and am writing these, I default to keyword arguments, and it fails because the arguments are positional-only.

For functions like this that might only be present in a handful of places, kwargs give a good way to document what parameters each argument is referring to. I don't think that in a universe in which we were introducing eval/exec now that we would explicitly disallow kwargs.

Previous discussion

I sent this email to python-ideas

Extra Links

Here is an implementation, which just consists in changing the argument clinic config and rerunning. I set up some very basic kwarg tests mostly to test the case of "locals provided but globals not provided", but fortunately the existing implementations already handled this cleanly!

Linked PRs

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions