Skip to content

Break salsa cycle when inferring function signatures #1729

@dcreager

Description

@dcreager

While digging into an unrelated issue, I noticed that we are currently very likely to enter a salsa cycle when inferring the signature of a function literal.

The particular example I found involves the functools.cache decorator, whose definition is:

def cache(fn: Callable[..., _T]) -> _lru_cache_wrapper[_T]:
    ...

Then when we infer the type of a function definition that is decorated with @cache:

@cache
def f(x: int) -> int:
    ...
  • We enter infer_definition_types for f.
  • We construct a Type::FunctionType for the (undecorated) definition.
  • We apply the @cache decorator:
    • This requires checking whether f is assignable to Callable[..., T]. To do that, we need the signature of f.
    • OverloadLiteral::raw_signature calls
    • Signature::from_function, which calls
    • Parameters::from_parameters, which calls
    • infer_method_information, which calls
    • infer_definition_types for f, to check if f is a generic method, @classmethod, or @staticmethod.

We should see if there's a way to break that cycle. We might even consider not constructing the signatures lazily!

Sub-issues

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions