-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix eval type check #5908
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
Fix eval type check #5908
Conversation
WalkthroughThe changes update documentation to clarify restrictions on editing the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Builtins
participant ScopeArgs
participant Scope
participant VM
User->>Builtins: call eval(source, scope_args, vm)
Builtins->>ScopeArgs: make_scope(vm, "eval")
ScopeArgs->>ScopeArgs: validate_globals_dict (must be real dict)
ScopeArgs->>Scope: construct Scope with validated globals/locals
Builtins->>VM: run_code(source, scope, mode, "eval")
VM-->>User: result or error
Poem
📜 Recent review detailsConfiguration used: .coderabbit.yml 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @youknowone, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the eval
and exec
built-in functions in RustPython by implementing stricter type validation for their globals
argument, aligning its behavior and error messages more closely with CPython. This change improves compatibility and robustness, supported by a comprehensive set of new tests that verify various edge cases and correct error handling. Additionally, a minor documentation update clarifies guidelines for contributing to the Lib/
directory.
Highlights
- Stricter
eval
/exec
globals type checking: Theglobals
argument foreval
andexec
now undergoes more rigorous type validation, ensuring it is a truedict
as required by CPython, rather than just any mapping. - Improved error messages for
globals
: Error messages for invalidglobals
arguments ineval
andexec
have been refined to match CPython's specific wording, distinguishing between non-mapping types and mapping types that are not true dictionaries. - Comprehensive
eval
test suite: A significant number of new test cases have been added forbuiltin_eval.py
to thoroughly cover various scenarios forglobals
andlocals
arguments, includingUserDict
,None
,__builtins__
injection, and free variable handling. - Documentation clarification: A note was added to the
copilot-instructions.md
file, emphasizing that theLib/
directory should only be updated by copying files from CPython, not by direct editing.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request fixes a type check for the globals
argument in the eval
function. The changes include modifying ScopeArgs
to accept any Python object for globals
and then performing a detailed type validation. The logic has been refactored into a validate_globals_dict
helper function inside make_scope
, which now also handles exec
with more specific error messages. A comprehensive set of tests for eval
has been added in extra_tests/snippets/builtin_eval.py
. I've provided a suggestion to fix an assertion in the new test file.
eval("x", []) | ||
assert False, "eval with list globals should fail" | ||
except TypeError as e: | ||
assert "globals must be a real dict" in str(e), e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion for eval
with a list
as globals
is not quite right. Since a list
is not a mapping, eval
should raise a TypeError
indicating that globals
must be a dict
, not a "real dict". The "real dict" message is for mapping-like objects that aren't actual dictionaries (e.g., collections.UserDict
).
assert "globals must be a real dict" in str(e), e | |
assert "globals must be a dict" in str(e) | |
assert "real dict" not in str(e), e |
b803b0f
to
c8f36aa
Compare
Summary by CodeRabbit
Lib/
directory should not be edited directly, except for copying files from CPython.eval
function, focusing on error handling and validation ofglobals
andlocals
arguments.globals
argument ineval
andexec
, ensuring stricter type requirements and clearer error messages.