Skip to content

chore: Use ruff formatter in place of black#1324

Merged
Czaki merged 1 commit intodevelopfrom
ruff_format
Oct 20, 2025
Merged

chore: Use ruff formatter in place of black#1324
Czaki merged 1 commit intodevelopfrom
ruff_format

Conversation

@Czaki
Copy link
Collaborator

@Czaki Czaki commented Oct 20, 2025

Summary by Sourcery

Replace Black with Ruff as the project’s code formatter and reformat the entire codebase accordingly

Enhancements:

  • Switch code formatter from Black to Ruff and configure Ruff settings in pyproject.toml
  • Reformat Python modules, notebooks, and tests to conform with Ruff style rules (operator spacing, blank lines, f-string formatting)

CI:

  • Remove Black pre-commit hooks and add Ruff check and format hooks in .pre-commit-config.yaml

Summary by CodeRabbit

  • Chores
    • Migrated code formatting toolchain from Black to Ruff for improved consistency and performance.
    • Updated Markdown formatting dependencies with additional plugins for enhanced documentation processing.
    • Expanded development tool exclusion rules to cover more standard build and cache directories.
    • Applied code style standardization across the codebase.

@Czaki Czaki added this to the 0.16.4 milestone Oct 20, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 20, 2025

Reviewer's Guide

This PR replaces Black with Ruff as the project’s code formatter by removing Black configurations and hooks, adding Ruff settings, and auto-formatting the codebase to comply with Ruff’s style rules.

Flow diagram for configuration changes in pyproject.toml

flowchart TD
    A["pyproject.toml"] -- Remove Black config --> B["No Black settings"]
    A -- Add Ruff config --> C["Ruff settings: line-length, exclude, target-version, fix"]
Loading

Flow diagram for pre-commit configuration update

flowchart TD
    A[".pre-commit-config.yaml"] -- Remove Black repo and hook --> B["No Black hook"]
    A -- Add ruff-format hook to Ruff repo --> C["Ruff-format hook active"]
Loading

File-Level Changes

Change Details Files
Replaced Black with Ruff as code formatter
  • Removed [tool.black] section and Black pre-commit hook
  • Added [tool.ruff] config in pyproject.toml with exclude patterns and fix enabled
  • Updated .pre-commit-config.yaml to drop black and add ruff-format hook
pyproject.toml
.pre-commit-config.yaml
Applied Ruff auto-formatting to codebase
  • Standardized spacing around binary operators and within f-strings
  • Adjusted trailing commas, indentation, and blank lines in multiline dicts and lists
  • Reformatted method calls and ZIP extraction examples for consistency
tutorials/tutorial_neuron_types/Neuron_types_example.ipynb
package/PartSegCore/analysis/batch_processing/batch_backend.py
package/PartSegCore/analysis/measurement_calculation.py
package/PartSegCore/sphinx/auto_parameters.py
package/PartSegCore/class_generator.py
package/PartSeg/_roi_analysis/prepare_plan_widget.py
package/PartSeg/common_gui/algorithms_description.py
package/PartSeg/common_gui/multiple_file_widget.py
package/PartSeg/common_gui/napari_image_view.py
package/PartSeg/plugins/napari_io/save_tiff_layer.py
package/PartSegImage/image.py
package/tests/test_PartSeg/test_common_gui.py
package/tests/test_PartSegCore/test_analysis_batch.py
package/tests/test_PartSeg/test_sentry.py
package/tests/test_PartSegCore/test_algorithm_describe_base.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 20, 2025

Walkthrough

The PR switches Python code formatting from Black to ruff-format by updating pre-commit and pyproject.toml configurations, then applies the resulting formatting changes across the codebase—primarily spacing adjustments in f-strings, removal of blank lines, and quote style normalization.

Changes

Cohort / File(s) Summary
Pre-commit and project configuration
.pre-commit-config.yaml, pyproject.toml
Removes Black pre-commit mirror and hook entirely; adds ruff-format hook alongside ruff-check; expands mdformat dependencies (mdformat-gfm, mdformat-frontmatter, mdformat-footnote, mdformat-gfm-alerts, mdformat-ruff, mdformat-toc); updates pyproject.toml to remove [tool.black] section and expand ruff exclude patterns.
Python source formatting (spacing in f-strings and indentation)
package/PartSeg/_roi_analysis/prepare_plan_widget.py, package/PartSeg/common_gui/algorithms_description.py, package/PartSeg/common_gui/multiple_file_widget.py, package/PartSeg/common_gui/napari_image_view.py, package/PartSeg/plugins/napari_io/save_tiff_layer.py, package/PartSegCore/analysis/batch_processing/batch_backend.py, package/PartSegCore/sphinx/auto_parameters.py, package/PartSegImage/image.py
Adjusts whitespace around operators in f-strings and arithmetic expressions (e.g., {i+1}{i + 1}); normalizes string quote styles within f-strings; removes superfluous blank lines.
Python source formatting (single-line refactoring)
package/PartSegCore/analysis/measurement_calculation.py, package/PartSegCore/class_generator.py
Condenses multi-line method signatures and error messages into single-line formats; no logic changes.
Test files formatting
package/tests/test_PartSeg/test_common_gui.py, package/tests/test_PartSeg/test_sentry.py, package/tests/test_PartSegCore/test_algorithm_describe_base.py, package/tests/test_PartSegCore/test_analysis_batch.py
Removes stray blank lines and adjusts spacing around arithmetic in f-strings; no logic or control-flow changes.
Notebook formatting
tutorials/tutorial_neuron_types/Neuron_types_example.ipynb
Minor spacing and style adjustments; one loop condition refactored as any(...) generator expression with equivalent behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

The diff is composed almost entirely of repetitive, homogeneous formatting adjustments applied by the ruff-format tool switch. Configuration changes in .pre-commit-config.yaml and pyproject.toml are straightforward tool substitutions. No logic, control flow, or behavioral changes are introduced; review focuses on validating tool compatibility and confirming formatting consistency.

Possibly related PRs

Poem

🐰 Black bids farewell, ruff takes the stage,
Spaces around operators, the code's new page,
F-strings shine brighter, quotes align just right,
Formatting magic, everything looks so tight! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "chore: Use ruff formatter in place of black" directly and accurately describes the main objective of this changeset. The primary purpose is to replace Black (the Python code formatter) with ruff's formatter as the project's formatting tool. This is evidenced by the removal of Black configuration from .pre-commit-config.yaml and pyproject.toml, the addition of the ruff-format hook in pre-commit, and the consistent formatting changes across multiple files that reflect ruff's style. The title is clear, specific, and concise—a developer scanning the commit history would immediately understand that this PR involves a formatter migration.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ruff_format

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `package/tests/test_PartSegCore/test_analysis_batch.py:547-548` </location>
<code_context>

</code_context>

<issue_to_address>
**issue (code-quality):** Avoid loops in tests. ([`no-loop-in-tests`](https://docs.sourcery.ai/Reference/Rules-and-In-Line-Suggestions/Python/Default-Rules/no-loop-in-tests))

<details><summary>Explanation</summary>Avoid complex code, like loops, in test functions.

Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:
* loops
* conditionals

Some ways to fix this:

* Use parametrized tests to get rid of the loop.
* Move the complex logic into helpers.
* Move the complex part into pytest fixtures.

> Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.

Software Engineering at Google / [Don't Put Logic in Tests](https://abseil.io/resources/swe-book/html/ch12.html#donapostrophet_put_logic_in_tests)
</details>
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tutorials/tutorial_neuron_types/Neuron_types_example.ipynb (1)

408-410: Verify functional equivalence of the loop-to-generator refactor.

This marks a behavioral change within a formatting pass. The original loop with explicit touch = True and break is replaced with an any() generator expression. While functionally equivalent, this refactor should be flagged separately from pure formatting changes for audit clarity.

# Original logic
touch = False
for axis_cords, max_size in zip(coords, current_component_area.shape):
    if np.max(axis_cords) == max_size - 1:
        touch = True
        break

# Refactored logic
touch = any(
    np.max(axis_cords) == max_size - 1 for axis_cords, max_size in zip(coords, current_component_area.shape)
)

Both are equivalent—the generator expression short-circuits just like the explicit break—but this should ideally be called out as a code improvement separate from a formatting PR.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aa9269a and 267412d.

📒 Files selected for processing (17)
  • .pre-commit-config.yaml (1 hunks)
  • package/PartSeg/_roi_analysis/prepare_plan_widget.py (1 hunks)
  • package/PartSeg/common_gui/algorithms_description.py (1 hunks)
  • package/PartSeg/common_gui/multiple_file_widget.py (1 hunks)
  • package/PartSeg/common_gui/napari_image_view.py (1 hunks)
  • package/PartSeg/plugins/napari_io/save_tiff_layer.py (1 hunks)
  • package/PartSegCore/analysis/batch_processing/batch_backend.py (1 hunks)
  • package/PartSegCore/analysis/measurement_calculation.py (1 hunks)
  • package/PartSegCore/class_generator.py (1 hunks)
  • package/PartSegCore/sphinx/auto_parameters.py (2 hunks)
  • package/PartSegImage/image.py (2 hunks)
  • package/tests/test_PartSeg/test_common_gui.py (0 hunks)
  • package/tests/test_PartSeg/test_sentry.py (0 hunks)
  • package/tests/test_PartSegCore/test_algorithm_describe_base.py (0 hunks)
  • package/tests/test_PartSegCore/test_analysis_batch.py (1 hunks)
  • pyproject.toml (1 hunks)
  • tutorials/tutorial_neuron_types/Neuron_types_example.ipynb (16 hunks)
💤 Files with no reviewable changes (3)
  • package/tests/test_PartSeg/test_common_gui.py
  • package/tests/test_PartSeg/test_sentry.py
  • package/tests/test_PartSegCore/test_algorithm_describe_base.py
🧰 Additional context used
🧬 Code graph analysis (5)
package/PartSeg/_roi_analysis/prepare_plan_widget.py (1)
package/PartSeg/common_backend/except_hook.py (1)
  • show_warning (83-95)
package/PartSeg/common_gui/algorithms_description.py (2)
package/PartSegCore/algorithm_describe_base.py (1)
  • _pretty_print (539-570)
package/PartSegCore/analysis/calculation_plan.py (1)
  • _pretty_print (810-829)
package/PartSegCore/analysis/measurement_calculation.py (1)
package/PartSegCore/analysis/measurement_base.py (1)
  • calculate_property (375-399)
package/PartSegCore/analysis/batch_processing/batch_backend.py (1)
package/PartSegCore/json_hooks.py (1)
  • PartSegEncoder (9-10)
package/PartSeg/plugins/napari_io/save_tiff_layer.py (1)
package/PartSegImage/image.py (2)
  • channel_names (431-432)
  • shape (654-656)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
  • GitHub Check: 4DNucleome.PartSeg (Tests_other test windows)
  • GitHub Check: 4DNucleome.PartSeg (Tests_other test macos)
  • GitHub Check: 4DNucleome.PartSeg (Tests_linux test_linux)
  • GitHub Check: 4DNucleome.PartSeg (Builds pyinstaller_linux)
  • GitHub Check: 4DNucleome.PartSeg (Builds pyinstaller windows)
  • GitHub Check: 4DNucleome.PartSeg (Builds pyinstaller macos_arm)
  • GitHub Check: 4DNucleome.PartSeg (Builds pyinstaller macos)
  • GitHub Check: 4DNucleome.PartSeg (Builds sdist)
  • GitHub Check: ubuntu-24.04 py3.12 --pre
  • GitHub Check: windows-2022 py3.12 --pre
  • GitHub Check: macos-14 py3.12 --pre
  • GitHub Check: repo py3.10
  • GitHub Check: repo py3.12
  • GitHub Check: repo py3.11
  • GitHub Check: Base py3.11 / macos-15-intel py 3.11 latest PyQt5
  • GitHub Check: Test PartSeg minimal / ubuntu-24.04 py 3.9 latest PyQt5
  • GitHub Check: test_coverage / ubuntu-24.04 py 3.12 latest PyQt5
  • GitHub Check: 4DNucleome.PartSeg (GetTestData linux)
  • GitHub Check: 4DNucleome.PartSeg (Documentation_check Notebook_check)
  • GitHub Check: 4DNucleome.PartSeg (manifest_check manifest_check)
  • GitHub Check: 4DNucleome.PartSeg (Documentation_check help)
  • GitHub Check: 4DNucleome.PartSeg (formatting_check check_formating)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Sourcery review
🔇 Additional comments (15)
package/PartSegCore/analysis/measurement_calculation.py (1)

1449-1453: Formatting change aligns with ruff-format migration.

The method signature has been reformatted from a multi-line definition to a single line, consistent with ruff's formatting conventions. The method's behavior and parameters remain unchanged, and the pylint disable comment is preserved.

tutorials/tutorial_neuron_types/Neuron_types_example.ipynb (1)

1-750: Notebook JSON structure is valid and ready.

The notebook JSON has been verified and validates correctly. All formatting is intact with no structural errors that would prevent loading or execution.

package/PartSegCore/sphinx/auto_parameters.py (1)

53-53: LGTM! Formatting-only change.

The quote style normalization in nested f-strings aligns with ruff-format rules without affecting behavior.

Also applies to: 62-62

package/tests/test_PartSegCore/test_analysis_batch.py (1)

548-548: LGTM! Formatting-only change.

Added spaces around the + operator in the f-string expression, consistent with ruff-format style.

package/PartSeg/common_gui/algorithms_description.py (1)

73-73: LGTM! Formatting-only change.

Added spaces around the + operator in the function call, consistent with ruff-format style.

package/PartSegCore/class_generator.py (1)

311-313: LGTM! Formatting-only change.

The error message string was reformatted without changing its content or behavior.

package/PartSegCore/analysis/batch_processing/batch_backend.py (1)

870-870: LGTM! Formatting-only changes.

Added spaces around the + operator in f-string expressions for Excel cell positions, consistent with ruff-format style.

Also applies to: 877-879, 884-884

package/PartSeg/common_gui/multiple_file_widget.py (1)

267-267: LGTM! Formatting-only change.

Added spaces around the + operator in the f-string expression, consistent with ruff-format style.

package/PartSeg/_roi_analysis/prepare_plan_widget.py (1)

870-870: LGTM! Formatting-only change.

Quote style normalization in the f-string aligns with ruff-format rules without affecting behavior.

package/PartSeg/common_gui/napari_image_view.py (1)

1053-1053: LGTM! Formatting-only change.

Quote style normalization in the nested f-string aligns with ruff-format rules without affecting behavior.

package/PartSeg/plugins/napari_io/save_tiff_layer.py (1)

45-45: LGTM! Formatting changes are consistent with Ruff style.

These formatting adjustments (quote style in f-strings and spacing in slice notation) have no semantic impact and align with Ruff's formatting conventions.

Also applies to: 49-49

package/PartSegImage/image.py (1)

287-287: LGTM! Consistent spacing in f-strings.

The spacing around the + operator improves readability and follows PEP 8 conventions. No functional changes to channel naming behavior.

Also applies to: 301-301

pyproject.toml (1)

266-280: LGTM! Comprehensive Ruff exclusion list.

The expanded exclude list appropriately covers version control directories, build artifacts, virtual environments, and generated files. This configuration properly replaces the removed Black configuration.

.pre-commit-config.yaml (2)

18-18: LGTM! Ruff formatter integration complete.

Adding the ruff-format hook alongside ruff-check properly completes the migration from Black to Ruff for automated formatting checks.


30-35: LGTM! Enhanced markdown formatting capabilities.

The additional mdformat dependencies (GFM, frontmatter, footnotes, alerts, ruff integration, and TOC support) provide comprehensive markdown formatting coverage aligned with the broader Ruff migration.

@codecov
Copy link

codecov bot commented Oct 20, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.18%. Comparing base (aa9269a) to head (267412d).
⚠️ Report is 23 commits behind head on develop.

Files with missing lines Patch % Lines
...ckage/PartSeg/_roi_analysis/prepare_plan_widget.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1324      +/-   ##
===========================================
- Coverage    93.20%   93.18%   -0.02%     
===========================================
  Files          210      210              
  Lines        33365    33365              
===========================================
- Hits         31097    31092       -5     
- Misses        2268     2273       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Czaki Czaki added the skip check PR title skip spellcheking PR title label Oct 20, 2025
@Czaki Czaki merged commit 3a46f96 into develop Oct 20, 2025
67 of 71 checks passed
@Czaki Czaki deleted the ruff_format branch October 20, 2025 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip check PR title skip spellcheking PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments