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