Skip to main content

SemanticModelFlags

Struct SemanticModelFlags 

Source
pub struct SemanticModelFlags(/* private fields */);
Expand description

Flags indicating the current model state.

Implementations§

Source§

impl SemanticModelFlags

Source

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 = 1

In 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.

Source

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.

Source

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: int

In 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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 = 1
Source

pub const F_STRING: Self

The model is in an f-string.

For example, the model could be visiting x in:

f'{x}'
Source

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.

Source

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"]):
    ...
Source

pub const SUBSCRIPT: Self

The model is in a subscript expression.

For example, the model could be visiting x["a"] in:

x["a"]["b"]
Source

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 = 1
Source

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 = 1
Source

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:
  ...
Source

pub const STUB_FILE: Self

The model is in a Python stub file (i.e., a .pyi file).

Source

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.

Source

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 = 1
Source

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]: pass

Note 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.

Source

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): ...
Source

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."""
    pass
Source

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,")
Source

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}"
Source

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):
    pass
Source

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.)

Source

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:

  1. At the top level of a module
  2. 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.

Source

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 RHS
Source

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 RHS
Source

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, y
Source

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":
    pass
Source

pub const T_STRING: Self

The model is in a t-string.

For example, the model could be visiting x in:

t'{x}'
Source

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)
Source

pub const ANNOTATION: Self

The context is in any type annotation.

Source

pub const STRING_TYPE_DEFINITION: Self

The context is in any string type definition.

Source

pub const DEFERRED_TYPE_DEFINITION: Self

The context is in any deferred type definition.

Source

pub const TYPING_CONTEXT: Self

The context is in a typing-only context.

Source

pub const TYPE_ALIAS: Self

The context is in any type alias.

Source§

impl SemanticModelFlags

Source

pub const fn empty() -> Self

Get a flags value with all bits unset.

Source

pub const fn all() -> Self

Get a flags value with all known bits set.

Source

pub const fn bits(&self) -> u32

Get the underlying bits value.

The returned value is exactly the bits set in this flags value.

Source

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.

Source

pub const fn from_bits_truncate(bits: u32) -> Self

Convert from a bits value, unsetting any unknown bits.

Source

pub const fn from_bits_retain(bits: u32) -> Self

Convert from a bits value exactly.

Source

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.

Source

pub const fn is_empty(&self) -> bool

Whether all bits in self are unset.

Source

pub const fn is_all(&self) -> bool

Whether all known bits in this flags value are set.

Source

pub const fn intersects(&self, other: Self) -> bool

Whether any set bits in other are also set in self.

Source

pub const fn contains(&self, other: Self) -> bool

Whether all set bits in other are also set in self.

Source

pub fn insert(&mut self, other: Self)

The bitwise or (|) of the bits in self and other.

Source

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.

Source

pub fn toggle(&mut self, other: Self)

The bitwise exclusive-or (^) of the bits in self and other.

Source

pub fn set(&mut self, other: Self, value: bool)

Call insert when value is true or remove when value is false.

Source

pub const fn intersection(self, other: Self) -> Self

The bitwise and (&) of the bits in self and other.

Source

pub const fn union(self, other: Self) -> Self

The bitwise or (|) of the bits in self and other.

Source

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.

Source

pub const fn symmetric_difference(self, other: Self) -> Self

The bitwise exclusive-or (^) of the bits in self and other.

Source

pub const fn complement(self) -> Self

The bitwise negation (!) of the bits in self, truncating the result.

Source§

impl SemanticModelFlags

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl BitAnd for SemanticModelFlags

Source§

fn bitand(self, other: Self) -> Self

The bitwise and (&) of the bits in self and other.

Source§

type Output = SemanticModelFlags

The resulting type after applying the & operator.
Source§

impl BitAndAssign for SemanticModelFlags

Source§

fn bitand_assign(&mut self, other: Self)

The bitwise and (&) of the bits in self and other.

Source§

impl BitOr for SemanticModelFlags

Source§

fn bitor(self, other: SemanticModelFlags) -> Self

The bitwise or (|) of the bits in self and other.

Source§

type Output = SemanticModelFlags

The resulting type after applying the | operator.
Source§

impl BitOrAssign for SemanticModelFlags

Source§

fn bitor_assign(&mut self, other: Self)

The bitwise or (|) of the bits in self and other.

Source§

impl BitXor for SemanticModelFlags

Source§

fn bitxor(self, other: Self) -> Self

The bitwise exclusive-or (^) of the bits in self and other.

Source§

type Output = SemanticModelFlags

The resulting type after applying the ^ operator.
Source§

impl BitXorAssign for SemanticModelFlags

Source§

fn bitxor_assign(&mut self, other: Self)

The bitwise exclusive-or (^) of the bits in self and other.

Source§

impl Clone for SemanticModelFlags

Source§

fn clone(&self) -> SemanticModelFlags

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SemanticModelFlags

Source§

impl Debug for SemanticModelFlags

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SemanticModelFlags

Source§

fn default() -> SemanticModelFlags

Returns the “default value” for a type. Read more
Source§

impl Eq for SemanticModelFlags

Source§

impl Extend<SemanticModelFlags> for SemanticModelFlags

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl Flags for SemanticModelFlags

Source§

const FLAGS: &'static [Flag<SemanticModelFlags>]

The set of defined flags.
Source§

type Bits = u32

The underlying bits type.
Source§

fn bits(&self) -> u32

Get the underlying bits value. Read more
Source§

fn from_bits_retain(bits: u32) -> SemanticModelFlags

Convert from a bits value exactly.
Source§

fn all_named() -> SemanticModelFlags

Get a flags value with all bits from named flags set. Read more
Source§

fn empty() -> Self

Get a flags value with all bits unset.
Source§

fn all() -> Self

Get a flags value with all known bits set.
Source§

fn known_bits(&self) -> Self::Bits

Get the known bits from a flags value.
Source§

fn unknown_bits(&self) -> Self::Bits

Get the unknown bits from a flags value.
Source§

fn contains_unknown_bits(&self) -> bool

This method will return true if any unknown bits are set.
Source§

fn from_bits(bits: Self::Bits) -> Option<Self>

Convert from a bits value. Read more
Source§

fn from_bits_truncate(bits: Self::Bits) -> Self

Convert from a bits value, unsetting any unknown bits.
Source§

fn from_name(name: &str) -> Option<Self>

Get a flags value with the bits of a flag with the given name set. Read more
Source§

fn iter(&self) -> Iter<Self>

Yield a set of contained flags values. Read more
Source§

fn iter_names(&self) -> IterNames<Self>

Yield a set of contained named flags values. Read more
Source§

fn iter_defined_names() -> IterDefinedNames<Self>

Yield a set of all named flags defined by Self::FLAGS.
Source§

fn iter_equal_names(&self) -> IterEqualNames<Self>

Get an iterator over all defined names for this flags value. Read more
Source§

fn is_empty(&self) -> bool

Whether all bits in this flags value are unset.
Source§

fn is_all(&self) -> bool

Whether all known bits in this flags value are set.
Source§

fn intersects(&self, other: Self) -> bool
where Self: Sized,

Whether any set bits in other are also set in self.
Source§

fn contains(&self, other: Self) -> bool
where Self: Sized,

Whether all set bits in other are also set in self.
Source§

fn truncate(&mut self)
where Self: Sized,

Remove any unknown bits from the flags.
Source§

fn insert(&mut self, other: Self)
where Self: Sized,

The bitwise or (|) of the bits in self and other.
Source§

fn remove(&mut self, other: Self)
where Self: Sized,

The intersection of self with the complement of other (&!). Read more
Source§

fn toggle(&mut self, other: Self)
where Self: Sized,

The bitwise exclusive-or (^) of the bits in self and other.
Source§

fn set(&mut self, other: Self, value: bool)
where Self: Sized,

Call Flags::insert when value is true or Flags::remove when value is false.
Source§

fn clear(&mut self)
where Self: Sized,

Unsets all bits in the flags.
Source§

fn intersection(self, other: Self) -> Self

The bitwise and (&) of the bits in self and other.
Source§

fn union(self, other: Self) -> Self

The bitwise or (|) of the bits in self and other.
Source§

fn difference(self, other: Self) -> Self

The intersection of self with the complement of other (&!). Read more
Source§

fn symmetric_difference(self, other: Self) -> Self

The bitwise exclusive-or (^) of the bits in self and other.
Source§

fn complement(self) -> Self

The bitwise negation (!) of the bits in self, truncating the result.
Source§

impl FromIterator<SemanticModelFlags> for SemanticModelFlags

Source§

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

Source§

type Item = SemanticModelFlags

The type of the elements being iterated over.
Source§

type IntoIter = Iter<SemanticModelFlags>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl LowerHex for SemanticModelFlags

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Not for SemanticModelFlags

Source§

fn not(self) -> Self

The bitwise negation (!) of the bits in self, truncating the result.

Source§

type Output = SemanticModelFlags

The resulting type after applying the ! operator.
Source§

impl Octal for SemanticModelFlags

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SemanticModelFlags

Source§

fn eq(&self, other: &SemanticModelFlags) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PublicFlags for SemanticModelFlags

Source§

type Primitive = u32

The type of the underlying storage.
Source§

type Internal = InternalBitFlags

The type of the internal field on the generated flags type.
Source§

impl StructuralPartialEq for SemanticModelFlags

Source§

impl Sub for SemanticModelFlags

Source§

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

The resulting type after applying the - operator.
Source§

impl SubAssign for SemanticModelFlags

Source§

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.

Source§

impl UpperHex for SemanticModelFlags

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.