Registry / testing / classify-imports

classify-imports

JSON →
library4.2.0pypypi✓ verified 87d ago

classify-imports is a Python utility library designed for refactoring imports in Python-like syntax. It provides tools to parse import statements, classify them into categories (e.g., built-in, third-party, application), and sort them according to defined rules. The library is currently at version 4.2.0 and is actively maintained with a focus on import organization.

pip install classify-imports
INSTALL
IMPORT
SIG · CLASSIFY-IMPORTS
C
classify-imports
testingpythonv4.2.0
Install
1.5s avg
Import
16ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.017s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.016s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

import_obj_from_str
from classify_imports import import_obj_from_str
Used to parse a string representation of an import statement into an import object.
sort
from classify_imports import sort
Used to sort a list of import objects, typically partitioning them into blocks.
classify_base
from classify_imports import classify_base
Provides a function to determine the base classification (e.g., 'BUILTIN', 'THIRD_PARTY', 'APPLICATION') for a module name.
Classified
from classify_imports import Classified
An Enum-like object containing predefined classification types and their default order.

This quickstart demonstrates the core functionalities of `classify-imports`: parsing a single import string into multiple, sorting a list of import objects into classified blocks, and determining the classification type of a module name. The `sort` function will arrange imports into 'FUTURE', 'BUILTIN', 'THIRD_PARTY', and 'APPLICATION' categories by default.

from classify_imports import import_obj_from_str, sort, classify_base, Classified import pprint # Example 1: Splitting an import object obj = import_obj_from_str('import foo, bar, baz') split_imports = [str(i) for i in obj.split()] print('Split Imports:') print(split_imports) # Example 2: Sorting and partitioning import objects imports_to_sort = [ import_obj_from_str('from classify_imports import sort'), import_obj_from_str('import sys'), import_obj_from_str('from pyramid.view import view_config'), import_obj_from_str('import cached_property'), ] partitioned = sort(imports_to_sort) print('\nSorted and Partitioned Imports:') pprint.pprint(partitioned) # Example 3: Classifying a module print('\nModule Classification:') print(f"'__future__' is: {classify_base('__future__')}") print(f"'classify_imports' is: {classify_base('classify_imports')}") print(f"'pyramid' is: {classify_base('pyramid')}") print(f"'os' is: {classify_base('os')}") print(f"Order of classifications: {Classified.order}")
Debug
Known issues
breakingThe library was rewritten and renamed from `aspy.refactor-imports` to `classify-imports` starting with version 4. Users upgrading from version 3.x or older of `aspy.refactor-imports` must update their import paths and potentially their usage patterns.
fix
Update `pip install aspy.refactor-imports` to `pip install classify-imports` and change all import statements from `from aspy.refactor_imports import ...` to `from classify_imports import ...`.
affects: <4.0.0
gotchaThe `classify_base` function and `sort` utility classify modules based on heuristics (e.g., stdlib list, known third-party patterns, or application imports relative to the project). Incorrectly configured application import names or paths can lead to modules being misclassified as third-party instead of application-specific, or vice-versa.
fix
Review the documentation for configuring application import paths and names if using in a larger project or with tools like `reorder-python-imports` that rely on its classification. Explicitly configure known application prefixes or modules if default heuristics are insufficient.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'aspy.refactor_imports'
Attempting to import the library using its old package name after upgrading to version 4.x or installing the new package name.
fix
The package was renamed. Use `from classify_imports import ...` instead. If you haven't installed `classify-imports`, install it with `pip install classify-imports`.
Imports are not sorted as expected, e.g., application imports appearing before built-ins or third-party.
The default sorting order of `classify-imports` might not align with user expectations or project-specific style guides, or a module is misclassified.
fix
Understand the default `Classified.order` ('FUTURE', 'BUILTIN', 'THIRD_PARTY', 'APPLICATION'). If a module is misclassified, ensure your environment or calling tool (like `reorder-python-imports`) correctly identifies application vs. third-party modules. Customization options might be available in tools integrating `classify-imports` to override the default order or classification logic.
Upgrade
Version history
4.2.0latest on PyPI · released Jul 31, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Amazon
1
OpenAI (training)
1
Resources
classify-imports — pip install classify-imports · libregistry