ruff/crates/ty_python_semantic/resources/mdtest/annotations
Douglas Creager c8851ecf70
[ty] Defer all parameter and return type annotations (#21906)
As described in astral-sh/ty#1729, we previously had a salsa cycle when
inferring the signature of many function definitions.

The most obvious case happened when (a) the function was decorated, (b)
it had no PEP-695 type params, and (c) annotations were not always
deferred (e.g. in a stub file). We currently evaluate and apply function
decorators eagerly, as part of `infer_function_definition`. Applying a
decorator requires knowing the signature of the function being
decorated. There were two places where signature construction called
`infer_definition_types` cyclically.

The simpler case was that we were looking up the generic context and
decorator list of the function to determine whether it has an implicit
`self` parameter. Before, we used `infer_definition_types` to determine
that information. But since we're in the middle of signature
construction for the function, we can just thread the information
through directly.

The harder case is that signature construction requires knowing the
inferred parameter and return type annotations. When (b) and (c) hold,
those type annotations are inferred in `infer_function_definition`! (In
theory, we've already finished that by the time we start applying
decorators, but signature construction doesn't know that.)

If annotations are deferred, the params/return annotations are inferred
in `infer_deferred_types`; if there are PEP-695 type params, they're
inferred in `infer_function_type_params`. Both of those are different
salsa queries, and don't induce this cycle.

So the quick fix here is to always defer inference of the function
params/return, so that they are always inferred under a different salsa
query.

A more principled fix would be to apply decorators lazily, just like we
construct signatures lazily. But that is a more invasive fix.

Fixes astral-sh/ty#1729

---------

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2025-12-11 15:00:18 -05:00
..
annotated.md [ty] Support stringified annotations in value-position `Annotated` instances (#21447) 2025-11-14 13:09:09 +00:00
any.md [ty] Improve the display of various special-form types (#21775) 2025-12-03 21:19:59 +00:00
callable.md [ty] Complete support for `ParamSpec` (#21445) 2025-12-05 22:00:06 +05:30
deferred.md [ty] fix global symbol lookup from eager scopes (#21317) 2025-11-12 10:15:51 -08:00
generic_alias.md [ty] Remove `GenericAlias`-related todo type (#21728) 2025-12-01 13:02:38 +00:00
int_float_complex.md [ty] Improve disjointness inference for `NominalInstanceType`s and `SubclassOfType`s (#18864) 2025-06-24 20:27:37 +00:00
invalid.md [ty] tighten up handling of subscripts in type expressions (#21503) 2025-11-18 10:43:07 -08:00
literal.md [ty] tighten up handling of subscripts in type expressions (#21503) 2025-11-18 10:43:07 -08:00
literal_string.md [ty] Improve several "Did you mean?" suggestions (#21597) 2025-11-25 10:29:01 +00:00
never.md [ty] Improve the display of various special-form types (#21775) 2025-12-03 21:19:59 +00:00
new_types.md [ty] Remove `GenericAlias`-related todo type (#21728) 2025-12-01 13:02:38 +00:00
optional.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
self.md [ty] Defer all parameter and return type annotations (#21906) 2025-12-11 15:00:18 -05:00
starred.md [ty] Return `Option<TupleType>` from `infer_tuple_type_expression` (#19735) 2025-08-04 13:48:19 +01:00
stdlib_typing_aliases.md [ty] Fix bug where ty would think all types had an `__mro__` attribute (#20995) 2025-10-27 11:19:12 +00:00
string.md [ty] Avoid inferring types for invalid binary expressions in string annotations (#21911) 2025-12-11 09:40:19 +01:00
union.md [ty] Implicit type aliases: Support for PEP 604 unions (#21195) 2025-11-03 21:50:25 +01:00
unsupported_special_forms.md [ty] Complete support for `ParamSpec` (#21445) 2025-12-05 22:00:06 +05:30
unsupported_special_types.md Generalize special-casing for enums constructed with the functional syntax (#17885) 2025-05-06 11:02:55 +01:00
unsupported_type_qualifiers.md [ty] validate constructor call of `TypedDict` (#19810) 2025-08-25 14:45:52 +02:00