-
Notifications
You must be signed in to change notification settings - Fork 96
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
base: master
Are you sure you want to change the base?
ruff format #550
Conversation
Reviewer's GuideThis 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
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
There was a problem hiding this 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
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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 |
There was a problem hiding this comment.
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
)
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 | |
Explanation
Too much nesting can make code difficult to understand, and this is especiallytrue 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( |
There was a problem hiding this comment.
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:
- Replace manual loop counter with call to enumerate (
convert-to-enumerate
) - Extract duplicate code into method (
extract-duplicate-method
) - Low code quality found in RetrieveHass.get_data - 7% (
low-code-quality
)
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.
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:
CI:
ruff format --check --diff