[ruff] Enable FURB rules for code modernization - #9896
Conversation
|
👋 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! 🙏 |
|
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/activateNow you can run (Added by the PR bot) |
There was a problem hiding this comment.
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()andremovesuffix()methods - Updates
re.Xflag to the more explicitre.VERBOSEflag - 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() |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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:
str.removeprefix()andstr.removesuffix()instead of manual string slicing (Python 3.9+ feature)re.Xwith the more explicitre.VERBOSEflagwritelines()with generator expressions for better memory efficiencyThese changes improve code readability and leverage modern Python features without affecting functionality.
Types of changes
Related issue or feature (if applicable):
None
Pull request in esphome-docs with documentation (if applicable):
Not applicable - no user-facing changes
Test Environment
Example entry for
config.yaml:# Not applicable - no configuration changesChecklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: