mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00

## Summary This PR is a more holistic fix for https://github.com/astral-sh/ruff/issues/9534 and https://github.com/astral-sh/ruff/issues/9159. When we visit the AST, we track nodes that we need to visit _later_ (deferred nodes). For example, when visiting a function, we defer the function body, since we don't want to visit the body until we've visited the rest of the statements in the containing scope. However, deferred nodes can themselves contain deferred nodes... For example, a function body can contain a lambda (which contains a deferred body). And then there are rarer cases, like a lambda inside of a type annotation. The aforementioned issues were fixed by reordering the deferral visits to catch common cases. But even with those fixes, we still fail on cases like: ```python from __future__ import annotations import re from typing import cast cast(lambda: re.match, 1) ``` Since we don't expect lambdas to appear inside of type definitions. This PR modifies the `Checker` to keep visiting until all the deferred stacks are empty. We _already_ do this for any one kind of deferred node; now, we do it for _all_ of them at a level above.
10 lines
250 B
Python
10 lines
250 B
Python
"""Test: ensure that we visit type definitions and lambdas recursively."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections import Counter
|
|
from typing import cast
|
|
|
|
|
|
def get_most_common_fn(k):
|
|
return lambda c: cast(Counter, c).most_common(k)
|