8000
Skip to content

[ruff] Enable FURB rules for code modernization - #9896

Merged
jesserockz merged 1 commit into
devfrom
refurb_ruff
Jul 26, 2025
Merged

[ruff] Enable FURB rules for code modernization#9896
jesserockz merged 1 commit into
devfrom
refurb_ruff

Conversation

@bdraco
@bdraco bdraco commented Jul 25, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR enables the FURB (refurb) linting rules in ruff to modernize the codebase and improve code quality. The changes include:

  • Using str.removeprefix() and str.removesuffix() instead of manual string slicing (Python 3.9+ feature)
  • Replacing re.X with the more explicit re.VERBOSE flag
  • Using writelines() with generator expressions for better memory efficiency
  • Modernizing string manipulation patterns throughout the codebase

These changes improve code readability and leverage modern Python features without affecting functionality.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

None

Pull request in esphome-docs with documentation (if applicable):

Not applicable - no user-facing changes

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# Not applicable - no configuration changes

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

Copilot AI review requested due to automatic review settings July 25, 2025 19:12
@bdraco
bdraco requested review from a team, clydebarrow and kahrendt as code owners July 25, 2025 19:12
@github-actions
Copy link
Copy Markdown
Contributor

👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR.

@clydebarrow, @kahrendt, @synesthesiam - You've been requested to review this PR as codeowner(s) of 6 file(s) that were modified. Thanks for your time! 🙏

@github-actions
github-actions Bot requested a review from synesthesiam July 25, 2025 19:13
@github-actions
Copy link
Copy Markdown
Contributor

To use the changes in this PR:

# Clone the repository:
git clone https://github.com/esphome/esphome
cd esphome

# Checkout the PR branch:
git fetch origin pull/9896/head:refurb_ruff
git checkout refurb_ruff

# Install the development version:
script/setup

# Activate the development version:
source venv/bin/activate

Now you can run esphome as usual to test the changes in this PR.


(Added by the PR bot)

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enables FURB (refurb) linting rules in ruff to modernize the Python codebase by leveraging Python 3.9+ features and improving code readability. The changes focus on string manipulation improvements and regular expression flag clarity without affecting functionality.

Key changes:

  • Replaces manual string prefix/suffix removal with removeprefix() and removesuffix() methods
  • Updates re.X flag to the more explicit re.VERBOSE flag
  • Optimizes file writing using writelines() with generator expressions

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pyproject.toml Enables FURB linting rules in ruff configuration
script/generate-esp32-boards.py Replaces loop-based file writing with memory-efficient writelines()
esphome/writer.py Updates regex flag from re.M to re.MULTILINE for clarity
esphome/cpp_generator.py Modernizes string prefix removal using removeprefix()
esphome/components/stepper/init.py Replaces manual suffix removal with removesuffix()
esphome/components/speaker/media_player/init.py Updates prefix removal to use removeprefix()
esphome/components/lvgl/helpers.py Changes re.X to re.VERBOSE for better readability
esphome/components/logger/init.py Updates regex flag to re.VERBOSE
esphome/components/display/init.py Modernizes suffix removal with removesuffix()

Comment thread esphome/components/stepper/__init__.py
Comment thread esphome/components/stepper/__init__.py
@codecov-commenter
codecov-commenter commented Jul 25, 2025
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 60.11%. Comparing base (f808c38) to head (a352210).
⚠️ Report is 1 commits behind head on dev.

Files with missing lines Patch % Lines
esphome/writer.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #9896      +/-   ##
==========================================
+ Coverage   60.09%   60.11%   +0.01%     
==========================================
  Files          51       51              
  Lines       10303    10302       -1     
  Branches     1372     1371       -1     
==========================================
+ Hits         6192     6193       +1     
+ Misses       3755     3754       -1     
+ Partials      356      355       -1     

☔ 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.

@jesserockz
jesserockz merged commit d64e4d3 into dev Jul 26, 2025
95 checks passed
@jesserockz
jesserockz deleted the refurb_ruff branch July 26, 2025 08:54
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0