mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Fix context-to-model references in SemanticModel
documentation (#6004)
This commit is contained in:
parent
45a24912a6
commit
4aac801277
1 changed files with 50 additions and 50 deletions
|
@ -995,7 +995,7 @@ impl<'a> SemanticModel<'a> {
|
|||
exceptions
|
||||
}
|
||||
|
||||
/// Generate a [`Snapshot`] of the current context.
|
||||
/// Generate a [`Snapshot`] of the current model.
|
||||
pub fn snapshot(&self) -> Snapshot {
|
||||
Snapshot {
|
||||
scope_id: self.scope_id,
|
||||
|
@ -1005,7 +1005,7 @@ impl<'a> SemanticModel<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Restore the context to the given [`Snapshot`].
|
||||
/// Restore the model to the given [`Snapshot`].
|
||||
pub fn restore(&mut self, snapshot: Snapshot) {
|
||||
let Snapshot {
|
||||
scope_id,
|
||||
|
@ -1019,53 +1019,53 @@ impl<'a> SemanticModel<'a> {
|
|||
self.flags = flags;
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a type annotation.
|
||||
/// Return `true` if the model is in a type annotation.
|
||||
pub const fn in_annotation(&self) -> bool {
|
||||
self.in_typing_only_annotation() || self.in_runtime_annotation()
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a typing-only type annotation.
|
||||
/// Return `true` if the model is in a typing-only type annotation.
|
||||
pub const fn in_typing_only_annotation(&self) -> bool {
|
||||
self.flags
|
||||
.contains(SemanticModelFlags::TYPING_ONLY_ANNOTATION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a runtime-required type annotation.
|
||||
/// Return `true` if the model is in a runtime-required type annotation.
|
||||
pub const fn in_runtime_annotation(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::RUNTIME_ANNOTATION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a type definition.
|
||||
/// Return `true` if the model is in a type definition.
|
||||
pub const fn in_type_definition(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a "simple" string type definition.
|
||||
/// Return `true` if the model is in a "simple" string type definition.
|
||||
pub const fn in_simple_string_type_definition(&self) -> bool {
|
||||
self.flags
|
||||
.contains(SemanticModelFlags::SIMPLE_STRING_TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a "complex" string type definition.
|
||||
/// Return `true` if the model is in a "complex" string type definition.
|
||||
pub const fn in_complex_string_type_definition(&self) -> bool {
|
||||
self.flags
|
||||
.contains(SemanticModelFlags::COMPLEX_STRING_TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a `__future__` type definition.
|
||||
/// Return `true` if the model is in a `__future__` type definition.
|
||||
pub const fn in_future_type_definition(&self) -> bool {
|
||||
self.flags
|
||||
.contains(SemanticModelFlags::FUTURE_TYPE_DEFINITION)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in any kind of deferred type definition.
|
||||
/// Return `true` if the model is in any kind of deferred type definition.
|
||||
pub const fn in_deferred_type_definition(&self) -> bool {
|
||||
self.in_simple_string_type_definition()
|
||||
|| self.in_complex_string_type_definition()
|
||||
|| self.in_future_type_definition()
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a forward type reference.
|
||||
/// Return `true` if the model is in a forward type reference.
|
||||
///
|
||||
/// Includes deferred string types, and future types in annotations.
|
||||
///
|
||||
|
@ -1086,42 +1086,42 @@ impl<'a> SemanticModel<'a> {
|
|||
|| (self.in_future_type_definition() && self.in_typing_only_annotation())
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in an exception handler.
|
||||
/// Return `true` if the model is in an exception handler.
|
||||
pub const fn in_exception_handler(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::EXCEPTION_HANDLER)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in an f-string.
|
||||
/// Return `true` if the model is in an f-string.
|
||||
pub const fn in_f_string(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::F_STRING)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in boolean test.
|
||||
/// Return `true` if the model is in boolean test.
|
||||
pub const fn in_boolean_test(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::BOOLEAN_TEST)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a `typing::Literal` annotation.
|
||||
/// Return `true` if the model is in a `typing::Literal` annotation.
|
||||
pub const fn in_literal(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::LITERAL)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a subscript expression.
|
||||
/// Return `true` if the model is in a subscript expression.
|
||||
pub const fn in_subscript(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::SUBSCRIPT)
|
||||
}
|
||||
|
||||
/// Return `true` if the context is in a type-checking block.
|
||||
/// Return `true` if the model is in a type-checking block.
|
||||
pub const fn in_type_checking_block(&self) -> bool {
|
||||
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 model has traversed past the "top-of-file" import boundary.
|
||||
pub const fn seen_import_boundary(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::IMPORT_BOUNDARY)
|
||||
}
|
||||
|
||||
/// Return `true` if the context has traverse past the `__future__` import boundary.
|
||||
/// Return `true` if the model has traverse past the `__future__` import boundary.
|
||||
pub const fn seen_futures_boundary(&self) -> bool {
|
||||
self.flags.contains(SemanticModelFlags::FUTURES_BOUNDARY)
|
||||
}
|
||||
|
@ -1196,12 +1196,12 @@ impl ShadowedBinding {
|
|||
}
|
||||
|
||||
bitflags! {
|
||||
/// Flags indicating the current context of the analysis.
|
||||
/// Flags indicating the current model state.
|
||||
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
|
||||
pub struct SemanticModelFlags: u16 {
|
||||
/// The context is in a typing-time-only type annotation.
|
||||
/// The model is in a typing-time-only type annotation.
|
||||
///
|
||||
/// For example, the context could be visiting `int` in:
|
||||
/// For example, the model could be visiting `int` in:
|
||||
/// ```python
|
||||
/// def foo() -> int:
|
||||
/// x: int = 1
|
||||
|
@ -1214,9 +1214,9 @@ bitflags! {
|
|||
/// are any annotated assignments in module or class scopes.
|
||||
const TYPING_ONLY_ANNOTATION = 1 << 0;
|
||||
|
||||
/// The context is in a runtime type annotation.
|
||||
/// The model is in a runtime type annotation.
|
||||
///
|
||||
/// For example, the context could be visiting `int` in:
|
||||
/// For example, the model could be visiting `int` in:
|
||||
/// ```python
|
||||
/// def foo(x: int) -> int:
|
||||
/// ...
|
||||
|
@ -1230,9 +1230,9 @@ bitflags! {
|
|||
/// are any annotated assignments in module or class scopes.
|
||||
const RUNTIME_ANNOTATION = 1 << 1;
|
||||
|
||||
/// The context is in a type definition.
|
||||
/// The model is in a type definition.
|
||||
///
|
||||
/// For example, the context could be visiting `int` in:
|
||||
/// For example, the model could be visiting `int` in:
|
||||
/// ```python
|
||||
/// from typing import NewType
|
||||
///
|
||||
|
@ -1244,9 +1244,9 @@ bitflags! {
|
|||
/// doesn't appear in a type annotation context, but rather in a type definition.
|
||||
const TYPE_DEFINITION = 1 << 2;
|
||||
|
||||
/// The context is in a (deferred) "simple" string type definition.
|
||||
/// The model is in a (deferred) "simple" string type definition.
|
||||
///
|
||||
/// For example, the context could be visiting `list[int]` in:
|
||||
/// For example, the model could be visiting `list[int]` in:
|
||||
/// ```python
|
||||
/// x: "list[int]" = []
|
||||
/// ```
|
||||
|
@ -1255,9 +1255,9 @@ bitflags! {
|
|||
/// as opposed to an implicitly concatenated string literal.
|
||||
const SIMPLE_STRING_TYPE_DEFINITION = 1 << 3;
|
||||
|
||||
/// The context is in a (deferred) "complex" string type definition.
|
||||
/// The model is in a (deferred) "complex" string type definition.
|
||||
///
|
||||
/// For example, the context could be visiting `list[int]` in:
|
||||
/// For example, the model could be visiting `list[int]` in:
|
||||
/// ```python
|
||||
/// x: ("list" "[int]") = []
|
||||
/// ```
|
||||
|
@ -1266,9 +1266,9 @@ bitflags! {
|
|||
/// string literals. These are uncommon but valid.
|
||||
const COMPLEX_STRING_TYPE_DEFINITION = 1 << 4;
|
||||
|
||||
/// The context is in a (deferred) `__future__` type definition.
|
||||
/// The model is in a (deferred) `__future__` type definition.
|
||||
///
|
||||
/// For example, the context could be visiting `list[int]` in:
|
||||
/// For example, the model could be visiting `list[int]` in:
|
||||
/// ```python
|
||||
/// from __future__ import annotations
|
||||
///
|
||||
|
@ -1279,9 +1279,9 @@ bitflags! {
|
|||
/// is enabled via `from __future__ import annotations`.
|
||||
const FUTURE_TYPE_DEFINITION = 1 << 5;
|
||||
|
||||
/// The context is in an exception handler.
|
||||
/// The model is in an exception handler.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// try:
|
||||
/// ...
|
||||
|
@ -1290,17 +1290,17 @@ bitflags! {
|
|||
/// ```
|
||||
const EXCEPTION_HANDLER = 1 << 6;
|
||||
|
||||
/// The context is in an f-string.
|
||||
/// The model is in an f-string.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// f'{x}'
|
||||
/// ```
|
||||
const F_STRING = 1 << 7;
|
||||
|
||||
/// The context is in a boolean test.
|
||||
/// The model is in a boolean test.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// if x:
|
||||
/// ...
|
||||
|
@ -1310,26 +1310,26 @@ bitflags! {
|
|||
/// not used, only its truthiness.
|
||||
const BOOLEAN_TEST = 1 << 8;
|
||||
|
||||
/// The context is in a `typing::Literal` annotation.
|
||||
/// The model is in a `typing::Literal` annotation.
|
||||
///
|
||||
/// For example, the context could be visiting any of `"A"`, `"B"`, or `"C"` in:
|
||||
/// For example, the model could be visiting any of `"A"`, `"B"`, or `"C"` in:
|
||||
/// ```python
|
||||
/// def f(x: Literal["A", "B", "C"]):
|
||||
/// ...
|
||||
/// ```
|
||||
const LITERAL = 1 << 9;
|
||||
|
||||
/// The context is in a subscript expression.
|
||||
/// The model is in a subscript expression.
|
||||
///
|
||||
/// For example, the context could be visiting `x["a"]` in:
|
||||
/// For example, the model could be visiting `x["a"]` in:
|
||||
/// ```python
|
||||
/// x["a"]["b"]
|
||||
/// ```
|
||||
const SUBSCRIPT = 1 << 10;
|
||||
|
||||
/// The context is in a type-checking block.
|
||||
/// The model is in a type-checking block.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// from typing import TYPE_CHECKING
|
||||
///
|
||||
|
@ -1339,9 +1339,9 @@ bitflags! {
|
|||
/// ```
|
||||
const TYPE_CHECKING_BLOCK = 1 << 11;
|
||||
|
||||
/// The context has traversed past the "top-of-file" import boundary.
|
||||
/// The model has traversed past the "top-of-file" import boundary.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// import os
|
||||
///
|
||||
|
@ -1352,9 +1352,9 @@ bitflags! {
|
|||
/// ```
|
||||
const IMPORT_BOUNDARY = 1 << 12;
|
||||
|
||||
/// The context has traversed past the `__future__` import boundary.
|
||||
/// The model has traversed past the `__future__` import boundary.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// from __future__ import annotations
|
||||
///
|
||||
|
@ -1367,9 +1367,9 @@ bitflags! {
|
|||
/// any other non-`__future__`-importing statements.
|
||||
const FUTURES_BOUNDARY = 1 << 13;
|
||||
|
||||
/// `__future__`-style type annotations are enabled in this context.
|
||||
/// `__future__`-style type annotations are enabled in this model.
|
||||
///
|
||||
/// For example, the context could be visiting `x` in:
|
||||
/// For example, the model could be visiting `x` in:
|
||||
/// ```python
|
||||
/// from __future__ import annotations
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue