OpenDataServices / developer-docs

Documentation about how we get our work done. For internal use, but happy to share
Other
1 stars 0 forks source link

Add Guide for Black and isort our repos #80

Closed odscjames closed 4 years ago

odscjames commented 4 years ago

So far i have the below:

1) Add black and isort to dev requirements and upgrade any locked requirements

NOTE black will only work on Python 3.6 up! So you may need to do something clever to install it on those ones only, or you may need to just not add it at this stage.

For example, here's how to do it in setup.py:

extras_require_test = ["pytest", "flake8", "isort"]

if sys.version_info[0] >= 3 and sys.version_info[1] >= 6:
    extras_require_test.append("black==19.10b0")

Lock black to version 19.10b0 (latest version at time of writing). If there is now a later version, it's ok to lock to that.

This is so that new major versions with breaking backwards changes don't unexpectedly cause issues in pull requests. We lock to an exact version rather than 19.* to avoid a Travis bug we saw.

2) Add .isort.cfg

Contents are: https://raw.githubusercontent.com/OpenDataServices/lib-cove/master/.isort.cfg

Add to Git

3) Add/Update setup.cfg

Contents are: https://raw.githubusercontent.com/OpenDataServices/lib-cove/master/setup.cfg

Add to Git

3) Run locally

    black  *.py */
    isort  --recursive *.py */

4) Add to Travis install section

If you didn't find a way to install Black above, you need to add something like:

# Black only runs under Python >= 3.6
  - if [[ $TRAVIS_PYTHON_VERSION != 3.5 ]]; then pip install black==19.*; fi

5) Add to Travis test section

  - if [[ $TRAVIS_PYTHON_VERSION != 3.5 ]] && [[ $TRAVIS_PYTHON_VERSION != 3.4 ]]; then black --check *.py */; fi
  - isort --check-only --recursive *.py */
  - flake8

6) Git commit into one large commit

7) Amend Author

     git commit --amend --author="Automated Reformatting <code+reformatting@opendataservices.coop>"

8) push - Travis should pass!

9) Test by making a new branch, and editing a formatting thing - black should fail!

10) Test by making a new branch, and reordering some imports - isort should fail!

odscjames commented 4 years ago

Start using Black & iSort on all our code

odscjames commented 4 years ago

https://opendataservices.gitbook.io/developer-docs/how-to-guides/add-black-isort-existing-repo