Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active August 1, 2022 18:56
Show Gist options
  • Save fernandojunior/1a8868bc651d6d45ffa6fed0c1b9ffb1 to your computer and use it in GitHub Desktop.
Save fernandojunior/1a8868bc651d6d45ffa6fed0c1b9ffb1 to your computer and use it in GitHub Desktop.
Baby steps to use flake8 and black

I suggest you to use some lint tools like flake8 to find some PEP8 warnings. Optionally, you can use flake8-black to automatically find and fix them. This way, you will have a more consistent and unified code style.

  • Add flake8 and flake8-black (optional) as a dependency to requirements.txt and test_requirements.txt
  • Create a .flake8 config file in project's root folder ROOT_FOLDER/.flake8 to setup some global flake8 configurations to customize or ignore some warnings/violations (see more here and here).

Example:

# .flake8 file
[flake8]
ignore = D203, E501, W504, W503, E731, E712
exclude =
    # No need to traverse our git directory
    .git,
    # There's no value in checking cache directories
    __pycache__,
    # The conf file is mostly autogenerated, ignore it
    docs/source/conf.py,
    *.ipynb_checkpoints*/
    # This contains builds of flake8 that we don't want to check
    dist
 max-line-length = 111
  • Create an entry in Makefile to automate lint inspection by running the make lint on a cmd line terminal:

Example:

# makefile file
lint:
    @flake8 scripts/ tests/
  • Optional: To find lint warnings/violations into Drone Pipeline (CI/CD) on a Pull Request, change ./validation/custom_validation.sh to:
# custom_validation.sh file	
flake8 ./scripts/

Optional: If you want to automatically find and refactoty code warnings/violations in your local machine:

  • Install black for flake8: pip install flake8-black (terminal)
  • Run black PATH/TO/SOURCE in a terminal to reformat the code based on PEP8 constraints.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment