Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce isort and Python-based code quality tools #446

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

1cu
Copy link
Contributor

@1cu 1cu commented Mar 3, 2025

ℹ️ Description

This PR introduces isort for standardized import formatting and a new set of Python scripts to improve code quality tooling. It establishes consistent import ordering rules and replaces direct shell commands with more maintainable Python scripts for formatting and linting tasks.

  • Link to the related issue(s): N/A
  • Motivation: Standardize import formatting across the project and improve maintainability of code quality tools while providing better error reporting and selective file processing capabilities.

📋 Changes Summary

  • Introduce isort configuration in pyproject.toml:
    • Configure black profile for consistent formatting
    • Set project-specific import ordering rules
    • Define line length and grouping preferences
  • Add new Python scripts in the scripts/ directory:
    • format_code.py: Handles code formatting using isort and autopep8
    • lint_code.py: Runs and coordinates multiple linting tools
    • git_utils.py: Provides shared git-related functionality
  • Update PDM commands to use the new scripts
  • Add proper error handling and status reporting
  • Implement selective processing of modified files
  • Add comprehensive docstrings and type hints

⚙️ Type of Change

  • ✨ New feature (adds new functionality without breaking existing usage)

✅ Checklist

  • I have reviewed my changes to ensure they meet the project's standards.
  • I have tested my changes and ensured that all tests pass (pdm run test).
  • I have verified that linting passes (pdm run lint).
  • I have run security scans and addressed any identified issues (pdm run audit).
  • I have updated documentation where necessary.

1cu added 3 commits March 3, 2025 08:29
Add isort to the development toolchain for better import organization with a
focus on minimal impact by only formatting changed files. This approach allows
gradual adoption of consistent import formatting without bulk changes to the
entire codebase.

Technical Changes:
- Add isort>=5.13.2 to dev dependencies
- Configure isort to match project conventions:
  * Use black profile as base configuration
  * Set line_length=160 to match project standards
  * Enable combine_as_imports and combine_star for compact imports
  * Configure multi_line_output=3 for vertical hanging indent style
  * Enable combine_straight_imports for consistent grouping

New PDM Commands:
- pdm format: Now includes isort for changed files before autopep8
  * Only formats imports in modified Python files
  * Maintains existing autopep8 formatting for all files
- pdm lint: Integrates isort into the linting pipeline
  * Runs isort on changed files
  * Maintains existing pylint, autopep8, and mypy checks
  * Proper error handling and reporting

Example of resulting import style:
```python
from collections.abc import AsyncGenerator, Callable, Generator
from typing import Any, TypeVar, cast
from unittest.mock import AsyncMock, MagicMock, patch
```

Design Decisions:
1. Selective Application:
   - Only format imports in changed files to minimize disruption
   - Allows gradual adoption of new import style
   - Prevents unnecessary git history noise

2. Configuration Choices:
   - Black profile ensures consistency with Python community standards
   - Project-specific adjustments for line length and combining rules
   - Maintains existing code style while improving readability

3. Integration Strategy:
   - Added to both format and lint workflows
   - Error handling preserves existing behavior

This change improves code maintainability and readability while respecting
existing code patterns and minimizing disruption to the development workflow.
Introduce new Python scripts to standardize and automate code quality tasks:
- Add format_code.py for automated code formatting using isort and autopep8
- Add lint_code.py for running multiple linting tools (pylint, mypy, isort)
- Add git_utils.py for shared git-related functionality
- Update pyproject.toml to use new scripts instead of direct shell commands

The new scripts provide:
- Consistent code formatting across the project
- Unified error reporting for all linting tools
- Automatic import sorting with isort
- Selective formatting of modified files for faster processing
- Better error handling and status reporting

PDM commands updated:
format -> python -m scripts.format_code
lint -> python -m scripts.lint_code
@github-actions github-actions bot added the enhancement New feature or request label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

1 participant