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

Switch to ruff / Add pre-commit hook #4428

Merged
merged 9 commits into from
Jun 11, 2024
Merged

Switch to ruff / Add pre-commit hook #4428

merged 9 commits into from
Jun 11, 2024

Conversation

spreeni
Copy link
Contributor

@spreeni spreeni commented Jun 9, 2024

I switched from black/isort/autoflake to ruff, which is much faster, combines these tools and also is a strong linter. Additionally, I set up a pre-commit hook that can optionally be used to enforce QA-checks before each commit. I personally find it very satisfying to work with this, as it often corrects/spots smaller errors.

I exclude stricter linting rules that might limit our development style, but applied multiple other linting rules which I think are sensible.

ruff linting modules that I normally use but omitted for now are pydocstyle ("D") and mccabe complexity ("C90"), as some functionality would need to be refactored to reduce complexity in our case and enforcing doc strings everywhere might also be quite restrictive.

Backend checklist

  • Formatted my code by running ruff check --select I --fix . && ruff check . && ruff format . in app/backend
  • Added tests for any new code or added a regression test if fixing a bug
  • All tests pass
  • Run the backend locally and it works
  • Added migrations if there are any database changes, rebased onto develop if necessary for linear migration history

@spreeni spreeni self-assigned this Jun 9, 2024
Copy link

vercel bot commented Jun 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
couchers ✅ Ready (Inspect) Visit Preview Jun 11, 2024 7:41am

@spreeni spreeni added 1.topic backend This issue relates to the python backend refactoring labels Jun 9, 2024
Copy link
Member

@aapeliv aapeliv left a comment

Choose a reason for hiding this comment

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

Neat! Definitely caught a few subtle bugs/issues here, thanks for introducing the linter! I even learned some more python syntax I wasn't even aware of!

Personally I don't mind the empty f-strings, and I doubt they really affect performance much. But I guess good to get rid of either way.

Comment on lines 14 to 22
ignore = [
"B007", # Loop control variable not used within loop body
"E501", # Line too long
"E711", # Comparison to `None` should be `cond is None`
"E712", # Avoid equality comparisons to `True`; use `if approved:` for truth checks
"F811", # Redefinition of unused variable
"F841", # Local variable is assigned to but never used
"UP015", # Unnecessary open mode parameters
]
Copy link
Member

Choose a reason for hiding this comment

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

Is this still an accurate list? These linters rules seem useful, are we failing a lot of them?

Copy link
Contributor Author

@spreeni spreeni Jun 10, 2024

Choose a reason for hiding this comment

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

So after looking at the code these are checks that I excluded for the following reasons, correct me if I am wrong:

  • B007: Sometimes we might want to have the variable unpacking to make clear what is being iterated over, no? As in for topic, name, items in group:, even if we just use topic/items?
  • E501: Would need quite a bit broken up or marked as noqa, as it also considers comments and long strings
  • E711: We use None to compare to sqlalchemy results - Correct me if I'm wrong, but I think these are not actually None, but just falsy?
  • E712: Same as above, just for True
  • F811: This currently applies to all fixture usages. We could fix this by not importing fixtures but having them in a conftest.py file
  • F841: This I assumed often refers to things that are in a variable that might be valuable later on, so I did not want to remove them all
  • UP015: Personal opinion, but I like to be excplicit about the open-mode

For each rule, you can comment out the ignore-line and check which lines would be affected to see for yourself as well. I think for F841 especially, you will be able to judge better what to exclude and what to leave.

Choose a reason for hiding this comment

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

To B007: You can put an underscore in front of the variable that is not required, but named. E.g: for topic, _name, items in group:.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@HansBambel Just saw that you commented, great to see you here 😄

@spreeni
Copy link
Contributor Author

spreeni commented Jun 10, 2024

These are current files that are over 350kb - I guess most of these are fine, but maybe good to have an eye on it.

app/web/yarn.lock (658 KB) exceeds 350 KB.
docs/full_er_diagram.png (1585 KB) exceeds 350 KB.
app/web/features/dashboard/Hero/mesut-kaya-eOcyhe5-9sQ-unsplash.jpeg (3825 KB) exceeds 350 KB.
app/web/public/img/blog/20210407_lucas.jpg (484 KB) exceeds 350 KB.
app/web/public/img/blog/20210825_talkingtown.png (8739 KB) exceeds 350 KB.
app/backend/src/data/osm/80500.geojson (2582 KB) exceeds 350 KB.
app/web/public/img/blog/20210501_virtual_events.jpg (551 KB) exceeds 350 KB.
app/mobile/src/yarn.lock (747 KB) exceeds 350 KB.
app/backend/src/data/osm/62149.geojson (3206 KB) exceeds 350 KB.
app/backend/src/data/osm/61320.geojson (490 KB) exceeds 350 KB.
docs/current_er_diagram.png (1352 KB) exceeds 350 KB.
app/web/public/img/blog/20210604_mindy.jpg (439 KB) exceeds 350 KB.
app/backend/src/data/osm/54224.geojson (1003 KB) exceeds 350 KB.
app/backend/src/data/osm/148838.geojson (11182 KB) exceeds 350 KB.

@spreeni
Copy link
Contributor Author

spreeni commented Jun 10, 2024

At some point, we could also integrate clang-format and eslint into the hook. Then a new person could just code away and would always have fully linted code when it came to the CI/CD pipeline.

This formatted the files, but always fails with an error "No such file or directory", so I left it out for now.

-   repo: https://github.com/pre-commit/mirrors-clang-format
    rev: 'v18.1.6'
    hooks:
      - id: clang-format
        args: ['--style=file:app/proto/.clang-format', '-i', '*.proto']
        files: ^.*.proto

aapeliv
aapeliv previously approved these changes Jun 10, 2024
Copy link
Member

@aapeliv aapeliv left a comment

Choose a reason for hiding this comment

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

I think you addressed all my questions. Looks good to me now!

# Conflicts:
#	app/backend/requirements.txt
@spreeni
Copy link
Contributor Author

spreeni commented Jun 11, 2024

@aapeliv I merged develop into the branch, ready to be merged once you approve it

Copy link
Member

@aapeliv aapeliv left a comment

Choose a reason for hiding this comment

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

Thanks, please merge!

@spreeni spreeni merged commit 45fbe38 into develop Jun 11, 2024
2 checks passed
@spreeni spreeni deleted the refactor/add-ruff branch June 11, 2024 14:08
@aapeliv
Copy link
Member

aapeliv commented Jun 11, 2024

I really like this... it's so friggin fast xD

bakeiro pushed a commit that referenced this pull request Jul 30, 2024
* Switch to ruff and applied linting rules

* Add pre-commit and apply hooks to all files; Added ruff configuration to media and client

* Restrict ruff pre-commit hook to only the backend

* Adjusted pull request checklist template

* Fix set error

* Implementing of code review findings; Fix of further lint findings

* Remove check-json from pre-commit; Applied clang-format

* Fixed requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.topic backend This issue relates to the python backend refactoring
Development

Successfully merging this pull request may close these issues.

3 participants