aspy.refactor_imports

raw JSON →
3.0.2 verified Mon Apr 27 auth: no python

Utilities for refactoring imports in Python-like syntax. Provides functions to categorize, modify, and reformat import statements. Current version 3.0.2, requires Python >=3.7. Maintained by asottile with regular updates.

pip install aspy.refactor_imports
error ModuleNotFoundError: No module named 'aspy_refactor_imports'
cause Incorrect package name with underscores instead of dots.
fix
Install as pip install aspy.refactor_imports and import as from aspy.refactor_imports import ...
error ImportError: cannot import name 'import_module' from 'aspy.refactor_imports'
cause Function may have been renamed or removed.
fix
Check documentation for current API; use categorize_imports instead.
error TypeError: categorize_imports() missing 1 required positional argument: 'classify'
cause Missing classifier function argument.
fix
Provide a classifier function: categorize_imports(lines, my_classifier) where my_classifier takes a string and returns an ImportType.
breaking Version 3.0.0 removed deprecated functions: `import_type`, `import_descriptions`, etc. Use `categorize_imports` and `classify_import` instead.
fix Replace calls to removed functions with new API.
gotcha The function `categorize_imports` expects a list of import strings and a classifier function returning `ImportType`. Misunderstanding the classifier signature leads to errors.
fix Ensure the classifier takes a single import line string and returns an `ImportType` enum member.
deprecated The `classify_import` function is deprecated in favor of using `categorize_imports` with a custom classifier.
fix Use `categorize_imports` directly.

Demonstrates categorizing import lines into types using aspy.refactor_imports.

from aspy.refactor_imports import categorize_imports, ImportType, classify_import

# Example: categorize imports in a source string
source = '''import os
import sys
from collections import OrderedDict
'''
import_lines = source.splitlines()
categorized = categorize_imports(import_lines, lambda x: ImportType.THIRD_PARTY)
print(categorized)