TypeInfo

Struct TypeInfo 

Source
pub struct TypeInfo {
    pub name: String,
    pub source_module: Option<ModuleRef>,
    pub import: HashSet<ImportRef>,
    pub type_refs: HashMap<String, TypeIdentifierRef>,
}
Expand description

Type information for creating Python stub files annotated by PyStubType trait.

Fields§

§name: String

The Python type name.

§source_module: Option<ModuleRef>

The module this type belongs to.

  • None: Type has no source module (e.g., typing.Any, primitives, generic container types)
  • Some(ModuleRef::Default): Type from current package’s default module
  • Some(ModuleRef::Named(path)): Type from specific module (e.g., "package.sub_mod")
§import: HashSet<ImportRef>

Python modules must be imported in the stub file.

For example, when name is typing.Sequence[int], import should contain typing. This makes it possible to use user-defined types in the stub file.

§type_refs: HashMap<String, TypeIdentifierRef>

Track all type identifiers referenced in the name expression.

This enables context-aware qualification of identifiers within compound type expressions. For example, in typing.Optional[ClassA], we need to track that ClassA is from a specific module and qualify it appropriately based on the target module context.

  • Key: bare identifier (e.g., “ClassA”)
  • Value: TypeIdentifierRef containing module and import kind

Implementations§

Source§

impl TypeInfo

Source

pub fn none() -> Self

A None type annotation.

Source

pub fn any() -> Self

A typing.Any type annotation.

Source

pub fn list_of<T: PyStubType>() -> Self

A list[Type] type annotation.

Source

pub fn set_of<T: PyStubType>() -> Self

A set[Type] type annotation.

Source

pub fn dict_of<K: PyStubType, V: PyStubType>() -> Self

A dict[Type] type annotation.

Source

pub fn builtin(name: &str) -> Self

A type annotation of a built-in type provided from builtins module, such as int, str, or float. Generic builtin types are also possible, such as dict[str, str].

Source

pub fn unqualified(name: &str) -> Self

Unqualified type.

Source

pub fn with_module(name: &str, module: ModuleRef) -> Self

A type annotation of a type that must be imported. The type name must be qualified with the module name:

pyo3_stub_gen::TypeInfo::with_module("pathlib.Path", "pathlib".into());
Source

pub fn locally_defined(type_name: &str, module: ModuleRef) -> Self

A type defined in the PyO3 module.

  • Types are referenced using fully qualified names to avoid symbol collision when used across modules.
  • For example, if A is defined in package.submod1, it will be referenced as submod1.A when used in other modules.
  • The module will be imported as from package import submod1.
  • When used in the same module where it’s defined, it will be automatically de-qualified during stub generation.
  • The source_module field tracks which module the type belongs to for future use.
pyo3_stub_gen::TypeInfo::locally_defined("A", "package.submod1".into());
Source

pub fn qualified_name(&self, target_module: &str) -> String

Get the qualified name for use in a specific target module.

  • If the type has no source module, returns the name as-is
  • If the type is from the same module as the target, returns unqualified name
  • If the type is from a different module, returns qualified name with module component
§Examples
  • Type A from “package.sub_mod” used in “package.sub_mod” -> “A”
  • Type A from “package.sub_mod” used in “package.main_mod” -> “sub_mod.A”
Source

pub fn is_same_module(&self, target_module: &str) -> bool

Check if this type is from the same module as the target module.

Source

pub fn is_internal_to_package(&self, package_root: &str) -> bool

Check if this type is internal to the package (starts with package root).

Source

pub fn qualified_for_module(&self, target_module: &str) -> String

Get the qualified name for use in a specific target module with context-aware rewriting.

This method handles compound type expressions by rewriting nested identifiers based on the type_refs tracking information. For example:

  • typing.Optional[ClassA] becomes typing.Optional[sub_mod.ClassA] when ClassA is from a different module.
§Arguments
  • target_module - The module where this type will be used
§Returns

The qualified type name string with identifiers properly qualified

Source

pub fn resolve_default_module(&mut self, default_module_name: &str)

Resolve ModuleRef::Default to the actual module name. Called at runtime when default module name is known.

Trait Implementations§

Source§

impl BitOr for TypeInfo

Source§

type Output = TypeInfo

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self

Performs the | operation. Read more
Source§

impl Clone for TypeInfo

Source§

fn clone(&self) -> TypeInfo

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TypeInfo

Source§

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

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

impl Display for TypeInfo

Source§

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

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

impl PartialEq for TypeInfo

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for TypeInfo

Source§

impl StructuralPartialEq for TypeInfo

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
§

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

§

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

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

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

§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
§

impl<T> Ungil for T
where T: Send,