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

Blackify some files to show style differences #1186

Merged
merged 7 commits into from
Sep 4, 2019

Conversation

jfuss
Copy link
Contributor

@jfuss jfuss commented May 17, 2019

Issue #, if available:
N/A

Description of changes:
This PR is meant to show what the differences would be if we started to use Black. A good read about benefits and tradeoffs can be found under the Django dep project (dep 0008).

Happy to have a formal write up but wanted to show how this could look in our codebase through a small PR. If the team decides that this is worth while, I can work on formalize this with pre-commit hooks to ensure formatting is always done and therefore we should no longer need to care about formatting :). We could do something similar to how the attrs project is using Black.

Checklist:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jfuss jfuss requested review from sanathkr, sriram-mv and awood45 May 17, 2019 15:18
@sanathkr
Copy link
Contributor

Orange is the new black!

@sriram-mv
Copy link
Contributor

Overall I like it. 👍

@sriram-mv
Copy link
Contributor

Shall we adopt this? Given the line length is configurable, I'm okay with us taking this on.

@jfuss
Copy link
Contributor Author

jfuss commented May 23, 2019

I haven't heard a strict no yet. There is still some work to do here and we need to figure out a good time to do the flip. I am hoping to have this ready sometime early next week.

@jfuss jfuss force-pushed the adopt-black branch 5 times, most recently from 5657b67 to 11b42e8 Compare July 22, 2019 04:17
@jfuss
Copy link
Contributor Author

jfuss commented Jul 22, 2019

This should be ready to go.

PR includes:

  • the current HEAD of development in formatted
  • updated DEVELOPMENT_GUIDE with instructions on installing black and or pre-commit
  • updated Makefile with a target to run the formatter
  • travis running black --check to make sure source code is black compliant
  • Integrated with pre-commit for those who want to run git hooks with black
  • Integrated with pyproject.toml to hold our black config. More info in PEP518

@jfuss jfuss marked this pull request as ready for review July 22, 2019 05:00
exclude = '''

(
/(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also exclude init template directories?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea. sure can do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have excluded the templates used for sam init. I will un-format them once, I do the update from develop and rerun black dance.

Makefile Outdated
# Verifications to run before sending a pull request
pr: init dev

Copy link
Contributor

Choose a reason for hiding this comment

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

remove empty lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Makefile Outdated
@@ -26,5 +26,10 @@ lint:
# Command to run everytime you make changes to verify everything works
dev: flake lint test

black-format:
Copy link
Contributor

Choose a reason for hiding this comment

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

call it just black?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure


[tool.black]
line-length = 120
target_version = ['py37', 'py27', 'py36']
Copy link
Contributor

Choose a reason for hiding this comment

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

remove py27?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept it because we still have py27 support :(. So wasn't comfortable making formatting related this and risk breaking it. I just quickly ran it and the only changes were removing u from strings like u"help". For now, I would prefer to just leave it.

.travis.yml Outdated

install:
# Install the code requirements
- make init

script:
# Verify code is black compliant
- /tmp/black --check setup.py tests samcli
Copy link
Contributor

Choose a reason for hiding this comment

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

make black instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make black will format the files are write it back to the filesystem. Instead this commands black --check makes sure that the files are formatted and if not the build will fail. This ensures our codebase is always formatted. I can create a make black-check? command or something. I thought about putting it in make pr but didn't want to go through the hassle of pip installing on appveyor too (you can only [pip] install black in 3.6 or above).

@sriram-mv
Copy link
Contributor

I wonder if we should run make black on a github post hook or something and completely forget about formatting. it gets auto taken care of.

@viksrivat
Copy link
Contributor

If we run it as a github post hook, do we still need to run make lint/flake ?

@jfuss
Copy link
Contributor Author

jfuss commented Aug 7, 2019

| I wonder if we should run make black on a github post hook or something and completely forget about formatting. it gets auto taken care of.

@thesriram That is what the pre-commit installation instructions are for. I didn't want to make it required. I don't like pip installing other tools, because it can mess with the global or user installation on a system. So I made this optional but travis does run a check to ensure the files are 'blackified', so we should never be checking in code that is not formatted.

| If we run it as a github post hook, do we still need to run make lint/flake ?

@viksrivat I think we can remove flake8, but would like to keep pylint around until we know black handles everything.

### 2. Install Additional Tooling
#### Black
We format our code using [Black](https://github.com/python/black) and verify the source code is black compliant
in Travis during PRs. You can find installation instructions on [Black's docs](https://black.readthedocs.io/en/stable/installation_and_usage.html).
Copy link
Contributor

Choose a reason for hiding this comment

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

Appveyor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

template,
env_vars,
debug_port,
debug_args,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why these random empty lines show up here, github formatting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see any empty lines in the diff. Must be github?


# Verifications to run before sending a pull request
pr: init dev
pr: init dev
Copy link
Contributor

Choose a reason for hiding this comment

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

Why isn't black run as part of make pr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the make black command requires you to commit the changes. So I didn't want the make pr to run that and make it look like it works. I can add another make command that does the check instead, which is probably what we want anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you install the git hooks, running the check on pr is just redundant. This is because you can't push unless black passes.

Copy link
Contributor

@sanathkr sanathkr left a comment

Choose a reason for hiding this comment

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

LGTM as long as CI passes ⬛️🏴

@sanathkr sanathkr merged commit a83aa9e into aws:develop Sep 4, 2019
@jfuss jfuss deleted the adopt-black branch September 4, 2019 16:33
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.

4 participants