[PR #2366] enhancement: introduce Ruff for Python linter for reordering and removing unused imports with automated pre-commit and sytle check #23596

Closed
opened 2026-02-21 20:21:23 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/2366

State: closed
Merged: Yes


  • The Ruff tool is a general linting and formatting tools for Python code
  • Ruff is blazingly fast, as it finished lining or fixing all the hundreds of Python scripts of Dify project in less than a second, with the support of local cache and the core written in Rust
  • Ruff linter is designed as drop-in replacement for Flake8 (plus dozens of plugins), isort, pydocstyle, pyupgrade, autoflake, and more
  • To sort Python imports, the isort rules (I001, I002) in Ruff are applied (https://docs.astral.sh/ruff/rules/#isort-i)
    • although the line breaks in a style closer to black
  • Re-arranged Python imports in sections separated in source parties for better readability, in the order of "future", "standard-library", "third-party", "first-party", "local-folder". This greatly improves the readability and understandability.
  • To remove the unused imports, F401 rule of Flake8 in Ruff is applied (https://docs.astral.sh/ruff/rules/#pyflakes-f)
    • fixed 190 violations
    • the tests directory is skipped as the tests requires some unused-like imports for importing mocking methods

The key changes in this PR:

  1. apply ruff auto-fixes in one-key linting script dev/reformat
  2. automatically run linter scripts in pre-commit (via husky support) when committing changes, if API module changes
  3. check Python style in CI style workflow by running ruff linter and list all the violations if it fails
**Original Pull Request:** https://github.com/langgenius/dify/pull/2366 **State:** closed **Merged:** Yes --- - The Ruff tool is a general linting and formatting tools for Python code - Ruff is blazingly fast, as it finished lining or fixing all the hundreds of Python scripts of Dify project in less than a second, with the support of local cache and the core written in Rust - Ruff linter is designed as drop-in replacement for [Flake8](https://pypi.org/project/flake8/) (plus dozens of plugins), [isort](https://pypi.org/project/isort/), [pydocstyle](https://pypi.org/project/pydocstyle/), [pyupgrade](https://pypi.org/project/pyupgrade/), [autoflake](https://pypi.org/project/autoflake/), and more - To sort Python imports, the isort rules (I001, I002) in Ruff are applied (https://docs.astral.sh/ruff/rules/#isort-i) - although the line breaks in a style closer to `black` - Re-arranged Python imports in sections separated in source parties for better readability, in the order of "future", "standard-library", "third-party", "first-party", "local-folder". This greatly improves the readability and understandability. - To remove the unused imports, F401 rule of Flake8 in Ruff is applied (https://docs.astral.sh/ruff/rules/#pyflakes-f) - fixed 190 violations - the tests directory is skipped as the tests requires some unused-like imports for importing mocking methods The key changes in this PR: 1. apply ruff auto-fixes in one-key linting script `dev/reformat` 2. automatically run linter scripts in `pre-commit` (via husky support) when committing changes, if API module changes 3. check Python style in CI style workflow by running ruff linter and list all the violations if it fails
yindo added the pull-request label 2026-02-21 20:21:23 -05:00
yindo closed this issue 2026-02-21 20:21:23 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#23596