Skip to content

Ensure ValidationInfo.field_name is correct on validator reuse #1692

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

DouweM
Copy link

@DouweM DouweM commented Apr 15, 2025

Change Summary

See pydantic/pydantic#11737 (comment)

Related issue number

Fixes pydantic/pydantic#11737

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @sydney-runkle

…e called with correct ValidationInfo.field_name
@DouweM
Copy link
Author

DouweM commented Apr 15, 2025

@davidhewitt Please review!

cc @Viicos

Edit: Never mind, failing tests, looking

Copy link

codspeed-hq bot commented Apr 15, 2025

CodSpeed Performance Report

Merging #1692 will not alter performance

Comparing DouweM:validator-reuse-field-name (9bac3bd) with main (9a25aa6)

Summary

✅ 157 untouched benchmarks

@DouweM
Copy link
Author

DouweM commented Apr 15, 2025

I ended up changing some tests in 0fd7ef1 and df34451.

Both are similar to the issue described in pydantic/pydantic#11737 (comment): we now get ValidationInfo.field_name even if the validator wasn't explicitly created for a specific field, which is not always expected.

I think the changes in the second commit make sense as the validator acts on the field value, but not necessarily in first commit where the validator acts on the entire model.

I'm not sure the State based approach gives us the ability to drop the field_name in that specific case though. I think I'm OK with this change in behavior, interpreting field_name as "this is the field currently being validated" more than "this is the field the validator acts on".

@Viicos What do you thnk?

@DouweM
Copy link
Author

DouweM commented Apr 15, 2025

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, looking good!

@davidhewitt
Copy link
Contributor

We're hitting some leak/GC issues! https://github.com/pydantic/pydantic-core/actions/runs/14476861875/job/40604489000?pr=1692

These might be reproducable on main; it's possible it's something new with 3.13.3.

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM, with just the question of maybe we start cleaning up the existing state to keep things efficient.

The 3.13t tests will not block merge, so depending on whether we leave the above for a follow-up this is good to go 👍

@Viicos
Copy link
Member

Viicos commented Apr 15, 2025

LGTM, with just the question of maybe we start cleaning up the existing state to keep things efficient.

You mean having the state rebound only if we are certain a validator (with info) is going to be used?

@davidhewitt
Copy link
Contributor

LGTM, with just the question of maybe we start cleaning up the existing state to keep things efficient.

You mean having the state rebound only if we are certain a validator (with info) is going to be used?

Nope I just meant the deprecation which you agreed with. 👍

@@ -1954,7 +1954,7 @@ class NoInfoValidatorFunctionSchema(TypedDict):
class WithInfoValidatorFunctionSchema(TypedDict, total=False):
type: Required[Literal['with-info']]
function: Required[WithInfoValidatorFunction]
field_name: str
field_name: str # deprecated
Copy link
Author

Choose a reason for hiding this comment

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

I think this TypedDict is only used for type hinting -- let me know if this is sufficient or if we should force a deprecation warning here as well somehow.

# insert_assert(reprs)
assert reprs == [
'ValidationInfo(config=None, context=None, data=None, field_name=None)',
"ValidationInfo(config=None, context=None, data=None, field_name='x')",
Copy link
Author

Choose a reason for hiding this comment

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

This test explicitly tested the now-deprecated behavior of setting a field_name on the validator function schema. If we assume no one was directly constructing these instead of us, removing this should be fine.

I could also leave the test and wrap the call in pytest.warns, to verify the old behavior keeps working (which it does) -- let me know what you prefer.

@DouweM
Copy link
Author

DouweM commented Apr 15, 2025

@Viicos Pydantic is still passing field_name, so it makes sense that test-pydantic-integration is failing now that I've deprecated it.

What's the best way to update pydantic for the new pydantic-core, before this is even merged?

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

Successfully merging this pull request may close these issues.

Incorrect ValidationInfo.field_name when using type statement
3 participants