Crate py_import_helper

Crate py_import_helper 

Source
Expand description

Python import organization library

A Rust library for automatically organizing Python imports according to PEP 8. Perfect for code generation projects, it categorizes imports into standard library, third-party, and local packages, then formats them with proper spacing and ordering.

§Usage

use py_import_helper::ImportHelper;

// Create a helper with package name for local import detection
let mut helper = ImportHelper::with_package_name("mypackage".to_string());

// Add custom local package prefixes
helper.add_local_package_prefix("mypackage");

// Collect imports using strings
helper.add_import_string("from typing import Any");
helper.add_import_string("from pydantic import BaseModel");
helper.add_import_string("from mypackage.models import User");

// Or use the builder pattern
helper.add_from_import("typing", &["Optional", "List"]);
helper.add_direct_import("json");

// Get categorized imports
let (future, stdlib, third_party, local) = helper.get_categorized();

Re-exports§

pub use registry::PackageRegistry;
pub use types::FormattingConfig;
pub use types::ImportCategory;
pub use types::ImportSections;
pub use types::ImportStatement;
pub use types::ImportType;
pub use registry::constants::COMMON_THIRD_PARTY_PACKAGES;
pub use registry::constants::PYTHON_STDLIB_MODULES;

Modules§

registry
Package registry for categorizing Python imports
types
Type definitions for Python import management
utils
Utility modules for import processing

Structs§

ImportHelper
Main helper for managing Python imports across the codebase