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

Running the black code formatter on the codebase #241

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

tomage
Copy link

@tomage tomage commented May 21, 2022

EDIT: This is really a tiny PR - should take about 5 mins to review I promise :)


Right.. This is a big one! But also, a small one.

It's big because it changes a lot of files, and it may introduce coding styles foreign to some developers.

It's small because functionally, the code is exactly the same. Black is written by one of the core contributors to python, and he did a clever thing with black: It compares the AST (abstract syntax tree) of the code before and after, and makes sure it's the same. This pretty much makes sure that the code functions the same. Hence, this patch is actually small.

**The only files that need any reviewing here are:

  • Makefile: Added format target
  • pyproject.toml: New file to configure black
  • requirements.txt: Adds black dependency
  • requirements.txt.freeze: Freeze after adding black (some other 3rd party deps added for black)
  • initial_setup.py: Properly converted to python 3 (print() calls) and ran black on it, as all other .py files.

pyproject.toml is nice. It can be used to configure other aspects of our python projects later on. Read more about it here: https://peps.python.org/pep-0621/ and here: https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/.

All of the other files are .py files changed by black, and thus need no reviewing, unless if the reader is curious how black is formatting the files.

@tomage
Copy link
Author

tomage commented May 21, 2022

To add: I do want to put in some pre-push git hook to prevent the code from being de-blackified, but I've also found that it's good to do this a bit after blackifying the codebase, so as to not stir things up too much.

Once (if?) we do add such a hook (for example via the pre-commit.com mechanism), there might be some files that will have gone out-of-black format, but it's no big deal to blackify them along with adding the hook.

@tomage tomage marked this pull request as draft May 29, 2022 23:15
@tomage tomage force-pushed the format-with-black branch 2 times, most recently from 419d539 to f5b8551 Compare May 31, 2022 05:03
Makefile Outdated
@@ -82,6 +82,11 @@ update-dependencies:
@make freeze-dependencies


.PHONY: format
format: setup
@. .venv/bin/activate && black .
Copy link
Author

Choose a reason for hiding this comment

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

Decided to call the target format, and not black, as there might be other formatting tools we might want to add (JS/HTML/CSS..?).

@@ -62,19 +66,34 @@ def get_answer(question, proper_answers=('yes','no')):
return answer


print "*" * TERMINAL_WIDTH
Copy link
Author

Choose a reason for hiding this comment

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

I converted these python 2 style print statements into calls to the print() function in python 3.

line-length = 79
# NOTE: PEP8 does recommend double-quotes, but to start with we'll support
# single quotes as to minimize the patch/noise produced by introducing black
skip-string-normalization = true
Copy link
Author

Choose a reason for hiding this comment

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

I decided to go for 79 chars, as it's by the PEP8 standard.
Also, I figured - for now - to allow using single quote strings. We can change it later if we want to.

@@ -28,6 +28,9 @@ requests==2.22.0
signxml==2.7.2
suds-jurko==0.6

# Development
black==22.3.0

Copy link
Author

Choose a reason for hiding this comment

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

Adding black.

cachetools==4.0.0
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
click==8.1.3
Copy link
Author

Choose a reason for hiding this comment

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

This, and the other dependencies below came in with the black package.

@tomage tomage marked this pull request as ready for review May 31, 2022 05:12
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.

1 participant