pub struct SemanticModelFlags(/* private fields */);Expand description
Flags indicating the current model state.
Implementations§
Source§impl SemanticModelFlags
impl SemanticModelFlags
Sourcepub const TYPING_ONLY_ANNOTATION: Self
pub const TYPING_ONLY_ANNOTATION: Self
The model is in a type annotation that will only be evaluated when running a type checker.
For example, the model could be visiting int in:
def foo() -> int:
x: int = 1In this case, Python doesn’t require that the type annotation be evaluated at runtime.
If from __future__ import annotations is used, all annotations are evaluated at
typing time. Otherwise, all function argument annotations are evaluated at runtime, as
are any annotated assignments in module or class scopes.
Sourcepub const RUNTIME_EVALUATED_ANNOTATION: Self
pub const RUNTIME_EVALUATED_ANNOTATION: Self
The model is in a type annotation that will be evaluated at runtime.
For example, the model could be visiting int in:
def foo(x: int) -> int:
...In this case, Python requires that the type annotation be evaluated at runtime,
as it needs to be available on the function’s __annotations__ attribute.
If from __future__ import annotations is used, all annotations are evaluated at
typing time. Otherwise, all function argument annotations are evaluated at runtime, as
are any annotated assignments in module or class scopes.
Sourcepub const RUNTIME_REQUIRED_ANNOTATION: Self
pub const RUNTIME_REQUIRED_ANNOTATION: Self
The model is in a type annotation that is required to be available at runtime.
For example, the context could be visiting int in:
from pydantic import BaseModel
class Foo(BaseModel):
x: intIn this case, Pydantic requires that the type annotation be available at runtime in order to perform runtime type-checking.
Unlike [RUNTIME_EVALUATED_ANNOTATION], annotations that are marked as
[RUNTIME_REQUIRED_ANNOTATION] cannot be deferred to typing time via conversion to a
forward reference (e.g., by wrapping the type in quotes), as the annotations are not
only required by the Python interpreter, but by runtime type checkers too.
Sourcepub const TYPE_DEFINITION: Self
pub const TYPE_DEFINITION: Self
The model is in a type definition.
For example, the model could be visiting int in:
from typing import NewType
UserId = NewType("UserId", int)All type annotations are also type definitions, but the converse is not true.
In our example, int is a type definition but not a type annotation, as it
doesn’t appear in a type annotation context, but rather in a type definition.
Sourcepub const SIMPLE_STRING_TYPE_DEFINITION: Self
pub const SIMPLE_STRING_TYPE_DEFINITION: Self
The model is in a (deferred) “simple” string type definition.
For example, the model could be visiting list[int] in:
x: "list[int]" = []“Simple” string type definitions are those that consist of a single string literal, as opposed to an implicitly concatenated string literal.
Note that this flag is only set when we are actually visiting the deferred definition, not when we “pass by” it when initially traversing the source tree.
Sourcepub const COMPLEX_STRING_TYPE_DEFINITION: Self
pub const COMPLEX_STRING_TYPE_DEFINITION: Self
The model is in a (deferred) “complex” string type definition.
For example, the model could be visiting list[int] in:
x: ("list" "[int]") = []“Complex” string type definitions are those that consist of a implicitly concatenated string literals. These are uncommon but valid.
Note that this flag is only set when we are actually visiting the deferred definition, not when we “pass by” it when initially traversing the source tree.
Sourcepub const FUTURE_TYPE_DEFINITION: Self
pub const FUTURE_TYPE_DEFINITION: Self
The model is in a (deferred) __future__ type definition.
For example, the model could be visiting list[int] in:
from __future__ import annotations
x: list[int] = []__future__-style type annotations are only enabled if the annotations feature
is enabled via from __future__ import annotations.
This flag should only be set in contexts where PEP-563 semantics are relevant to
resolution of the type definition. For example, the flag should not be set
in the following context, because the type definition is not inside a type annotation,
so whether or not from __future__ import annotations is active has no relevance:
from __future__ import annotations
from typing import TypeAlias
X: TypeAlias = list[int]Note also that this flag is only set when we are actually visiting the deferred definition, not when we “pass by” it when initially traversing the source tree.
Sourcepub const EXCEPTION_HANDLER: Self
pub const EXCEPTION_HANDLER: Self
The model is in an exception handler.
For example, the model could be visiting x in:
try:
...
except Exception:
x: int = 1Sourcepub const F_STRING: Self
pub const F_STRING: Self
The model is in an f-string.
For example, the model could be visiting x in:
f'{x}'Sourcepub const BOOLEAN_TEST: Self
pub const BOOLEAN_TEST: Self
The model is in a boolean test.
For example, the model could be visiting x in:
if x:
...The implication is that the actual value returned by the current expression is not used, only its truthiness.
Sourcepub const TYPING_LITERAL: Self
pub const TYPING_LITERAL: Self
The model is in a typing::Literal annotation.
For example, the model could be visiting any of "A", "B", or "C" in:
def f(x: Literal["A", "B", "C"]):
...Sourcepub const SUBSCRIPT: Self
pub const SUBSCRIPT: Self
The model is in a subscript expression.
For example, the model could be visiting x["a"] in:
x["a"]["b"]Sourcepub const TYPE_CHECKING_BLOCK: Self
pub const TYPE_CHECKING_BLOCK: Self
The model is in a type-checking block.
For example, the model could be visiting x in:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
x: int = 1Sourcepub const IMPORT_BOUNDARY: Self
pub const IMPORT_BOUNDARY: Self
The model has traversed past the “top-of-file” import boundary.
For example, the model could be visiting x in:
import os
def f() -> None:
...
x: int = 1Sourcepub const FUTURE_ANNOTATIONS: Self
pub const FUTURE_ANNOTATIONS: Self
The model is in a file that has from __future__ import annotations
at the top of the module.
For example, the model could be visiting x in:
from __future__ import annotations
def f(x: int) -> int:
...Sourcepub const FUTURE_ANNOTATIONS_OR_STUB: Self
pub const FUTURE_ANNOTATIONS_OR_STUB: Self
__future__-style type annotations are enabled in this model.
That could be because it’s a stub file,
or it could be because it’s a non-stub file that has from __future__ import annotations
at the top of the module.
Sourcepub const MODULE_DOCSTRING_BOUNDARY: Self
pub const MODULE_DOCSTRING_BOUNDARY: Self
The model has traversed past the module docstring.
For example, the model could be visiting x in:
"""Module docstring."""
x: int = 1Sourcepub const TYPE_PARAM_DEFINITION: Self
pub const TYPE_PARAM_DEFINITION: Self
The model is in a (deferred) type parameter definition.
For example, the model could be visiting T, P or Ts in:
class Foo[T, *Ts, **P]: passNote that this flag is not set for “pre-PEP-695” TypeVars, ParamSpecs or TypeVarTuples. None of the following would lead to the flag being set:
from typing import TypeVar, ParamSpec, TypeVarTuple
T = TypeVar("T")
P = ParamSpec("P")
Ts = TypeVarTuple("Ts")Note also that this flag is only set when we are actually visiting the deferred definition, not when we “pass by” it when initially traversing the source tree.
Sourcepub const NAMED_EXPRESSION_ASSIGNMENT: Self
pub const NAMED_EXPRESSION_ASSIGNMENT: Self
The model is in a named expression assignment.
For example, the model could be visiting x in:
if (x := 1): ...Sourcepub const PEP_257_DOCSTRING: Self
pub const PEP_257_DOCSTRING: Self
The model is in a docstring as described in PEP 257.
For example, the model could be visiting either the module, class, or function docstring in:
"""Module docstring."""
class Foo:
"""Class docstring."""
pass
def foo():
"""Function docstring."""
passSourcepub const DUNDER_ALL_DEFINITION: Self
pub const DUNDER_ALL_DEFINITION: Self
The model is visiting the r.h.s. of a module-level __all__ definition.
This could be any module-level statement that assigns or alters __all__,
for example:
__all__ = ["foo"]
__all__: str = ["foo"]
__all__ = ("bar",)
__all__ += ("baz,")Sourcepub const INTERPOLATED_STRING_REPLACEMENT_FIELD: Self
pub const INTERPOLATED_STRING_REPLACEMENT_FIELD: Self
The model is in an f-string replacement field.
For example, the model could be visiting x or y in:
f"first {x} second {y}"Sourcepub const CLASS_BASE: Self
pub const CLASS_BASE: Self
The model is visiting the bases tuple of a class.
For example, the model could be visiting Foo or Bar in:
class Baz(Foo, Bar):
passSourcepub const DEFERRED_CLASS_BASE: Self
pub const DEFERRED_CLASS_BASE: Self
The model is visiting a class base that was initially deferred while traversing the AST. (This only happens in stub files.)
Sourcepub const ATTRIBUTE_DOCSTRING: Self
pub const ATTRIBUTE_DOCSTRING: Self
The model is in an attribute docstring.
An attribute docstring is a string literal immediately following an assignment or an annotated assignment statement. The context in which this is valid are:
- At the top level of a module
- At the top level of a class definition i.e., a class attribute
For example:
a = 1
"""This is an attribute docstring for `a` variable"""
class Foo:
b = 1
"""This is an attribute docstring for `Foo.b` class variable"""Unlike other kinds of docstrings as described in PEP 257, attribute docstrings are discarded at runtime. However, they are used by some documentation renderers and static-analysis tools.
Sourcepub const ANNOTATED_TYPE_ALIAS: Self
pub const ANNOTATED_TYPE_ALIAS: Self
The model is in the value expression of a PEP 613 explicit type alias.
For example:
from typing import TypeAlias
OptStr: TypeAlias = str | None # We're visiting the RHSSourcepub const DEFERRED_TYPE_ALIAS: Self
pub const DEFERRED_TYPE_ALIAS: Self
The model is in the value expression of a PEP 695 type statement.
For example:
type OptStr = str | None # We're visiting the RHSSourcepub const ASSERT_STATEMENT: Self
pub const ASSERT_STATEMENT: Self
The model is visiting an assert statement.
For example, the model might be visiting y in
assert (y := x**2) > 42, ySourcepub const NO_TYPE_CHECK: Self
pub const NO_TYPE_CHECK: Self
The model is in a [@no_type_check] context.
This is used to skip type checking when the @no_type_check decorator is found.
For example (adapted from #13824):
from typing import no_type_check
@no_type_check
def fn(arg: "A") -> "R":
passSourcepub const T_STRING: Self
pub const T_STRING: Self
The model is in a t-string.
For example, the model could be visiting x in:
t'{x}'Sourcepub const ORELSE: Self
pub const ORELSE: Self
The model is in the body of an else clause.
For example, the model could be visiting x in:
try:
...
except Exception:
...
else:
print(x)Sourcepub const ANNOTATION: Self
pub const ANNOTATION: Self
The context is in any type annotation.
Sourcepub const STRING_TYPE_DEFINITION: Self
pub const STRING_TYPE_DEFINITION: Self
The context is in any string type definition.
Sourcepub const DEFERRED_TYPE_DEFINITION: Self
pub const DEFERRED_TYPE_DEFINITION: Self
The context is in any deferred type definition.
Sourcepub const TYPING_CONTEXT: Self
pub const TYPING_CONTEXT: Self
The context is in a typing-only context.
Sourcepub const TYPE_ALIAS: Self
pub const TYPE_ALIAS: Self
The context is in any type alias.
Source§impl SemanticModelFlags
impl SemanticModelFlags
Sourcepub const fn bits(&self) -> u32
pub const fn bits(&self) -> u32
Get the underlying bits value.
The returned value is exactly the bits set in this flags value.
Sourcepub const fn from_bits(bits: u32) -> Option<Self>
pub const fn from_bits(bits: u32) -> Option<Self>
Convert from a bits value.
This method will return None if any unknown bits are set.
Sourcepub const fn from_bits_truncate(bits: u32) -> Self
pub const fn from_bits_truncate(bits: u32) -> Self
Convert from a bits value, unsetting any unknown bits.
Sourcepub const fn from_bits_retain(bits: u32) -> Self
pub const fn from_bits_retain(bits: u32) -> Self
Convert from a bits value exactly.
Sourcepub fn from_name(name: &str) -> Option<Self>
pub fn from_name(name: &str) -> Option<Self>
Get a flags value with the bits of a flag with the given name set.
This method will return None if name is empty or doesn’t
correspond to any named flag.
Sourcepub const fn intersects(&self, other: Self) -> bool
pub const fn intersects(&self, other: Self) -> bool
Whether any set bits in other are also set in self.
Sourcepub const fn contains(&self, other: Self) -> bool
pub const fn contains(&self, other: Self) -> bool
Whether all set bits in other are also set in self.
Sourcepub fn remove(&mut self, other: Self)
pub fn remove(&mut self, other: Self)
The intersection of self with the complement of other (&!).
This method is not equivalent to self & !other when other has unknown bits set.
remove won’t truncate other, but the ! operator will.
Sourcepub fn toggle(&mut self, other: Self)
pub fn toggle(&mut self, other: Self)
The bitwise exclusive-or (^) of the bits in self and other.
Sourcepub fn set(&mut self, other: Self, value: bool)
pub fn set(&mut self, other: Self, value: bool)
Call insert when value is true or remove when value is false.
Sourcepub const fn intersection(self, other: Self) -> Self
pub const fn intersection(self, other: Self) -> Self
The bitwise and (&) of the bits in self and other.
Sourcepub const fn union(self, other: Self) -> Self
pub const fn union(self, other: Self) -> Self
The bitwise or (|) of the bits in self and other.
Sourcepub const fn difference(self, other: Self) -> Self
pub const fn difference(self, other: Self) -> Self
The intersection of self with the complement of other (&!).
This method is not equivalent to self & !other when other has unknown bits set.
difference won’t truncate other, but the ! operator will.
Sourcepub const fn symmetric_difference(self, other: Self) -> Self
pub const fn symmetric_difference(self, other: Self) -> Self
The bitwise exclusive-or (^) of the bits in self and other.
Sourcepub const fn complement(self) -> Self
pub const fn complement(self) -> Self
The bitwise negation (!) of the bits in self, truncating the result.
Source§impl SemanticModelFlags
impl SemanticModelFlags
Sourcepub const fn iter(&self) -> Iter<SemanticModelFlags> ⓘ
pub const fn iter(&self) -> Iter<SemanticModelFlags> ⓘ
Yield a set of contained flags values.
Each yielded flags value will correspond to a defined named flag. Any unknown bits will be yielded together as a final flags value.
Sourcepub const fn iter_names(&self) -> IterNames<SemanticModelFlags> ⓘ
pub const fn iter_names(&self) -> IterNames<SemanticModelFlags> ⓘ
Yield a set of contained named flags values.
This method is like iter, except only yields bits in contained named flags.
Any unknown bits, or bits not corresponding to a contained flag will not be yielded.
Trait Implementations§
Source§impl Binary for SemanticModelFlags
impl Binary for SemanticModelFlags
Source§impl BitAnd for SemanticModelFlags
impl BitAnd for SemanticModelFlags
Source§impl BitAndAssign for SemanticModelFlags
impl BitAndAssign for SemanticModelFlags
Source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
The bitwise and (&) of the bits in self and other.
Source§impl BitOr for SemanticModelFlags
impl BitOr for SemanticModelFlags
Source§fn bitor(self, other: SemanticModelFlags) -> Self
fn bitor(self, other: SemanticModelFlags) -> Self
The bitwise or (|) of the bits in self and other.
Source§type Output = SemanticModelFlags
type Output = SemanticModelFlags
| operator.Source§impl BitOrAssign for SemanticModelFlags
impl BitOrAssign for SemanticModelFlags
Source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
The bitwise or (|) of the bits in self and other.
Source§impl BitXor for SemanticModelFlags
impl BitXor for SemanticModelFlags
Source§impl BitXorAssign for SemanticModelFlags
impl BitXorAssign for SemanticModelFlags
Source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
The bitwise exclusive-or (^) of the bits in self and other.
Source§impl Clone for SemanticModelFlags
impl Clone for SemanticModelFlags
Source§fn clone(&self) -> SemanticModelFlags
fn clone(&self) -> SemanticModelFlags
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for SemanticModelFlags
Source§impl Debug for SemanticModelFlags
impl Debug for SemanticModelFlags
Source§impl Default for SemanticModelFlags
impl Default for SemanticModelFlags
Source§fn default() -> SemanticModelFlags
fn default() -> SemanticModelFlags
impl Eq for SemanticModelFlags
Source§impl Extend<SemanticModelFlags> for SemanticModelFlags
impl Extend<SemanticModelFlags> for SemanticModelFlags
Source§fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
The bitwise or (|) of the bits in each flags value.
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Flags for SemanticModelFlags
impl Flags for SemanticModelFlags
Source§const FLAGS: &'static [Flag<SemanticModelFlags>]
const FLAGS: &'static [Flag<SemanticModelFlags>]
Source§fn from_bits_retain(bits: u32) -> SemanticModelFlags
fn from_bits_retain(bits: u32) -> SemanticModelFlags
Source§fn all_named() -> SemanticModelFlags
fn all_named() -> SemanticModelFlags
Source§fn known_bits(&self) -> Self::Bits
fn known_bits(&self) -> Self::Bits
Source§fn unknown_bits(&self) -> Self::Bits
fn unknown_bits(&self) -> Self::Bits
Source§fn contains_unknown_bits(&self) -> bool
fn contains_unknown_bits(&self) -> bool
true if any unknown bits are set.Source§fn from_bits_truncate(bits: Self::Bits) -> Self
fn from_bits_truncate(bits: Self::Bits) -> Self
Source§fn from_name(name: &str) -> Option<Self>
fn from_name(name: &str) -> Option<Self>
Source§fn iter_names(&self) -> IterNames<Self> ⓘ
fn iter_names(&self) -> IterNames<Self> ⓘ
Source§fn iter_defined_names() -> IterDefinedNames<Self> ⓘ
fn iter_defined_names() -> IterDefinedNames<Self> ⓘ
Self::FLAGS.Source§fn iter_equal_names(&self) -> IterEqualNames<Self> ⓘ
fn iter_equal_names(&self) -> IterEqualNames<Self> ⓘ
Source§fn intersects(&self, other: Self) -> boolwhere
Self: Sized,
fn intersects(&self, other: Self) -> boolwhere
Self: Sized,
other are also set in self.Source§fn contains(&self, other: Self) -> boolwhere
Self: Sized,
fn contains(&self, other: Self) -> boolwhere
Self: Sized,
other are also set in self.Source§fn insert(&mut self, other: Self)where
Self: Sized,
fn insert(&mut self, other: Self)where
Self: Sized,
|) of the bits in self and other.Source§fn toggle(&mut self, other: Self)where
Self: Sized,
fn toggle(&mut self, other: Self)where
Self: Sized,
^) of the bits in self and other.Source§fn intersection(self, other: Self) -> Self
fn intersection(self, other: Self) -> Self
&) of the bits in self and other.Source§fn difference(self, other: Self) -> Self
fn difference(self, other: Self) -> Self
Source§fn symmetric_difference(self, other: Self) -> Self
fn symmetric_difference(self, other: Self) -> Self
^) of the bits in self and other.Source§fn complement(self) -> Self
fn complement(self) -> Self
!) of the bits in self, truncating the result.Source§impl FromIterator<SemanticModelFlags> for SemanticModelFlags
impl FromIterator<SemanticModelFlags> for SemanticModelFlags
Source§fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
The bitwise or (|) of the bits in each flags value.
Source§impl IntoIterator for SemanticModelFlags
impl IntoIterator for SemanticModelFlags
Source§impl LowerHex for SemanticModelFlags
impl LowerHex for SemanticModelFlags
Source§impl Not for SemanticModelFlags
impl Not for SemanticModelFlags
Source§impl Octal for SemanticModelFlags
impl Octal for SemanticModelFlags
Source§impl PartialEq for SemanticModelFlags
impl PartialEq for SemanticModelFlags
Source§impl PublicFlags for SemanticModelFlags
impl PublicFlags for SemanticModelFlags
impl StructuralPartialEq for SemanticModelFlags
Source§impl Sub for SemanticModelFlags
impl Sub for SemanticModelFlags
Source§fn sub(self, other: Self) -> Self
fn sub(self, other: Self) -> Self
The intersection of self with the complement of other (&!).
This method is not equivalent to self & !other when other has unknown bits set.
difference won’t truncate other, but the ! operator will.
Source§type Output = SemanticModelFlags
type Output = SemanticModelFlags
- operator.Source§impl SubAssign for SemanticModelFlags
impl SubAssign for SemanticModelFlags
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
The intersection of self with the complement of other (&!).
This method is not equivalent to self & !other when other has unknown bits set.
difference won’t truncate other, but the ! operator will.
Auto Trait Implementations§
impl Freeze for SemanticModelFlags
impl RefUnwindSafe for SemanticModelFlags
impl Send for SemanticModelFlags
impl Sync for SemanticModelFlags
impl Unpin for SemanticModelFlags
impl UnsafeUnpin for SemanticModelFlags
impl UnwindSafe for SemanticModelFlags
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more