|
| 1 | +# Code Style and Structure |
| 2 | + |
| 3 | +pyOpenSci suggests that you follow the [python PEP8 style guide](https://peps.python.org/pep-0008/) for decisions |
| 4 | +related to styling your open source Python packages code. |
| 5 | + |
| 6 | +We also suggest that you consider: |
| 7 | +1. adhering to a commonly used docstring format. If you use xx or xx then it will further support easy API documentation using tools such as Sphinx autodoc. |
| 8 | +2. Adding a linter to your development build that will catch style issues and tell you how to reformat your code as needed. (see below for more ) |
| 9 | +3. Add a code styler such as black or blue to keep the entire package consistent. |
| 10 | + |
| 11 | +## Docstring styles |
| 12 | + |
| 13 | +```{note} |
| 14 | +what is a docstring... |
| 15 | +https://peps.python.org/pep-0257/#what-is-a-docstring |
| 16 | +``` |
| 17 | + |
| 18 | +* https://numpydoc.readthedocs.io/en/latest/ |
| 19 | +* Ask on slack this week what resources people like for this |
| 20 | + |
| 21 | +## Code linters |
| 22 | + |
| 23 | +Code linters are tools that look at your code and identify problems such as: |
| 24 | + |
| 25 | +* unused variables |
| 26 | +* invalide styles |
| 27 | +* missing docstrings |
| 28 | +* incorrect spacing |
| 29 | + |
| 30 | +and more |
| 31 | + |
| 32 | +Linters can be run as a command-line tool and/or can also be setup as a part of |
| 33 | +your favorite programming tool (e.g. VScode, pycharm, etc). |
| 34 | + |
| 35 | +Some maintainers stup a pre-commit hook in their reporisoty. A hook will check |
| 36 | +your code prior to your committing it. IT will itendify issues and if the |
| 37 | +hook includes a styler, will fix any issues with code style before you commit. |
| 38 | + |
| 39 | +This type of setup can be helpful to ensure code consistency associated with new |
| 40 | +contributions... |
| 41 | + |
| 42 | + |
| 43 | +Regardless of how you set this up, We suggest setting up a linter and a code styler |
| 44 | +as they will give you automatic feedback about your code's structure as you (or a contributor) write it. |
| 45 | + |
| 46 | +## Some popular linters |
| 47 | + |
| 48 | +* A very popular code linter for Python is [flake8](https://flake8.pycqa.org/en/latest/). |
| 49 | +`Flake8` checks if the code is according to [PEP 8](https://www.python.org/dev/peps/pep-0008/), |
| 50 | +the Python Enhancement Proposal about `Style Guide for Python Code`. |
| 51 | + |
| 52 | + |
| 53 | +See also: |
| 54 | + |
| 55 | +- [mypy](http://mypy-lang.org/) |
| 56 | +- [numpydoc](https://numpydoc.readthedocs.io/en/latest/) |
| 57 | +- [pydocstyle](https://github.com/PyCQA/pydocstyle) |
| 58 | + |
| 59 | + |
| 60 | +## Code stylers |
| 61 | + |
| 62 | +Code stylers are tools that fix styling issues in a file, formatting it automatically. |
| 63 | +Code *styling* is different from code *linting* because it doesn't change the functionality |
| 64 | +of your code at all, it *only* changes how it looks. |
| 65 | +Using an automatic formatting tool helps to keep the source code within specification |
| 66 | +and also helps review workflow. While stylers might cause your code to look differently |
| 67 | +than you would have chosen, many projects have adopted them in order to have a single |
| 68 | +code style that is consistent across projects. |
| 69 | + |
| 70 | +Currently, [black](https://github.com/psf/black) is the most popular code styler for Python. |
| 71 | +It will format the code according the `PEP 8` and should work fine with `flake8` (maybe it needs |
| 72 | +some extra configuration as, for example, `line-length`). |
| 73 | + |
| 74 | +See also: |
| 75 | + |
| 76 | +- [isort](https://github.com/timothycrosley/isort) |
| 77 | + |
| 78 | + |
| 79 | +## Git pre-commit hook |
| 80 | + |
| 81 | +Git pre-commit hook is a useful tool that checks your code automatically when you run a `git commit` and, |
| 82 | +if it fails, the `git commit` is canceled. This is often used to make sure |
| 83 | +that the changes to your code match a particular style, or that there are no |
| 84 | +code linting errors. |
| 85 | + |
| 86 | +For example, if you want that `git commit` checks if your code matches the PEP8 specification, |
| 87 | +you can configure a git flake8 pre-commit hook: |
| 88 | + |
| 89 | + |
| 90 | +```yaml |
| 91 | +# file: .pre-commit-config.yaml |
| 92 | +repos: |
| 93 | + - repo: https://gitlab.com/pycqa/flake8 |
| 94 | + rev: '3.7.9' |
| 95 | + hooks: |
| 96 | + - id: flake8 |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +```{note} |
| 101 | +See [the flake8 hooks explanation](https://flake8.pycqa.org/en/latest/user/using-hooks.html) for more details. |
| 102 | +``` |
| 103 | + |
| 104 | +This file specifies a hook that will be triggered automatically before each `git commit`, |
| 105 | +in this case, it specifies a flake8 using version `3.7.9`. |
| 106 | + |
| 107 | +Before you can see any change, first you should install `pre-commit` library. |
| 108 | +One way to do it is using `pip` or `conda`: |
| 109 | + |
| 110 | +```sh |
| 111 | +pip install pre-commit |
| 112 | + |
| 113 | +# or |
| 114 | + |
| 115 | +conda install -c conda-forge pre-commit |
| 116 | +``` |
| 117 | + |
| 118 | +Now, you can install your pre-commit hook using `pre-commit install`, and it will create the hook based on |
| 119 | +the file `.pre-commit-config.yaml`. |
| 120 | + |
| 121 | +Before each `git commit` command, in this case, git will run `flake8` and, if it fails, the `commit` will be canceled. |
| 122 | + |
| 123 | + |
| 124 | +## What git pre-commit hook should I use? |
| 125 | + |
| 126 | +The git pre-commit hook you should use, depends on the project needs or the team needs. |
| 127 | +Some pre-commit hooks you can find useful would be: |
| 128 | + |
| 129 | +- [flake8](https://flake8.pycqa.org/en/latest/user/using-hooks.html) |
| 130 | +- [mypy](https://github.com/pre-commit/mirrors-mypy) |
| 131 | +- [black](https://black.readthedocs.io/en/stable/integrations/source_version_control.html) |
| 132 | +- [isort](https://github.com/pre-commit/mirrors-isort) |
| 133 | + |
| 134 | +If you want more information about `pre-commit`, check out its [documentation](https://pre-commit.com/). |
0 commit comments