mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:50:38 +00:00
Rename ContextFlags
to SemanticModelFlags
(#4611)
This commit is contained in:
parent
73e179ffab
commit
ba4c0a21fa
2 changed files with 43 additions and 40 deletions
|
@ -28,7 +28,7 @@ use ruff_python_semantic::binding::{
|
|||
Importation, StarImportation, SubmoduleImportation,
|
||||
};
|
||||
use ruff_python_semantic::definition::{ContextualizedDefinition, Module, ModuleKind};
|
||||
use ruff_python_semantic::model::{ContextFlags, ResolvedReference, SemanticModel};
|
||||
use ruff_python_semantic::model::{ResolvedReference, SemanticModel, SemanticModelFlags};
|
||||
use ruff_python_semantic::node::NodeId;
|
||||
use ruff_python_semantic::scope::{ClassDef, FunctionDef, Lambda, Scope, ScopeId, ScopeKind};
|
||||
use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
|
||||
|
@ -190,22 +190,22 @@ where
|
|||
.iter()
|
||||
.any(|alias| alias.name.as_str() == "annotations")
|
||||
{
|
||||
self.semantic_model.flags |= ContextFlags::FUTURE_ANNOTATIONS;
|
||||
self.semantic_model.flags |= SemanticModelFlags::FUTURE_ANNOTATIONS;
|
||||
}
|
||||
} else {
|
||||
self.semantic_model.flags |= ContextFlags::FUTURES_BOUNDARY;
|
||||
self.semantic_model.flags |= SemanticModelFlags::FUTURES_BOUNDARY;
|
||||
}
|
||||
}
|
||||
Stmt::Import(_) => {
|
||||
self.semantic_model.flags |= ContextFlags::FUTURES_BOUNDARY;
|
||||
self.semantic_model.flags |= SemanticModelFlags::FUTURES_BOUNDARY;
|
||||
}
|
||||
_ => {
|
||||
self.semantic_model.flags |= ContextFlags::FUTURES_BOUNDARY;
|
||||
self.semantic_model.flags |= SemanticModelFlags::FUTURES_BOUNDARY;
|
||||
if !self.semantic_model.seen_import_boundary()
|
||||
&& !helpers::is_assignment_to_a_dunder(stmt)
|
||||
&& !helpers::in_nested_block(self.semantic_model.parents())
|
||||
{
|
||||
self.semantic_model.flags |= ContextFlags::IMPORT_BOUNDARY;
|
||||
self.semantic_model.flags |= SemanticModelFlags::IMPORT_BOUNDARY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2210,7 +2210,7 @@ where
|
|||
self.visit_body(body);
|
||||
self.semantic_model.handled_exceptions.pop();
|
||||
|
||||
self.semantic_model.flags |= ContextFlags::EXCEPTION_HANDLER;
|
||||
self.semantic_model.flags |= SemanticModelFlags::EXCEPTION_HANDLER;
|
||||
for excepthandler in handlers {
|
||||
self.visit_excepthandler(excepthandler);
|
||||
}
|
||||
|
@ -2345,7 +2345,7 @@ where
|
|||
|
||||
fn visit_annotation(&mut self, expr: &'b Expr) {
|
||||
let flags_snapshot = self.semantic_model.flags;
|
||||
self.semantic_model.flags |= ContextFlags::ANNOTATION;
|
||||
self.semantic_model.flags |= SemanticModelFlags::ANNOTATION;
|
||||
self.visit_type_definition(expr);
|
||||
self.semantic_model.flags = flags_snapshot;
|
||||
}
|
||||
|
@ -2390,7 +2390,7 @@ where
|
|||
..
|
||||
})
|
||||
) {
|
||||
self.semantic_model.flags -= ContextFlags::BOOLEAN_TEST;
|
||||
self.semantic_model.flags -= SemanticModelFlags::BOOLEAN_TEST;
|
||||
}
|
||||
|
||||
// Pre-visit.
|
||||
|
@ -2434,7 +2434,7 @@ where
|
|||
}
|
||||
|
||||
if self.semantic_model.match_typing_expr(value, "Literal") {
|
||||
self.semantic_model.flags |= ContextFlags::LITERAL;
|
||||
self.semantic_model.flags |= SemanticModelFlags::LITERAL;
|
||||
}
|
||||
|
||||
if self.settings.rules.any_enabled(&[
|
||||
|
@ -4200,7 +4200,7 @@ where
|
|||
if self.semantic_model.in_subscript() {
|
||||
visitor::walk_expr(self, expr);
|
||||
} else if matches!(ctx, ExprContext::Store | ExprContext::Del) {
|
||||
self.semantic_model.flags |= ContextFlags::SUBSCRIPT;
|
||||
self.semantic_model.flags |= SemanticModelFlags::SUBSCRIPT;
|
||||
visitor::walk_expr(self, expr);
|
||||
} else {
|
||||
match analyze::typing::match_annotated_subscript(
|
||||
|
@ -4248,7 +4248,7 @@ where
|
|||
}
|
||||
}
|
||||
Expr::JoinedStr(_) => {
|
||||
self.semantic_model.flags |= ContextFlags::F_STRING;
|
||||
self.semantic_model.flags |= SemanticModelFlags::F_STRING;
|
||||
visitor::walk_expr(self, expr);
|
||||
}
|
||||
_ => visitor::walk_expr(self, expr),
|
||||
|
@ -4629,7 +4629,7 @@ impl<'a> Checker<'a> {
|
|||
/// Visit an body of [`Stmt`] nodes within a type-checking block.
|
||||
fn visit_type_checking_block(&mut self, body: &'a [Stmt]) {
|
||||
let snapshot = self.semantic_model.flags;
|
||||
self.semantic_model.flags |= ContextFlags::TYPE_CHECKING_BLOCK;
|
||||
self.semantic_model.flags |= SemanticModelFlags::TYPE_CHECKING_BLOCK;
|
||||
self.visit_body(body);
|
||||
self.semantic_model.flags = snapshot;
|
||||
}
|
||||
|
@ -4637,7 +4637,7 @@ impl<'a> Checker<'a> {
|
|||
/// Visit an [`Expr`], and treat it as a type definition.
|
||||
pub(crate) fn visit_type_definition(&mut self, expr: &'a Expr) {
|
||||
let snapshot = self.semantic_model.flags;
|
||||
self.semantic_model.flags |= ContextFlags::TYPE_DEFINITION;
|
||||
self.semantic_model.flags |= SemanticModelFlags::TYPE_DEFINITION;
|
||||
self.visit_expr(expr);
|
||||
self.semantic_model.flags = snapshot;
|
||||
}
|
||||
|
@ -4645,7 +4645,7 @@ impl<'a> Checker<'a> {
|
|||
/// Visit an [`Expr`], and treat it as _not_ a type definition.
|
||||
pub(crate) fn visit_non_type_definition(&mut self, expr: &'a Expr) {
|
||||
let snapshot = self.semantic_model.flags;
|
||||
self.semantic_model.flags -= ContextFlags::TYPE_DEFINITION;
|
||||
self.semantic_model.flags -= SemanticModelFlags::TYPE_DEFINITION;
|
||||
self.visit_expr(expr);
|
||||
self.semantic_model.flags = snapshot;
|
||||
}
|
||||
|
@ -4655,7 +4655,7 @@ impl<'a> Checker<'a> {
|
|||
/// its truthiness.
|
||||
pub(crate) fn visit_boolean_test(&mut self, expr: &'a Expr) {
|
||||
let snapshot = self.semantic_model.flags;
|
||||
self.semantic_model.flags |= ContextFlags::BOOLEAN_TEST;
|
||||
self.semantic_model.flags |= SemanticModelFlags::BOOLEAN_TEST;
|
||||
self.visit_expr(expr);
|
||||
self.semantic_model.flags = snapshot;
|
||||
}
|
||||
|
@ -5148,8 +5148,8 @@ impl<'a> Checker<'a> {
|
|||
for (expr, snapshot) in type_definitions {
|
||||
self.semantic_model.restore(snapshot);
|
||||
|
||||
self.semantic_model.flags |=
|
||||
ContextFlags::TYPE_DEFINITION | ContextFlags::FUTURE_TYPE_DEFINITION;
|
||||
self.semantic_model.flags |= SemanticModelFlags::TYPE_DEFINITION
|
||||
| SemanticModelFlags::FUTURE_TYPE_DEFINITION;
|
||||
self.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
@ -5178,12 +5178,14 @@ impl<'a> Checker<'a> {
|
|||
}
|
||||
|
||||
let type_definition_flag = match kind {
|
||||
AnnotationKind::Simple => ContextFlags::SIMPLE_STRING_TYPE_DEFINITION,
|
||||
AnnotationKind::Complex => ContextFlags::COMPLEX_STRING_TYPE_DEFINITION,
|
||||
AnnotationKind::Simple => SemanticModelFlags::SIMPLE_STRING_TYPE_DEFINITION,
|
||||
AnnotationKind::Complex => {
|
||||
SemanticModelFlags::COMPLEX_STRING_TYPE_DEFINITION
|
||||
}
|
||||
};
|
||||
|
||||
self.semantic_model.flags |=
|
||||
ContextFlags::TYPE_DEFINITION | type_definition_flag;
|
||||
SemanticModelFlags::TYPE_DEFINITION | type_definition_flag;
|
||||
self.visit_expr(expr);
|
||||
} else {
|
||||
if self
|
||||
|
|
|
@ -45,7 +45,7 @@ pub struct SemanticModel<'a> {
|
|||
pub body: &'a [Stmt],
|
||||
pub body_index: usize,
|
||||
// Internal, derivative state.
|
||||
pub flags: ContextFlags,
|
||||
pub flags: SemanticModelFlags,
|
||||
pub handled_exceptions: Vec<Exceptions>,
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ impl<'a> SemanticModel<'a> {
|
|||
shadowed_bindings: IntMap::default(),
|
||||
body: &[],
|
||||
body_index: 0,
|
||||
flags: ContextFlags::new(path),
|
||||
flags: SemanticModelFlags::new(path),
|
||||
handled_exceptions: Vec::default(),
|
||||
}
|
||||
}
|
||||
|
@ -581,29 +581,30 @@ impl<'a> SemanticModel<'a> {
|
|||
|
||||
/// Return `true` if the context is in a type annotation.
|
||||
pub const fn in_annotation(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::ANNOTATION)
|
||||
self.flags.contains(SemanticModelFlags::ANNOTATION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a type definition.
|
||||
pub const fn in_type_definition(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::TYPE_DEFINITION)
|
||||
self.flags.contains(SemanticModelFlags::TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a "simple" string type definition.
|
||||
pub const fn in_simple_string_type_definition(&self) -> bool {
|
||||
self.flags
|
||||
.contains(ContextFlags::SIMPLE_STRING_TYPE_DEFINITION)
|
||||
.contains(SemanticModelFlags::SIMPLE_STRING_TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a "complex" string type definition.
|
||||
pub const fn in_complex_string_type_definition(&self) -> bool {
|
||||
self.flags
|
||||
.contains(ContextFlags::COMPLEX_STRING_TYPE_DEFINITION)
|
||||
.contains(SemanticModelFlags::COMPLEX_STRING_TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a `__future__` type definition.
|
||||
pub const fn in_future_type_definition(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::FUTURE_TYPE_DEFINITION)
|
||||
self.flags
|
||||
.contains(SemanticModelFlags::FUTURE_TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in any kind of deferred type definition.
|
||||
|
@ -615,54 +616,54 @@ impl<'a> SemanticModel<'a> {
|
|||
|
||||
/// Return `true` if the context is in an exception handler.
|
||||
pub const fn in_exception_handler(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::EXCEPTION_HANDLER)
|
||||
self.flags.contains(SemanticModelFlags::EXCEPTION_HANDLER)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in an f-string.
|
||||
pub const fn in_f_string(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::F_STRING)
|
||||
self.flags.contains(SemanticModelFlags::F_STRING)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in boolean test.
|
||||
pub const fn in_boolean_test(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::BOOLEAN_TEST)
|
||||
self.flags.contains(SemanticModelFlags::BOOLEAN_TEST)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a `typing::Literal` annotation.
|
||||
pub const fn in_literal(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::LITERAL)
|
||||
self.flags.contains(SemanticModelFlags::LITERAL)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a subscript expression.
|
||||
pub const fn in_subscript(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::SUBSCRIPT)
|
||||
self.flags.contains(SemanticModelFlags::SUBSCRIPT)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a type-checking block.
|
||||
pub const fn in_type_checking_block(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::TYPE_CHECKING_BLOCK)
|
||||
self.flags.contains(SemanticModelFlags::TYPE_CHECKING_BLOCK)
|
||||
}
|
||||
|
||||
/// Return `true` if the context has traversed past the "top-of-file" import boundary.
|
||||
pub const fn seen_import_boundary(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::IMPORT_BOUNDARY)
|
||||
self.flags.contains(SemanticModelFlags::IMPORT_BOUNDARY)
|
||||
}
|
||||
|
||||
/// Return `true` if the context has traverse past the `__future__` import boundary.
|
||||
pub const fn seen_futures_boundary(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::FUTURES_BOUNDARY)
|
||||
self.flags.contains(SemanticModelFlags::FUTURES_BOUNDARY)
|
||||
}
|
||||
|
||||
/// Return `true` if `__future__`-style type annotations are enabled.
|
||||
pub const fn future_annotations(&self) -> bool {
|
||||
self.flags.contains(ContextFlags::FUTURE_ANNOTATIONS)
|
||||
self.flags.contains(SemanticModelFlags::FUTURE_ANNOTATIONS)
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// Flags indicating the current context of the analysis.
|
||||
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
|
||||
pub struct ContextFlags: u16 {
|
||||
pub struct SemanticModelFlags: u16 {
|
||||
/// The context is in a type annotation.
|
||||
///
|
||||
/// For example, the context could be visiting `int` in:
|
||||
|
@ -823,7 +824,7 @@ bitflags! {
|
|||
}
|
||||
}
|
||||
|
||||
impl ContextFlags {
|
||||
impl SemanticModelFlags {
|
||||
pub fn new(path: &Path) -> Self {
|
||||
let mut flags = Self::default();
|
||||
if is_python_stub_file(path) {
|
||||
|
@ -839,7 +840,7 @@ pub struct Snapshot {
|
|||
scope_id: ScopeId,
|
||||
stmt_id: Option<NodeId>,
|
||||
definition_id: DefinitionId,
|
||||
flags: ContextFlags,
|
||||
flags: SemanticModelFlags,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue