Skip to content

ruff format #550

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

ruff format #550

wants to merge 1 commit into from

Conversation

polyfloyd
Copy link
Contributor

@polyfloyd polyfloyd commented May 25, 2025

I started out by wanting to fix that failing ruff check workflow which complains about single quotes.

I took it a little further and used ruff format and added it to the CI too. Please let me know what you think :)

Summary by Sourcery

Apply ruff-based code formatting across the Python codebase and enforce formatting in the CI pipeline

Enhancements:

  • Reformat source files for consistent line wrapping, indentation, and quoting according to ruff rules

CI:

  • Add a CI job to verify code formatting with ruff format --check --diff

Copy link

sourcery-ai bot commented May 25, 2025

Reviewer's Guide

This PR applies automated formatting via Ruff across the Python codebase, restructuring long lines into multi-line expressions (especially logging, solver invocations, and DataFrame pipelines) and adds a Ruff format check to the CI workflow to enforce consistent styling.

File-Level Changes

Change Details Files
Standardized code formatting with Ruff
  • Split long f-string logging/debug calls into multi-line blocks
  • Wrapped lengthy function and class instantiation calls (e.g., solver commands, DataFrame.from_dict, resample/aggregate chains) across lines with consistent indentation
  • Reformatted list concatenations, dict literals, and mathematical expressions into multi-line expressions
  • Added trailing commas in multi-line argument lists for better diff hygiene
src/emhass/optimization.py
src/emhass/utils.py
src/emhass/forecast.py
src/emhass/retrieve_hass.py
src/emhass/command_line.py
Enforced formatting in CI
  • Added a new “Check formatting” step running ruff format --check --diff
  • Integrated Ruff format validation into existing code-quality workflow
.github/workflows/code-quality.yml

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

Copy link

Copy link

@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 @polyfloyd - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

Comment on lines 631 to 634
if isinstance(forecast_input, str):
if isinstance(ast.literal_eval(forecast_input), list):
forecast_input = ast.literal_eval(
forecast_input
)
forecast_input = ast.literal_eval(forecast_input)
runtimeparams[forecast_key] = forecast_input
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Merge nested if conditions (merge-nested-ifs)

Suggested change
if isinstance(forecast_input, str):
if isinstance(ast.literal_eval(forecast_input), list):
forecast_input = ast.literal_eval(
forecast_input
)
forecast_input = ast.literal_eval(forecast_input)
runtimeparams[forecast_key] = forecast_input
if isinstance(forecast_input, str) and isinstance(ast.literal_eval(forecast_input), list):
forecast_input = ast.literal_eval(forecast_input)
runtimeparams[forecast_key] = forecast_input


ExplanationToo much nesting can make code difficult to understand, and this is especially
true in Python, where there are no brackets to help out with the delineation of
different nesting levels.

Reading deeply nested code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if conditions can be combined using
and is an easy win.

self.logger.error("EMHASS was unable to obtain configuration data from Home Assistant")
self.logger.error(
"EMHASS was unable to obtain configuration data from Home Assistant"
)
return False

def get_data(
Copy link

Choose a reason for hiding this comment

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

issue (code-quality): We've found these issues:


Explanation

The quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.

How can you solve this?

It might be worth refactoring this function to make it shorter and more readable.

  • Reduce the function length by extracting pieces of functionality out into
    their own functions. This is the most important thing you can do - ideally a
    function should be less than 10 lines.
  • Reduce nesting, perhaps by introducing guard clauses to return early.
  • Ensure that variables are tightly scoped, so that code using related concepts
    sits together within the function rather than being scattered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant