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

Proposal: Add ruff (linter and formatter) #40

Merged
merged 10 commits into from
Aug 12, 2024
Merged

Proposal: Add ruff (linter and formatter) #40

merged 10 commits into from
Aug 12, 2024

Conversation

SmittieC
Copy link

@SmittieC SmittieC commented Feb 14, 2024

Things will get ruff if we don't add something like this sooner or later. This is a linter and formatter. This is apparently way faster than tools like black and flake8.

I also added a Makefile to reduce the amount of commands one has to run each time working with this.

It's base branch is my latest branch based off of nh/webhook. This is to avoid as much merge conflicts as possible if/when this is merged

@SmittieC SmittieC changed the title Proposal: Add ruff as a pre-commit hook Proposal: Add ruff (linter and formatter) Feb 14, 2024
Copy link
Contributor

@kaapstorm kaapstorm left a comment

Choose a reason for hiding this comment

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

I love this idea. I am a fan of ruff. I use it in my own projects. It's the best linter.

But I will push back hard on a couple of those formatting choices: Either use single quotes, or leave the quotes alone. Also, these bracket decisions do not improve readability.

Comment on lines 30 to 32
template_path = os.sep.join(
(os.path.dirname(os.path.abspath(__file__)), "templates")
)
Copy link
Contributor

Choose a reason for hiding this comment

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

No.

Copy link
Author

Choose a reason for hiding this comment

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

Haha agreed!

@@ -36,8 +36,8 @@ def save_token(token, request):
tok = Token(
client_id=client.client_id,
expires_at=expires_at,
access_token=token['access_token'],
token_type=token['token_type'],
access_token=token["access_token"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Absolutely not.

Copy link
Author

Choose a reason for hiding this comment

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

You not a fan of double quotes?

Copy link
Author

Choose a reason for hiding this comment

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

You might find a840441 to your liking?

pyproject.toml Outdated
@@ -0,0 +1,5 @@
[tool.ruff]
line-length = 120
Copy link
Author

Choose a reason for hiding this comment

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

I can't remember, do we as dimagi use 120 chars as the line length?

Copy link
Contributor

Choose a reason for hiding this comment

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

For flake8, it's currently set to 115 lines for CommCare HQ (code reference)

Copy link
Contributor

Choose a reason for hiding this comment

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

We use 115 (to make code review easier (if your browser is maximized on a 1280-wide screen)).

(Personally, I prefer PEP-8 line lengths (79 for code, 72 for docstrings) because (1) it makes code readable when you split your windows vertically -- code on the left, tests on the right -- and (2) long lines of text are ugly and hard to read, which is why newspapers have columns.)

pyproject.toml Outdated
@@ -0,0 +1,5 @@
[tool.ruff]
line-length = 115
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious what you think of this:

Suggested change
line-length = 115
line-length = 79 # Preferred line length to wrap lines
[tool.ruff.lint.pycodestyle]
max-line-length = 115 # Line length to allow for line-too-long violations

I noticed that Ruff will not reformat strings if they exceed line-length.

Copy link
Author

Choose a reason for hiding this comment

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

79 is probably fine.

I noticed that Ruff will not reformat strings if they exceed line-length.

Weird

line-length = 115

[tool.ruff.format]
quote-style = "single"
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that Ruff has a "preserve" option to leave quotes as-is, but that it prefers to follow PEP-8 which says, "Pick a rule and stick to it." So, okay, fair enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

I find it ironic that "single" is in double quotes.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah that's my reasoning as well. I don't mind single or double quotes. I presume you prefer single quotes?

Copy link
Author

Choose a reason for hiding this comment

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

Oh I see you said it just below: "I prefer single"

Copy link
Contributor

@kaapstorm kaapstorm left a comment

Choose a reason for hiding this comment

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

On principle I think this is a good idea. I think this is also a good codebase to use this in. And I'm coming around to liking the idea of having only one choice of quotation marks. I prefer single, but I could also make an argument in favor of double. ... I do like 79-character lines though, because it makes my IDE layout happy.

@SmittieC
Copy link
Author

On principle I think this is a good idea. I think this is also a good codebase to use this in. And I'm coming around to liking the idea of having only one choice of quotation marks. I prefer single, but I could also make an argument in favor of double. ... I do like 79-character lines though, because it makes my IDE layout happy.

Cool. I don't have any objections to 79 character lines. I'll make an update

@mkangia
Copy link
Contributor

mkangia commented Feb 16, 2024

Do we still need isort on top of this? I like that it fixes files automatically

Does ruffle also take care of line spacing between methods? I have always used 2 spaces and now assume that is the standard.

@mkangia
Copy link
Contributor

mkangia commented Feb 16, 2024

PR is still in draft though.

@SmittieC
Copy link
Author

FYI: I snuck d1ee5ae into this PR.

@SmittieC
Copy link
Author

Do we still need isort on top of this? I like that it fixes files automatically

Does ruffle also take care of line spacing between methods? I have always used 2 spaces and now assume that is the standard.

Looks like we can get sorting with 8aec7d9. As for the spaces between methods, I'm not 100% sure. Will have to look into this a bit more, but I'm sure one can achieve this.

@mkangia
Copy link
Contributor

mkangia commented Feb 16, 2024

Looks like we can get sorting with 8aec7d9.

Great, thanks

@Charl1996
Copy link
Contributor

@SmittieC What is the status of this PR?

@SmittieC SmittieC marked this pull request as ready for review August 8, 2024 11:39
@SmittieC
Copy link
Author

SmittieC commented Aug 8, 2024

@SmittieC What is the status of this PR?

I forgot about this. If people are happy I think we can merge

Copy link
Contributor

@kaapstorm kaapstorm left a comment

Choose a reason for hiding this comment

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

Happy

@SmittieC SmittieC merged commit dfec7cc into cs/updates Aug 12, 2024
4 checks passed
@SmittieC SmittieC deleted the cs/add_ruff branch August 12, 2024 06:08
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.

5 participants