refactor(checks): replace Scenario sequence with step-based structure#2297
refactor(checks): replace Scenario sequence with step-based structure#2297
Conversation
- Remove ScenarioBuilder and scenario() to eliminate variable shadowing
- Add fluent methods (interact, check, etc.) directly to Scenario
- Update Suite to accept only Scenario
- Use scenario/scenario1/scenario2 as variable names in examples
BREAKING CHANGE: scenario() and ScenarioBuilder are removed. Use
Scenario("name").interact(...).check(...) instead.
Closes #2287
Made-with: Cursor
…-encourages-variable-shadowing
- Add Step model grouping interacts and checks per TestCase - Replace sequence field with steps in Scenario - Add Scenario.from_sequence() for flat component input - Update runner with _build_steps for target binding - Fix Equals examples to use expected_value parameter Refs: 2290 Made-with: Cursor
Summary of ChangesHello, 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 significantly refactors the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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 by creating a comment using either
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 Limitations & Feedback Gemini Code Assist 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant and well-executed refactoring of the Scenario class, replacing the flat sequence of components with a more structured steps approach. The new Step model, which groups interactions and checks, improves clarity and makes the scenario's structure more explicit. The introduction of Scenario.from_sequence() is a thoughtful addition that maintains ease of use for simpler cases. The runner has been simplified, and the changes are consistently applied across the codebase, including documentation and tests. Overall, this is a great improvement to the library's core logic. I have one minor suggestion for improving code clarity in the runner.
| interact = type(interact).model_validate( | ||
| interact.model_copy(update={"outputs": target}) | ||
| ) |
There was a problem hiding this comment.
The current way of creating a new Interact instance with an updated outputs field is a bit convoluted and potentially inefficient. It creates an intermediate model instance with model_copy, which is then passed to model_validate (which internally converts it back to a dictionary before validation). A more direct and clearer approach would be to dump the model to a dictionary, update the dictionary, and then validate it to create the new instance. This was the approach used before this refactoring.
This is more explicit about the "dump, update, validate" process and avoids creating an unnecessary intermediate model object.
| interact = type(interact).model_validate( | |
| interact.model_copy(update={"outputs": target}) | |
| ) | |
| interact_data = interact.model_dump() | |
| interact_data["outputs"] = target | |
| interact = type(interact).model_validate(interact_data) |
There was a problem hiding this comment.
Using model_dump would prevent potentially excluded field to be passed to newly created model.
There was a problem hiding this comment.
You are absolutely right, @kevinmessiaen. My apologies for overlooking that crucial detail. Using model_dump() could indeed lead to issues if the Interact model has fields that are excluded from serialization by default (e.g., with exclude=True in their Field definition) but are still necessary for model_validate() to reconstruct a complete and correct instance.
model_copy(update=...) is a much safer and more robust approach in this scenario, as it preserves all existing fields of the model and only modifies the specified ones, ensuring that no essential data is inadvertently dropped or defaulted during the update process.
Thank you for catching this and pointing out the potential pitfall. The original implementation is indeed better for correctness and data integrity.
…f-scenario-object-proposal
mattbit
left a comment
There was a problem hiding this comment.
Note: some hack with the Interact validator to set the private attribute needed for the generator. Fixed for now with a set_outputs but is something we'll need to get rid of in the future.
Description
Related Issue
Type of Change
Checklist
CODE_OF_CONDUCT.mddocument.CONTRIBUTING.mdguide.pdm.lockrunningpdm update-lock(only applicable whenpyproject.tomlhas beenmodified)