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

Implement fail-fast feature #9708

Merged
merged 20 commits into from
Jun 25, 2024
Merged

Implement fail-fast feature #9708

merged 20 commits into from
Jun 25, 2024

Conversation

uriyyo
Copy link
Contributor

@uriyyo uriyyo commented Jun 20, 2024

Change Summary

Implement fail-fast feature

Related issue number

pydantic/pydantic-core#1322

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @tiangolo

@uriyyo
Copy link
Contributor Author

uriyyo commented Jun 20, 2024

please review

Copy link

codspeed-hq bot commented Jun 20, 2024

CodSpeed Performance Report

Merging #9708 will not alter performance

Comparing uriyyo:fail-fast (64f7267) with main (5694da3)

Summary

✅ 13 untouched benchmarks

pydantic/types.py Outdated Show resolved Hide resolved
Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

@uriyyo,

Looking great overall - I've added a few comments / change requests. Let me know when you'd like another review 🚀 !

tests/test_types.py Show resolved Hide resolved
pydantic/fields.py Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Jun 21, 2024
@pydantic-hooky pydantic-hooky bot assigned uriyyo and unassigned tiangolo Jun 21, 2024
@sydney-runkle
Copy link
Member

Super excited to include this in v2.8 - it'll be a great addition to our performance tips docs as well :).

uriyyo and others added 3 commits June 21, 2024 21:19
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
@uriyyo
Copy link
Contributor Author

uriyyo commented Jun 21, 2024

@sydney-runkle Could you please review this PR again? Especially apply_known_metadata changes, not sure it was done correctly.

@pydantic-hooky pydantic-hooky bot added ready for review and removed awaiting author revision awaiting changes from the PR author labels Jun 21, 2024
@sydney-runkle sydney-runkle added relnotes-feature relnotes-performance Used for performance improvements. and removed relnotes-fix Used for bugfixes. labels Jun 21, 2024
Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looks great - just a few more changes!

I'll also pull this down locally to do some testing after this next round to make sure things are ready to move ahead!

We can do a pydantic-core minor release next week to prep for v2.8, then get this across the line!

pydantic/_internal/_known_annotated_metadata.py Outdated Show resolved Hide resolved
pydantic/_internal/_std_types_schema.py Outdated Show resolved Hide resolved
@@ -307,6 +307,7 @@ class SequenceValidator:
min_length: int | None = None
max_length: int | None = None
strict: bool | None = None
fail_fast: bool | None = None
Copy link
Member

Choose a reason for hiding this comment

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

Same question - is fail_fast applicable to all sequence types? It probably should be for this case, but the case about where we reuse SEQUENCE_CONSTRAINTS is where more of my concern lies.

Copy link
Member

Choose a reason for hiding this comment

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

Also, let's add some tests for my_field: Sequence = Field(..., fail_fast=True), etc.

Copy link
Member

Choose a reason for hiding this comment

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

Then see what happens with a deque or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same question - is fail_fast applicable to all sequence types?

You are right, it's not applicable to all sequence types. But I don't know how to correctly pass fail_fast to schema. Before calling apply_known_metadata it' always calls sequence_like_prepare_pydantic_annotations that fails because fail_fast is not in a list of SEQUENCE_CONSTRAINTS.

Maybe you have PR example how to correctly implement staff like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, let's add some tests for my_field: Sequence = Field(..., fail_fast=True), etc.

It won't work for now. I guess first we need to implement a way how from python say rust to stop validation.

More context here - pydantic/pydantic-core#1322 (comment)

pydantic/types.py Show resolved Hide resolved
@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Jun 22, 2024
@pydantic-hooky pydantic-hooky bot assigned uriyyo and unassigned sydney-runkle Jun 22, 2024
@uriyyo
Copy link
Contributor Author

uriyyo commented Jun 22, 2024

please review

@pydantic-hooky pydantic-hooky bot added ready for review and removed awaiting author revision awaiting changes from the PR author labels Jun 22, 2024
@pydantic-hooky pydantic-hooky bot assigned tiangolo and unassigned uriyyo Jun 22, 2024
Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Awesome work here!

Approved. We'll merge this in once we get the next pydantic-core version released. Should be later this week 🚀

@Viicos
Copy link
Member

Viicos commented Jun 24, 2024

I'm really liking this feature. I'm guessing we could have something similar at the model level:

from pydantic import BaseModel, ConfigDict, ValidationError

class Model(BaseModel):
    a: int
    b: int

    model_config = ConfigDict(fail_fast=True)

try:
    Model(a="some", b="string")
except ValidationError as e:
    print(e)
    # Only one error, for a

# or:

try:
    Model.model_validate({"a": "some", "b": "string"}, fail_fast=True)
except ValidationError as e:
    ...

Other than that, I'm hoping this is forward compatible with what was suggested here (making it an exception), then it could also be extended to any type:

from typing import Annotated

from pydantic import BaseModel, BeforeValidator

def my_validator(value: Any):
    raise FailFast  # Or `FailNow`?

class Model(BaseModel):
    a: Annotated[int, BeforeValidator(my_validator)]
    b: int

# Validating `Model` would not do the validation for `b`.

@sydney-runkle
Copy link
Member

Agreed, this is super promising as a new pattern.

Issues / PRs welcome re the ideas suggested above by @Viicos 🚀

@sydney-runkle sydney-runkle merged commit 32f4918 into pydantic:main Jun 25, 2024
51 checks passed
@uriyyo uriyyo deleted the fail-fast branch June 25, 2024 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants