ImportHelper

Struct ImportHelper 

Source
pub struct ImportHelper { /* private fields */ }
Expand description

Main helper for managing Python imports across the codebase

Implementations§

Source§

impl ImportHelper

Source

pub fn new() -> Self

Create a new import helper instance

Source

pub fn with_package_name(package_name: String) -> Self

Create a new import helper instance with package name for local import detection

Source

pub fn registry(&self) -> &PackageRegistry

Get immutable reference to the package registry

§Examples
use py_import_helper::ImportHelper;

let helper = ImportHelper::new();
assert!(helper.registry().is_stdlib("typing"));
Source

pub fn registry_mut(&mut self) -> &mut PackageRegistry

Get mutable reference to the package registry

Use this to customize which packages are recognized as stdlib or third-party before generating imports.

§Examples
use py_import_helper::ImportHelper;

let mut helper = ImportHelper::new();
helper.registry_mut()
    .add_stdlib_package("my_custom_stdlib")
    .add_third_party_package("my_company_lib");

helper.add_import_string("import my_custom_stdlib");
let (_future, stdlib, _third, _local) = helper.get_categorized();
assert!(stdlib.iter().any(|s| s.contains("my_custom_stdlib")));
Source

pub fn clear_cache(&mut self) -> &mut Self

Clear the categorization cache

Call this after modifying the registry to ensure changes take effect.

§Examples
use py_import_helper::ImportHelper;

let mut helper = ImportHelper::new();
helper.add_import_string("import mypackage");
helper.registry_mut().add_stdlib_package("mypackage");
helper.clear_cache();  // Force re-categorization
Source

pub fn add_local_package_prefix( &mut self, prefix: impl Into<String>, ) -> &mut Self

Add a custom local package prefix to the recognition list

Source

pub fn add_local_package_prefixes( &mut self, prefixes: &[impl AsRef<str>], ) -> &mut Self

Add multiple local package prefixes at once

Source

pub fn add_import(&mut self, spec: &ImportSpec)

Add an import using structured ImportSpec

Source

pub fn add_import_string(&mut self, import_statement: &str)

Convenience method to add import from string (for backward compatibility)

Source

pub fn add_from_import(&mut self, package: &str, items: &[&str])

Add a from import statement programmatically Example: add_from_import("typing", &["Any", "Optional"])

Source

pub fn add_type_checking_from_import(&mut self, package: &str, items: &[&str])

Add a from import statement to TYPE_CHECKING block programmatically Example: add_type_checking_from_import("httpx", &["Client", "Response"])

Source

pub fn add_direct_import(&mut self, module: &str)

Add a direct import statement programmatically Example: add_direct_import("json“)

Source

pub fn add_type_checking_direct_import(&mut self, module: &str)

Add a direct import statement to TYPE_CHECKING block programmatically Example: add_type_checking_direct_import("httpx“)

Source

pub fn add_type_checking_import(&mut self, import_statement: &str)

Add an import statement to the TYPE_CHECKING block

Source

pub fn get_all_categorized(&self) -> AllCategorizedImports

Generate all imports (regular + TYPE_CHECKING) for templates Returns a tuple with 8 vectors: (future, stdlib, third_party, local, tc_future, tc_stdlib, tc_third_party, tc_local)

Source

pub fn get_type_checking_categorized(&self) -> CategorizedImports

Generate categorized TYPE_CHECKING imports for templates Returns (future_imports, stdlib_imports, third_party_imports, local_imports) Get TYPE_CHECKING imports categorized by type

Returns a tuple of (future_imports, stdlib_imports, third_party_imports, local_imports) for imports that should go in the TYPE_CHECKING block.

Source

pub fn get_type_checking_categorized_impl(&self) -> CategorizedImports

Source

pub fn get_categorized(&self) -> CategorizedImports

Get the collected imports as categorized tuples Returns (future_imports, stdlib_imports, third_party_imports, local_imports)

Source

pub fn reset(&mut self) -> &mut Self

Reset the import sections while preserving configuration Useful when reusing the same helper for multiple files

Source

pub fn is_empty(&self) -> bool

Check if any imports have been collected (excluding TYPE_CHECKING imports)

Source

pub fn is_type_checking_empty(&self) -> bool

Check if any TYPE_CHECKING imports have been collected

Source

pub fn count(&self) -> usize

Count total number of import statements collected (excluding TYPE_CHECKING imports)

Source

pub fn count_type_checking(&self) -> usize

Count total number of TYPE_CHECKING import statements collected

Source

pub fn get_formatted(&self) -> Vec<String>

Generate sorted and formatted import statements

Source

pub fn clone_config(&self) -> Self

Clone configuration without imports (useful for creating multiple helpers with same config)

Source§

impl ImportHelper

Convenience functions for common import operations

Source

pub fn create_model_imports(&mut self, required_types: &[String])

Create imports for a model file with required type imports

Trait Implementations§

Source§

impl Debug for ImportHelper

Source§

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

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

impl Default for ImportHelper

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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.