Skip to content
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 eval and exec to take keyword arguments #105879

Closed
rtpg opened this issue Jun 17, 2023 · 6 comments
Closed

Allow eval and exec to take keyword arguments #105879

rtpg opened this issue Jun 17, 2023 · 6 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@rtpg
Copy link
Contributor

rtpg commented Jun 17, 2023

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

@rtpg rtpg added the type-feature A feature request or enhancement label Jun 17, 2023
@JelleZijlstra JelleZijlstra changed the title Alow eval and exec to take keyword arguments Allow eval and exec to take keyword arguments Jun 17, 2023
@sunmy2019
Copy link
Member

My concern is, will this hurt performance a lot?

@JelleZijlstra
Copy link
Member

We can benchmark it, but that seems unlikely; parsing code is probably a lot slower than accepting some keyword arguments.

@rtpg
Copy link
Contributor Author

rtpg commented Jun 17, 2023

@sunmy2019 I added a link to a commit implementing this. The short version is eval now has a bit more parsing code, but exec already has support for one kwarg, so we're still looking at the same order of magnitude of instructions or existing code IMO.

@JelleZijlstra
Copy link
Member

Make a PR :)

@rtpg
Copy link
Contributor Author

rtpg commented Jun 19, 2023

Created the PR, and tests seem to be passing now

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 27, 2023
@JelleZijlstra JelleZijlstra self-assigned this May 1, 2024
JelleZijlstra added a commit that referenced this issue May 2, 2024
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JelleZijlstra
Copy link
Member

This will be in Python 3.13, thank you!

SonicField pushed a commit to SonicField/cpython that referenced this issue May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants