mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00
Consider all TYPE_CHECKING
symbols for type-checking blocks (#16669)
## Summary This PR stabilizes the preview behavior introduced in https://github.com/astral-sh/ruff/pull/15719 to recognize all symbols named `TYPE_CHECKING` as type-checking checks in `if TYPE_CHECKING` conditions. This ensures compatibility with mypy and pyright. This PR also stabilizes the new behavior that removes `if 0:` and `if False` to be no longer considered type checking blocks. Since then, this syntax has been removed from the typing spec and was only used for Python modules that don't have a `typing` module ([comment](https://github.com/astral-sh/ruff/pull/15719#issuecomment-2612787793)). The preview behavior was first released with Ruff 0.9.5 (6th of February), which was about a month ago. There are no open issues or PRs for the changed behavior ## Test Plan The snapshots for `SIM108` change because `SIM108` ignored type checking blocks but it can no simplify `if 0` or `if False` blocks again because they're no longer considered type checking blocks. The changes in the `TC005` snapshot or only due to that `if 0` and `if False` are no longer recognized as type checking blocks <!-- How was it tested? -->
This commit is contained in:
parent
3d2f2a2f8d
commit
92193a3254
6 changed files with 78 additions and 152 deletions
|
@ -1,10 +1,10 @@
|
|||
//! Analysis rules for the `typing` module.
|
||||
|
||||
use ruff_python_ast::helpers::{any_over_expr, is_const_false, map_subscript};
|
||||
use ruff_python_ast::helpers::{any_over_expr, map_subscript};
|
||||
use ruff_python_ast::identifier::Identifier;
|
||||
use ruff_python_ast::name::QualifiedName;
|
||||
use ruff_python_ast::{
|
||||
self as ast, Expr, ExprCall, ExprName, Int, Operator, ParameterWithDefault, Parameters, Stmt,
|
||||
self as ast, Expr, ExprCall, ExprName, Operator, ParameterWithDefault, Parameters, Stmt,
|
||||
StmtAssign,
|
||||
};
|
||||
use ruff_python_stdlib::typing::{
|
||||
|
@ -391,44 +391,19 @@ pub fn is_mutable_expr(expr: &Expr, semantic: &SemanticModel) -> bool {
|
|||
pub fn is_type_checking_block(stmt: &ast::StmtIf, semantic: &SemanticModel) -> bool {
|
||||
let ast::StmtIf { test, .. } = stmt;
|
||||
|
||||
if semantic.use_new_type_checking_block_detection_semantics() {
|
||||
return match test.as_ref() {
|
||||
// As long as the symbol's name is "TYPE_CHECKING" we will treat it like `typing.TYPE_CHECKING`
|
||||
// for this specific check even if it's defined somewhere else, like the current module.
|
||||
// Ex) `if TYPE_CHECKING:`
|
||||
Expr::Name(ast::ExprName { id, .. }) => {
|
||||
id == "TYPE_CHECKING"
|
||||
match test.as_ref() {
|
||||
// As long as the symbol's name is "TYPE_CHECKING" we will treat it like `typing.TYPE_CHECKING`
|
||||
// for this specific check even if it's defined somewhere else, like the current module.
|
||||
// Ex) `if TYPE_CHECKING:`
|
||||
Expr::Name(ast::ExprName { id, .. }) => {
|
||||
id == "TYPE_CHECKING"
|
||||
// Ex) `if TC:` with `from typing import TYPE_CHECKING as TC`
|
||||
|| semantic.match_typing_expr(test, "TYPE_CHECKING")
|
||||
}
|
||||
// Ex) `if typing.TYPE_CHECKING:`
|
||||
Expr::Attribute(ast::ExprAttribute { attr, .. }) => attr == "TYPE_CHECKING",
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
// Ex) `if typing.TYPE_CHECKING:`
|
||||
Expr::Attribute(ast::ExprAttribute { attr, .. }) => attr == "TYPE_CHECKING",
|
||||
_ => false,
|
||||
}
|
||||
|
||||
// Ex) `if False:`
|
||||
if is_const_false(test) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Ex) `if 0:`
|
||||
if matches!(
|
||||
test.as_ref(),
|
||||
Expr::NumberLiteral(ast::ExprNumberLiteral {
|
||||
value: ast::Number::Int(Int::ZERO),
|
||||
..
|
||||
})
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Ex) `if typing.TYPE_CHECKING:`
|
||||
if semantic.match_typing_expr(test, "TYPE_CHECKING") {
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns `true` if the [`ast::StmtIf`] is a version-checking block (e.g., `if sys.version_info >= ...:`).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue