Skip to content

Add context to validation#516

Merged
toondaey merged 9 commits intomasterfrom
add-context-to-validation
Mar 30, 2026
Merged

Add context to validation#516
toondaey merged 9 commits intomasterfrom
add-context-to-validation

Conversation

@toondaey
Copy link
Copy Markdown
Contributor

This makes it easy for us to pass context data to pydantic validation. One of it's use cases is that of the test where we can then update this context thereby allowing nested models to validate based on the value provided by the parent.

This makes it easy for us to pass context data to pydantic validation.
One of it's use cases is that of the test where we can then update this
context thereby allowing nested models to validate based on the value
provided by the parent.
@toondaey toondaey requested a review from a team as a code owner March 24, 2026 10:38
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 enhances the configuration loading mechanism by introducing a "context" parameter to the Pydantic validation process. This allows for more sophisticated and interdependent validation rules, particularly useful for nested configurations where a child's validity might depend on data from its parent or other parts of the overall configuration. The change provides greater flexibility and power to define complex validation logic within the configuration system.

Highlights

  • Contextual Validation: Introduced an optional "context" dictionary to "load_file", "load_io", and "load_dict" functions, enabling external data to be passed during Pydantic model validation.
  • Pydantic Integration: Modified the internal calls to "schema.model_validate" to accept and utilize this new "context" parameter.
  • Nested Model Validation Example: Added a new test case that demonstrates how this context can be used to validate nested Pydantic models, allowing a child model's field to be validated against values provided by a parent model.

🧠 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.

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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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 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.

Footnotes

  1. 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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a 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 introduces a useful feature for passing context to Pydantic validation, which is great for complex validation scenarios. The implementation is mostly solid, and the tests are comprehensive.

I've found one area for improvement regarding the handling of the context object. The current implementation mutates the user-provided context, which can lead to unexpected behavior. My suggestions focus on making this safer by working on a copy of the context, which also allows for simplifying the code by centralizing the context handling logic. Please see my detailed comments.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.75%. Comparing base (9b7d129) to head (0efdd04).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #516      +/-   ##
==========================================
+ Coverage   81.25%   81.75%   +0.49%     
==========================================
  Files          43       43              
  Lines        4210     4210              
==========================================
+ Hits         3421     3442      +21     
+ Misses        789      768      -21     
Files with missing lines Coverage Δ
cognite/extractorutils/__init__.py 100.00% <100.00%> (ø)
...e/extractorutils/unstable/configuration/loaders.py 78.31% <100.00%> (+25.30%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Hmnt39
Hmnt39 previously approved these changes Mar 26, 2026
Copy link
Copy Markdown
Contributor

@Hmnt39 Hmnt39 left a comment

Choose a reason for hiding this comment

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

Looks good to me

@toondaey toondaey added the waiting-for-risk-review Waiting for a member of the risk review team to take an action label Mar 26, 2026
@trygveu trygveu self-assigned this Mar 27, 2026
@trygveu trygveu added the risk-review-ongoing Risk review is in progress label Mar 27, 2026
trygveu
trygveu previously approved these changes Mar 27, 2026
Copy link
Copy Markdown

@trygveu trygveu left a comment

Choose a reason for hiding this comment

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

LGTM, I have a couple of suggestions to make it clear that this is Pydantic context. (Context is a slightly overloaded term)

Co-authored-by: Trygve Utstumo <trygveu@users.noreply.github.com>
@toondaey toondaey dismissed stale reviews from trygveu and Hmnt39 via 6b844a3 March 27, 2026 10:01
toondaey and others added 3 commits March 27, 2026 11:02
Co-authored-by: Trygve Utstumo <trygveu@users.noreply.github.com>
Co-authored-by: Trygve Utstumo <trygveu@users.noreply.github.com>
Co-authored-by: Trygve Utstumo <trygveu@users.noreply.github.com>
@toondaey toondaey requested review from Hmnt39 and trygveu March 27, 2026 10:03
Copy link
Copy Markdown

@trygveu trygveu left a comment

Choose a reason for hiding this comment

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

Thanks
risk review ok

@toondaey toondaey requested a review from trygveu March 27, 2026 13:06
Copy link
Copy Markdown

@trygveu trygveu left a comment

Choose a reason for hiding this comment

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

Risk review ok

@toondaey toondaey merged commit d37235a into master Mar 30, 2026
10 of 11 checks passed
@toondaey toondaey deleted the add-context-to-validation branch March 30, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk-review-ongoing Risk review is in progress waiting-for-risk-review Waiting for a member of the risk review team to take an action

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants