pub struct ImportHelper { /* private fields */ }Expand description
Main helper for managing Python imports across the codebase
Implementations§
Source§impl ImportHelper
impl ImportHelper
Sourcepub fn with_package_name(package_name: String) -> Self
pub fn with_package_name(package_name: String) -> Self
Create a new import helper instance with package name for local import detection
Sourcepub fn with_formatting_config(config: FormattingConfig) -> Self
pub fn with_formatting_config(config: FormattingConfig) -> Self
Create a new import helper with custom formatting configuration
Sourcepub fn with_package_and_config(
package_name: String,
config: FormattingConfig,
) -> Self
pub fn with_package_and_config( package_name: String, config: FormattingConfig, ) -> Self
Create a new import helper with package name and custom formatting
Sourcepub fn formatting_config(&self) -> &FormattingConfig
pub fn formatting_config(&self) -> &FormattingConfig
Get the current formatting configuration
Sourcepub fn set_formatting_config(&mut self, config: FormattingConfig)
pub fn set_formatting_config(&mut self, config: FormattingConfig)
Set a new formatting configuration
Sourcepub fn registry(&self) -> &PackageRegistry
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"));Sourcepub fn registry_mut(&mut self) -> &mut PackageRegistry
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")));Sourcepub fn clear_cache(&mut self) -> &mut Self
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-categorizationSourcepub fn add_local_package_prefix(
&mut self,
prefix: impl Into<String>,
) -> &mut Self
pub fn add_local_package_prefix( &mut self, prefix: impl Into<String>, ) -> &mut Self
Add a custom local package prefix to the recognition list
Sourcepub fn add_local_package_prefixes(
&mut self,
prefixes: &[impl AsRef<str>],
) -> &mut Self
pub fn add_local_package_prefixes( &mut self, prefixes: &[impl AsRef<str>], ) -> &mut Self
Add multiple local package prefixes at once
Sourcepub fn add_import(&mut self, spec: &ImportSpec)
pub fn add_import(&mut self, spec: &ImportSpec)
Add an import using structured ImportSpec
Sourcepub fn add_import_string(&mut self, import_statement: &str)
pub fn add_import_string(&mut self, import_statement: &str)
Convenience method to add import from string (for backward compatibility)
Sourcepub fn add_from_import(&mut self, package: &str, items: &[&str])
pub fn add_from_import(&mut self, package: &str, items: &[&str])
Add a from import statement programmatically
Example: add_from_import("typing", &["Any", "Optional"])
Sourcepub fn add_from_import_multiline(&mut self, package: &str, items: &[&str])
pub fn add_from_import_multiline(&mut self, package: &str, items: &[&str])
Add a multiline from import statement programmatically
Sourcepub fn add_type_checking_from_import(&mut self, package: &str, items: &[&str])
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"])
Sourcepub fn add_direct_import(&mut self, module: &str)
pub fn add_direct_import(&mut self, module: &str)
Add a direct import statement programmatically
Example: add_direct_import("json“)
Sourcepub fn add_type_checking_direct_import(&mut self, module: &str)
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“)
Sourcepub fn add_type_checking_import(&mut self, import_statement: &str)
pub fn add_type_checking_import(&mut self, import_statement: &str)
Add an import statement to the TYPE_CHECKING block
Sourcepub fn get_all_categorized(&self) -> AllCategorizedImports
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)
Sourcepub fn get_type_checking_categorized(&self) -> CategorizedImports
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.
pub fn get_type_checking_categorized_impl(&self) -> CategorizedImports
Sourcepub fn get_categorized(&self) -> CategorizedImports
pub fn get_categorized(&self) -> CategorizedImports
Get the collected imports as categorized tuples
Returns (future_imports, stdlib_imports, third_party_imports, local_imports)
Sourcepub fn clear(&mut self) -> &mut Self
pub fn clear(&mut self) -> &mut Self
Clear all registered imports while preserving configuration
This method clears both regular and TYPE_CHECKING imports, making the helper ready to collect new imports. Configuration like formatting settings and local package prefixes are preserved.
This is useful when reusing the same helper for multiple files.
§Examples
use py_import_helper::ImportHelper;
let mut helper = ImportHelper::with_package_name("mypackage".to_string());
helper.add_import_string("from typing import Any");
assert!(!helper.is_empty());
helper.clear();
assert!(helper.is_empty());
assert_eq!(helper.count(), 0);Sourcepub fn reset(&mut self) -> &mut Self
pub fn reset(&mut self) -> &mut Self
Reset the import helper to a native state without any configuration
This method resets the helper to the same state as ImportHelper::new(),
clearing all imports, local package prefixes, and resetting formatting
configuration to defaults. The package name is also cleared.
This is useful when you need a completely fresh helper instance without creating a new one.
§Examples
use py_import_helper::ImportHelper;
let mut helper = ImportHelper::with_package_name("mypackage".to_string());
helper.add_local_package_prefix("other_package");
helper.add_import_string("from typing import Any");
// Reset to native state
helper.reset();
assert!(helper.is_empty());
assert_eq!(helper.count(), 0);
// Local package prefixes and package name are clearedSourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if any imports have been collected (excluding TYPE_CHECKING imports)
Sourcepub fn is_type_checking_empty(&self) -> bool
pub fn is_type_checking_empty(&self) -> bool
Check if any TYPE_CHECKING imports have been collected
Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Count total number of import statements collected (excluding TYPE_CHECKING imports)
Sourcepub fn count_type_checking(&self) -> usize
pub fn count_type_checking(&self) -> usize
Count total number of TYPE_CHECKING import statements collected
Sourcepub fn get_formatted(&self) -> Vec<String>
pub fn get_formatted(&self) -> Vec<String>
Generate sorted and formatted import statements
Sourcepub fn clone_config(&self) -> Self
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
impl ImportHelper
Convenience functions for common import operations
Sourcepub fn create_model_imports(&mut self, required_types: &[String])
pub fn create_model_imports(&mut self, required_types: &[String])
Create imports for a model file with required type imports