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: StringThe 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 moduleSome(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
impl TypeInfo
Sourcepub fn list_of<T: PyStubType>() -> Self
pub fn list_of<T: PyStubType>() -> Self
A list[Type] type annotation.
Sourcepub fn set_of<T: PyStubType>() -> Self
pub fn set_of<T: PyStubType>() -> Self
A set[Type] type annotation.
Sourcepub fn dict_of<K: PyStubType, V: PyStubType>() -> Self
pub fn dict_of<K: PyStubType, V: PyStubType>() -> Self
A dict[Type] type annotation.
Sourcepub fn builtin(name: &str) -> Self
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].
Sourcepub fn unqualified(name: &str) -> Self
pub fn unqualified(name: &str) -> Self
Unqualified type.
Sourcepub fn with_module(name: &str, module: ModuleRef) -> Self
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());Sourcepub fn locally_defined(type_name: &str, module: ModuleRef) -> Self
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
Ais defined inpackage.submod1, it will be referenced assubmod1.Awhen 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_modulefield tracks which module the type belongs to for future use.
pyo3_stub_gen::TypeInfo::locally_defined("A", "package.submod1".into());Sourcepub fn qualified_name(&self, target_module: &str) -> String
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”
Sourcepub fn is_same_module(&self, target_module: &str) -> bool
pub fn is_same_module(&self, target_module: &str) -> bool
Check if this type is from the same module as the target module.
Sourcepub fn is_internal_to_package(&self, package_root: &str) -> bool
pub fn is_internal_to_package(&self, package_root: &str) -> bool
Check if this type is internal to the package (starts with package root).
Sourcepub fn qualified_for_module(&self, target_module: &str) -> String
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]becomestyping.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
Sourcepub fn resolve_default_module(&mut self, default_module_name: &str)
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§
impl Eq for TypeInfo
impl StructuralPartialEq for TypeInfo
Auto Trait Implementations§
impl Freeze for TypeInfo
impl RefUnwindSafe for TypeInfo
impl Send for TypeInfo
impl Sync for TypeInfo
impl Unpin for TypeInfo
impl UnwindSafe for TypeInfo
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,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§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