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

Feat/black formatter: add black to pre-commit config and github workflow #30

Merged
merged 6 commits into from
Apr 25, 2024

Conversation

mlederbauer
Copy link
Owner

Description:

This PR introduces the Black code formatter into our development process. Black is a highly opinionated, deterministic formatter that allows us to focus more on writing code rather than formatting it.

Changes include:

Added Black to our pre-commit hooks in .pre-commit-config.yaml. This will ensure that code is automatically formatted when committing changes.

- repo: https://github.com/psf/black
  rev: 22.12.0
  hooks:
    - id: black
      args: ["-l", "79”]

Updated our GitHub workflow in .github/workflows/tests.yml to include a step for checking code formatting with Black. This will help us maintain a consistent code style across all contributions.

- name: Format with Black
  run: |
    pip install black
    black --check --line-length 79 .

By integrating Black into our development process, we can ensure a consistent, PEP 8 compliant code style across the project, and allow contributors to focus more on the logic of their code rather than its formatting. Kindly lmk what you think

@mlederbauer
Copy link
Owner Author

image
Looks like this then in a workflow setting

@mlederbauer
Copy link
Owner Author

A lot of files were changed due to adding black I hope it’s not too unreadable

@kbiniek
Copy link
Collaborator

kbiniek commented Apr 25, 2024

Just to understand correctly, if you have black non-conforming code and commit, black automatically formats your files and commits the formatted code instead of your original one?

@kbiniek kbiniek self-requested a review April 25, 2024 09:50
Copy link
Collaborator

@kbiniek kbiniek left a comment

Choose a reason for hiding this comment

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

Very nice tho, thank you!

@@ -20,5 +20,9 @@ jobs:
run: |
pip install poetry
poetry install
- name: Format with Black
run: |
pip install black
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could also install black using poetry and just run poetry run black then we dont need the pip install
(same for pytest and pylint when/if we implement them)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes great catch! lemme check and add it

@mlederbauer
Copy link
Owner Author

Yes @kbiniek it will format the code each time before commiting. One might get an error when commiting when the code is not formated properly — you might need to commit again (see attached screenshots on VSCode). You pretty much never have to run black yourself, it’s all done with the pre-commit config automatically (after runnig make install)! The test in the github-workflow checks if the code is formatted and runs the tests

image image image

Copy link
Collaborator

@kbiniek kbiniek left a comment

Choose a reason for hiding this comment

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

very nice, thank you!! I only have one comment but thats personal taste i think so ignore and merge away if you disagree :)

@@ -20,5 +20,9 @@ jobs:
run: |
pip install poetry
poetry install
- name: Format with Black
run: |
poetry add black
Copy link
Collaborator

@kbiniek kbiniek Apr 25, 2024

Choose a reason for hiding this comment

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

poetry install installs everything we have in the pyproject.toml right? Do we want to maybe include black in the pyproject.toml directly then? Then we dont have to add it every time and can also run it inbetween commits just to format the code from time to time manually which i like to do but maybe thats just me

Copy link
Owner Author

@mlederbauer mlederbauer Apr 25, 2024

Choose a reason for hiding this comment

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

Yes that sounds good as well! From the experiences I’ve heard from: When developing a package, its dependencies should only be packages required for the src code to work, black would be an example for a “development” pckg. Butin this case it honestly shouldn’t matter; so we can add it and leave the poetry add black as a “general test workflow”, does that sound good?

Or other thing to consider form my side, which is why i would perhaps consider keeping it that way: black is “enforced” any time we commit anyways, meaning that the code that is commited already has to be black-format conform. Would it therefore be ok for you to leave it like this, and have the code be formatted each time when running git commit?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Then also the line length 79. is already custom and doens’t need to be specified when running the cmd black

@mlederbauer mlederbauer merged commit 3727216 into main Apr 25, 2024
1 check passed
@mlederbauer mlederbauer deleted the feat/black-formatter branch April 25, 2024 18:35
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