[ty] Treat pydantic validator/serializer decorators as implicit classmethods#23272
Draft
rayzeller wants to merge 2 commits intoastral-sh:mainfrom
Draft
[ty] Treat pydantic validator/serializer decorators as implicit classmethods#23272rayzeller wants to merge 2 commits intoastral-sh:mainfrom
rayzeller wants to merge 2 commits intoastral-sh:mainfrom
Conversation
…methods Pydantic's `@field_validator`, `@model_validator`, and `@field_serializer` decorators implicitly wrap decorated functions as classmethods. ty previously treated them as instance methods, causing incorrect type inference for the `cls` parameter (typed as `Self` instead of `type[Self]`). This expands `KnownModule` to support third-party modules (gated by search path kind) and adds `KnownFunction` variants for the three pydantic decorators, following the approach recommended in astral-sh#2403. Closes astral-sh/ty#2719 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportMemory usage unchanged ✅ |
- Add `#[derive(Debug)]` to `KnownModuleSearchPathKind` - Consolidate duplicate match arms for pydantic functions - Improve comment explaining third-party module test skip - Document `@model_validator(mode="after")` known limitation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pydantic's
@field_validator,@model_validator, and@field_serializerdecorators implicitly wrap decorated functions as classmethods. ty previously treated them as instance methods, causing incorrect type inference for theclsparameter (typed asSelfinstead oftype[Self]).This PR expands
KnownModuleto support third-party modules and addsKnownFunctionvariants for the three pydantic decorators, following the approach recommended by carljm in #2403.Changes
SearchPath::is_third_party()— new helper that returnstruefor bothSitePackagesandEditablepathsKnownModule— addedPydantic,PydanticFunctionalValidators,PydanticFunctionalSerializersvariants, gated by a newKnownModuleSearchPathKindso third-party modules only resolve from third-party search pathsKnownFunction— addedFieldValidator,ModelValidator,FieldSerializervariants withis_pydantic_implicit_classmethod_decorator()helperbuilder.rs— extended the decorator loop ininfer_first_self_or_cls_parameter_type()to detect pydantic call-expression decorators as implicit classmethodsKnown limitations (out of scope)
@model_validator(mode="after")receives the model instance, not the class, so it should not be treated as a classmethod. This needs argument-aware detection and is a follow-up task.Test plan
cargo nextest run -p ty_module_resolver— 139 passedcargo nextest run -p ty_python_semantic— 463 passedMDTEST_EXTERNAL=1 cargo nextest run -p ty_python_semantic --test mdtest -- "external/pydantic"— passedcargo clippy --workspace --all-targets --all-features -- -D warnings— cleanuvx prek run -a— all checks passedCloses astral-sh/ty#2719
🤖 Generated with Claude Code