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

Update pre-commit configuration to use Ruff for linting and formatting #1031

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cuza
Copy link
Member

@cuza cuza commented Feb 28, 2025

Replaced pre-commit hooks flake8, black, pyupgrade, etc. with ruff for linting and formatting.

@cuza cuza requested a review from a team as a code owner February 28, 2025 22:37
@cuza cuza force-pushed the cuza/replacing-flake8,-black,-pyupgrade,etc-with-ruff branch from 7002922 to afcf3b5 Compare March 4, 2025 20:03
Copy link
Member

@EvanKrall EvanKrall left a comment

Choose a reason for hiding this comment

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

It would be interesting to upgrade black to 24.8.0 first, and then double-check that ruff isn't doing anything black wouldn't do.

This is more palatable than it was originally; most of the changes fall into a few categories:

  1. added or removed blank lines (not a problem in git blame)
  2. things that black should already be doing (splitting mock.patch args onto multiple lines with commas)
  3. changes that we should make anyway (combining f"a b c" f"d e f" into f"a b cd e f"

@@ -120,7 +121,7 @@ def make_k8s_options():


def make_action(**kwargs):
kwargs.setdefault("name", "action"),
(kwargs.setdefault("name", "action"),)
Copy link
Member

Choose a reason for hiding this comment

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

we should probably just not have the trailing comma on these lines

hooks:
- id: black
args: [--target-version, py38]
files: ^tests/.*\.py$
Copy link
Member

Choose a reason for hiding this comment

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

I think this shows that we want to keep end-of-file-fixer

Copy link

Choose a reason for hiding this comment

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

I got rid of it in my pr, I think this is covered by W292
https://docs.astral.sh/ruff/rules/missing-newline-at-end-of-file/

pod_annotations=mock_k8s_action_run.command_config.annotations,
service_account_name=mock_k8s_action_run.command_config.service_account_name,
ports=mock_k8s_action_run.command_config.ports,
), mock_get_cluster.return_value.create_task.calls
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just delete the , mock_get_cluster.return_value.create_task.calls -- I assume this was meant to have the list of calls be included into the assertion error. If you delete that, then ruff shouldn't want to change the indentation here.

With an assert statement, extra data that is passed in after the comma will be included into the AssertionError:

>>> assert 1==2, "hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: hello

but I'm pretty sure that doesn't work with assert_called_once_with:

>>> from unittest import mock
>>> a = mock.Mock()
>>> a.b.assert_called_once_with(3), "hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/unittest/mock.py", line 924, in assert_called_once_with
    raise AssertionError(msg)
AssertionError: Expected 'b' to be called once. Called 0 times.

Comment on lines +40 to +43
with mock.patch(
"tron.kubernetes.PyDeferredQueue",
autospec=True,
), mock.patch(
Copy link
Member

Choose a reason for hiding this comment

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

Surprised this isn't already formatted like this. How old of black are we running?

Copy link
Member

Choose a reason for hiding this comment

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

Looks like black 24.8.0 (last version of black that supports py38) also formats it this way.

), mock.patch(
"tron.config.static_config.load_yaml_file",
autospec=True,
), mock.patch("tron.utils.logreader.get_superregion", autospec=True, return_value="fake"), mock.patch(
Copy link
Member

Choose a reason for hiding this comment

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

I'd add the trailing comma here after return_value="fake"

"tron.utils.logreader.S3LogsReader", autospec=True
) as mock_s3_reader:

), mock.patch("tron.utils.logreader.S3LogsReader", autospec=True) as mock_s3_reader:
Copy link
Member

Choose a reason for hiding this comment

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

I'd add trailing comma after autospec=True

constraints=[["an attr", "an op", "a val"]],
docker_parameters=[{"key": "init", "value": "true"}],
**self.other_task_kwargs,
), mock_get_cluster.return_value.create_task.calls
Copy link
Member

Choose a reason for hiding this comment

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

see below

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