mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 08:30:16 +00:00
Lazily evaluate all PEP 695 type alias values (#8033)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary In https://github.com/astral-sh/ruff/pull/7968, I introduced a regression whereby we started to treat imports used _only_ in type annotation bounds (with `__future__` annotations) as unused. The root of the issue is that I started using `visit_annotation` for these bounds. So we'd queue up the bound in the list of deferred type parameters, then when visiting, we'd further queue it up in the list of deferred type annotations... Which we'd then never visit, since deferred type annotations are visited _before_ deferred type parameters. Anyway, the better solution here is to use a dedicated flag for these, since they have slightly different behavior than type annotations. I've also fixed what I _think_ is a bug whereby we previously failed to resolve `Callable` in: ```python type RecordCallback[R: Record] = Callable[[R], None] from collections.abc import Callable ``` IIUC, the values in type aliases should be evaluated lazily, like type parameters. Closes https://github.com/astral-sh/ruff/issues/8017. ## Test Plan `cargo test`
This commit is contained in:
parent
94b4bb0f57
commit
a62c735f9e
8 changed files with 71 additions and 16 deletions
|
@ -1600,6 +1600,16 @@ bitflags! {
|
|||
/// ```
|
||||
const FUTURE_ANNOTATIONS = 1 << 14;
|
||||
|
||||
/// The model is in a type parameter definition.
|
||||
///
|
||||
/// For example, the model could be visiting `Record` in:
|
||||
/// ```python
|
||||
/// from typing import TypeVar
|
||||
///
|
||||
/// Record = TypeVar("Record")
|
||||
///
|
||||
const TYPE_PARAM_DEFINITION = 1 << 15;
|
||||
|
||||
/// The context is in any type annotation.
|
||||
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_ANNOTATION.bits();
|
||||
|
||||
|
@ -1610,11 +1620,12 @@ bitflags! {
|
|||
/// The context is in any deferred type definition.
|
||||
const DEFERRED_TYPE_DEFINITION = Self::SIMPLE_STRING_TYPE_DEFINITION.bits()
|
||||
| Self::COMPLEX_STRING_TYPE_DEFINITION.bits()
|
||||
| Self::FUTURE_TYPE_DEFINITION.bits();
|
||||
| Self::FUTURE_TYPE_DEFINITION.bits()
|
||||
| Self::TYPE_PARAM_DEFINITION.bits();
|
||||
|
||||
/// The context is in a typing-only context.
|
||||
const TYPING_CONTEXT = Self::TYPE_CHECKING_BLOCK.bits() | Self::TYPING_ONLY_ANNOTATION.bits() |
|
||||
Self::STRING_TYPE_DEFINITION.bits();
|
||||
Self::STRING_TYPE_DEFINITION.bits() | Self::TYPE_PARAM_DEFINITION.bits();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue