[ty] fix query cycles in decorated function with parameter defaults#23014
Merged
[ty] fix query cycles in decorated function with parameter defaults#23014
Conversation
…meters When a protocol method is decorated (e.g., with `@contextmanager`) and has default parameter values, the type checker would incorrectly report an `invalid-argument-type` error when passing a conforming implementation. The root cause was a query cycle during signature computation. When computing the signature for a function with defaults, `definition_expression_type` was called for each default, which first tries `infer_definition_types`. If we're already in the middle of inferring that definition (e.g., during decorator application), this creates a cycle. The fix uses `infer_deferred_types` directly for default value types, following the same pattern already used for parameter annotations in `function_signature_expression_type`. Since defaults are always deferred (as noted in `infer_function_definition`), we can skip the `infer_definition_types` check and go directly to deferred inference, avoiding the cycle. Additionally, when getting the `propagatable_kind` for a `FunctionLiteral` during decorator application, we now get the kind directly from the function without computing its full signature, which is a related optimization. Fixes: astral-sh/ty#2692 https://claude.ai/code/session_01GZpEtiXNH5JVFUK4ntvUF7
Typing conformance resultsNo changes detected ✅ |
|
charliermarsh
approved these changes
Feb 2, 2026
carljm
added a commit
that referenced
this pull request
Feb 2, 2026
* main: (48 commits) add info for non_octal permissions (#22972) Fix empty body rule rendering (#23039) [ty] Infer `ParamSpec` from class constructors for callable protocols (#22853) Update NPM Development dependencies (#23030) Update CodSpeedHQ/action action to v4.8.2 (#23029) [ty] remove special handling for `Any()` in match class patterns (#23011) Update Rust crate get-size2 to v0.7.4 (#23022) Update Rust crate insta to v1.46.1 (#23023) Update taiki-e/install-action action to v2.67.11 (#23033) Update Rust crate colored to v3.1.1 (#23031) Update cargo-bins/cargo-binstall action to v1.17.3 (#23028) Update Rust crate uuid to v1.20.0 (#23032) [ty] Avoid using `.node()` for detecting `Self` (#23000) Update Rust crate proc-macro2 to v1.0.106 (#23024) Update actions/setup-python action to v6.2.0 (#23027) [ty] fix query cycles in decorated function with parameter defaults (#23014) Update Rust crate quote to v1.0.44 (#23025) Update Rust crate thiserror to v2.0.18 (#23026) Update Rust crate filetime to v0.2.27 (#23021) Update Rust crate clearscreen to v4.0.3 (#23020) ...
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.
Fixes astral-sh/ty#2692
Summary
This PR fixes query cycles that occur when processing decorated functions with default parameter values. The issue arises because computing a function's full signature requires evaluating default parameter expressions, which can trigger deferred type inference that may cycle back to the original query.
For
FunctionLiteraltypes, directly check decorator attributes (is_classmethod,is_staticmethod) instead of computing the full callable signature. This avoids unnecessary query cycles while still propagating the callable kind through decorators.When computing parameter default types, use
infer_deferred_typesdirectly instead ofdefinition_expression_type. Since defaults are always deferred (as per the function definition logic), this approach is more efficient and avoids potential cycles (becausedefinition_expression_typere-queriesinfer_definition_typesfor the function definitionTest Plan
Added protocol subtyping test cases in
protocols.mdcovering decorated methods with various configurations. Existing test suite passes without regressions.We also see some significant memory-use reductions on some projects, presumably due to reduced query cycles.