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

[components] Enrich pydantic validation errors with filepaths, line numbers #26962

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

benpankow
Copy link
Member

@benpankow benpankow commented Jan 9, 2025

Summary

Ports over the YAML line tracking functionality to enrich Pydantic ValidationErrors thrown when instantiating components. The filepath of the loaded YAML file & offending line number are appended to the error message.

E       ValidationError: 2 validation errors for MyNestedComponentSchema
E         `nested.foo.an_int at /var/folders/bc/4npnrqyd46l4vbbkdsdsw3_h0000gn/T/tmpte2ox0bw/component.yaml:6`
E           Field required [type=missing, input_value={'a_string': 'test'}, input_type=dict]
E             For further information visit https://errors.pydantic.dev/2.8/v/missing
E         `nested.baz.a_string at /var/folders/bc/4npnrqyd46l4vbbkdsdsw3_h0000gn/T/tmpte2ox0bw/component.yaml:11`
E           Field required [type=missing, input_value={'an_int': 10}, input_type=dict]
E             For further information visit https://errors.pydantic.dev/2.8/v/missing

Test Plan

New set of validation integration tests.

@benpankow
Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@benpankow benpankow force-pushed the benpankow/components-enrich-yaml branch from 954424c to 54d3e9d Compare January 9, 2025 17:30
@benpankow benpankow requested review from schrockn, OwenKephart and smackesey and removed request for schrockn and OwenKephart January 9, 2025 17:32
@@ -3,6 +3,6 @@
- key: customers

- command: "python customer_stats.py"
assets:
Copy link
Member

Choose a reason for hiding this comment

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

rm?

both error reporting and populating source position information.
"""
try:
model = parse_obj_as(cls, obj_parse_root.value)
Copy link
Member

@schrockn schrockn Jan 9, 2025

Choose a reason for hiding this comment

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

This is deprecated:

'parse_obj_as is deprecated. Use pydantic.TypeAdapter.validate_python instead.',

Comment on lines +23 to +29
def from_file(contents: str, filepath: str) -> "ComponentFileModel":
parsed = parse_yaml_with_source_positions(contents, filepath)
obj = _parse_and_populate_model_with_annotated_errors(
cls=ComponentFileModel, obj_parse_root=parsed, obj_key_path_prefix=[]
)

obj._source_position_tree = parsed.source_position_tree # noqa: SLF001
Copy link
Member

Choose a reason for hiding this comment

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

We should create a new non-pydantic class that represents the File and all metadata about it.

Copy link
Member

Choose a reason for hiding this comment

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

need code smell article for this one :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants