Skip to content
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

Add Ruff as a code checker #222

Merged
merged 2 commits into from
Jul 18, 2023
Merged

Conversation

JacobCallahan
Copy link
Member

@JacobCallahan JacobCallahan commented Jul 7, 2023

Ruff is a newer code checker that is written in Rust and is very fast. Adding this now with a number of pretty sane checks to start with. I recommend having Ruff integrated into your editor to pick out issues as you write code.

Most of the changes in this PR are adding/modifying docstrings.

@JacobCallahan
Copy link
Member Author

Available rules can be found here: https://beta.ruff.rs/docs/rules/

If you find one you think should be added, comment with your reasoning.

@JacobCallahan
Copy link
Member Author

All tests passed locally, including functional for all providers.
Screenshot from 2023-07-07 20-21-56

broker/broker.py Show resolved Hide resolved
ruff.toml Outdated Show resolved Hide resolved
broker/exceptions.py Outdated Show resolved Hide resolved
Comment on lines 135 to 145
filter_list = eval(
filter_list = eval( # noqa: S307
raw_f.replace(f"@{filter_key}", filter_key), {filter_key: filter_list}
)
filter_list = filter_list if isinstance(filter_list, list) else [filter_list]
filter_list = (
filter_list if isinstance(filter_list, list) else [filter_list]
)
elif f"@{filter_key}" in raw_f:
# perform an attribute filter on each host
filter_list = list(
filter(
lambda item: eval(
lambda item: eval( # noqa: S307
Copy link
Member

Choose a reason for hiding this comment

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

S307 | suspicious-eval-usage | Use of possibly insecure function; consider using ast.literal_eval

Could you perhaps use ast.literal_eval?

Copy link
Member Author

Choose a reason for hiding this comment

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

unfortunately, no. we need to pass in the filterable data structure and ast.literal_eval can't do that.

Comment on lines -497 to +529
if request := _frame.frame.f_locals.get("request"):
if request := _frame.frame.f_locals.get("request"): # noqa: F821
Copy link
Member

Choose a reason for hiding this comment

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

Just me being curious, where is _frame being defined?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is largely due to the order in which the frames are available, so it gives us the ability to "look back" at the previous frame to get the request object from the actual fixture instead of the pytest-internal frame we'd be at otherwise.

ruff.toml Outdated
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
"D406", # Section name should end with a newline
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for ignoring D406?

Copy link
Member Author

Choose a reason for hiding this comment

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

See comment about D407 ;)

ruff.toml Outdated
# False positives https://github.com/astral-sh/ruff/issues/5386
"PLC0208", # Use a sequence type instead of a `set` when iterating over values
# Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923
"PLR0913", # Too many arguments to function call ({c_args} > {max_args})
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it better to define a sane number of max arguments?
https://beta.ruff.rs/docs/settings/#pylint-max-args

Copy link
Member Author

Choose a reason for hiding this comment

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

Sane is up to personal preference and tool compatibility. Most of the times this was triggered in Broker was due to click command options.

ruff.toml Outdated Show resolved Hide resolved
ruff.toml Outdated
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
Copy link
Member

Choose a reason for hiding this comment

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

Why can't broker be compliant with pydocstyle recommendations?
I would like to see at least D202 not being ignored.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can use D202, but I don't like the others.

ruff.toml Outdated Show resolved Hide resolved
Ruff is a newer code checker that is written in Rust and is very fast.
Adding this now with a number of pretty sane checks to start with.
I recommend having ruff integrated into your editor to pick out issues
as you write code.
@JacobCallahan
Copy link
Member Author

@ogajduse thanks for the thorough review! Updated all I saw to address and a little more.

@JacobCallahan JacobCallahan requested a review from ogajduse July 12, 2023 12:52
Switch all relevant config files to pyproject.toml
Also added in pre-commit checks for black and ruff
Bumped out python version 3.9
Added in dependabot config for actions, since others are largely unpinned
try:
host_obj.teardown()
except Exception as err:
except Exception as err: # noqa: BLE001
logger.debug(f"Tell Jake the exception was: {err}")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logger.debug(f"Tell Jake the exception was: {err}")
logger.exception(f"Tell Jake the exception was: {err}")

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll keep as-is since the creation of the exception class below will also log as an error.

@JacobCallahan JacobCallahan merged commit 6f1bdee into SatelliteQE:0.4.0 Jul 18, 2023
@JacobCallahan JacobCallahan deleted the 0.4.0 branch July 18, 2023 16:39
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.

3 participants