-
Notifications
You must be signed in to change notification settings - Fork 113
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
Replace flake8
, isort
, pylint
and black
by ruff
#2149
Replace flake8
, isort
, pylint
and black
by ruff
#2149
Conversation
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
…ruff` Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
Signed-off-by: Yury Fedotov <[email protected]>
flake8
, isort
, pylint
and black
by ruff
... | ||
pass |
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.
Otherwise, the ruff formatter reformats that to one liner:
def process_data(raw_data, train_test_split): ...
And test_task_node_metadata
in package/tests/test_api/test_rest/test_responses.py
fails. See my modification to that test, to make it compatible with pass
, below.
Makefile
Outdated
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.
Note how this simplifies after we define all settings, including directories and rulesets, once, in a central ruff.toml
configuration file. This allows all "parties" that use ruff
:
- Developers
- CI
- Pre-commit hooks
To run just ruff check
and ruff format
from anywhere in the repo, and it applies a corresponding configuration automatically.
@@ -14,31 +14,11 @@ repos: | |||
- id: check-json # Checks json files for parseable syntax. | |||
- id: check-case-conflict # Check for files that would conflict in case-insensitive filesystems | |||
- id: check-merge-conflict # Check for files that contain merge conflict strings. | |||
- id: debug-statements # Check for debugger imports and py37+ `breakpoint()` calls in python source. |
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.
ruff
's T100
ruleset, which I'm enabling, checks and autofixes that, so this pre-commit hook becomes redundant.
[lint.mccabe] | ||
max-complexity = 18 | ||
|
||
[lint.pylint] | ||
max-args = 12 |
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.
I checked all arguments that your pylint
, isort
and flake8
configs used, and they are all default except for those
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.
Diffs like this, or e.g. adding a blank line between module docstring and first import line, is what ruff format
does automatically due to its minor differences with black
. There's no config for ruff format
to make it fully backwards compatible.
name='apply_types_to_shuttles', | ||
tags='shuttles' | ||
name="apply_types_to_shuttles", | ||
tags="shuttles", |
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.
Another autofix from ruff format
- makes sense, since now single and double quotes are mixes within the same node
definition. I'm curious why previous formatter didn't flag that.
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.
The formatter was run only on package/
. The demo-project
is outside of the formatter.
get_top_shuttles_data, | ||
make_cancel_policy_bar_chart, | ||
make_price_analysis_image, | ||
make_price_histogram, | ||
get_top_shuttles_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.
I'm curious why isort
didn't catch that in the past...
Signed-off-by: Yury Fedotov <[email protected]>
@@ -42,7 +43,7 @@ def _create_base_api_app() -> FastAPI: | |||
@app.middleware("http") | |||
async def set_secure_headers(request, call_next): | |||
response = await call_next(request) | |||
secure_headers.framework.fastapi(response) # pylint: disable=no-member |
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.
I have deleted all pylint: disable...
directives from the entire codebase.
Signed-off-by: Yury Fedotov <[email protected]>
flake8
, isort
, pylint
and black
by ruff
flake8
, isort
, pylint
and black
by ruff
🔥 Thank you @yury-fedotov for this PR! |
extend = "../ruff.toml" | ||
|
||
[lint.isort] | ||
known-first-party = ["kedro_viz"] |
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.
should we also add kedro here as third party?
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.
Yes - added.
Signed-off-by: Yury Fedotov <[email protected]>
@@ -0,0 +1,5 @@ | |||
extend = "../ruff.toml" | |||
|
|||
[lint.isort] |
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.
Is this a best practice to have ruff.toml ? Can we do this in demo-project/pyproject.toml -> table [tool.ruff.lint.isort] ?
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.
ruff
supports both pyproject.toml
section and dedicated ruff.toml
- see here. So yes what you propose is feasible.
There is no explicit recommendation in ruff
docs on which to prefer, and I have not heard of what's generally considered best practice. My thinking in general is that whenever possible, I'd prefer a dedicated config for every tool, because it:
- Makes each config file have isolated responsibility
- Simplifies version control a bit - e.g. 2 PRs where one changes dependencies and another changes the lint rulesets wouldn't touch the same file
- Makes
pyproject.toml
smaller and more manageable
So I use ruff.toml
in all of my projects, and never put that in pyproject.toml
because of above.
Let me know if you disagree & still prefer the pyproject.toml
- I can change to that. If so, then probably we'd need to configure ruff
via pyproject.toml
not only in demo-project
but also in repo root and package
.
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.
I came up with a better argument :)
- It's useful to have
ruff
configuration in repo root, so that bothpackage
,demo-project
or other packages can inherit it - Repo root is not a Python package, so
ruff.toml
is preferred overpyproject.toml
in root - (1 + 2) sort of require us to use
ruff.toml
in the root, so for consistency, I suggest usingruff.toml
files inpackage
anddemo-project
too.
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.
Sure, I am happy to have ruff.toml, I wanted to understand if this was a better practice suggested from ruff. Thanks for the PR
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.
LGTM. thanks a lott @yury-fedotov
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.
Thank you @yury-fedotov 💯
[lint.per-file-ignores] | ||
"*/cli_steps.py" = ["B011"] # assert False instead of AssertionError | ||
"*/base_deployer.py" = ["B024"] # ABCs without abstract methods | ||
"package/kedro_viz/__init__.py" = ["B028"] # Risky usage of positional arguments |
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.
Q: does it make sense to ignore some of these specific issues in the code with noqa inline instead of here? Can keep the ignores that apply to multiple files here.
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.
I definitely wouldn't want to ignore the 'broad exception' rule in general, because I think we should avoid it as much as possible. I guess some others like # noqa: ARG002 should be fine.
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.
Broad exception rule has BLE
code in ruff
and is selected for entire repo ✅ (See line 14 of this file)
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.
Good question!
The way I implemented it is as follows:
- Whatever has an inline NOQA before that's applied to 1-2 places, stays as inline NOQA.
- Whatever was a file-level
pylint: disable
statement at the beginning of the file is moved toruff.toml
from that separate file.
Let me explain the rationale for (2).
Consider two implementations, using package/kedro_viz/integrations/kedro/sqlite_store.py
as an example:
Before
# pylint: broad-exception-caught
At the beginning of that particular file.
After
"package/kedro_viz/integrations/kedro/sqlite_store.py" = ["BLE"]
In ruff.toml
- In both cases, we essentially store the same directive - disable a particular rule in a particular file.
- However (2) makes it more explicit and centralizes all those directives, which would otherwise be scattered across tens of files, in a single file.
- Long term, that helps gradually make the codebase adhere to those rulesets, and delete those
per-file-ignores
. As opposed to distributed inline NOQAs that typically just never addressed.
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.
Overall looks good but left a question regarding the ignoring of errors.
Thanks @yury-fedotov! 💯
@yury-fedotov , feel free to merge this PR :) |
I don't have write access @rashidakanchwala |
no worries, let me merge. Once again Thank you!, this is big improvement to our dev workflow! |
Description
Closes #1670
Development notes
This PR brings
ruff
tokedro-viz
to replace:flake8
isort
pylint
black
It introduces a central
ruff.toml
file in repo root, to act as a single source of truth for Python codestyle management, and leveragesruff
's hierarchical configuration feature to make this config applicable to both thedemo-project
andpackage
parts of the codebase.Note
Another enhancement is that linter/formatter configuration, such as:
Is now defined in a single file, once, as opposed to being duplicated in
Makefile
,pre-commit
config etc., which should streamline further code quality management by simplifying addition of rulesets, or directories in scope.Note for reviewers
While the diff from this PR is quite big (72 files), most of that is very simple auto-fixes from
ruff format
that are slightly different fromblack
. So reviewing should not be very hard.Testing plan
make lint
passes locallymake pytest
passes locallymerge-gatekeeper
to run full CIOther tasks
RELEASE.md
file