diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9c712cf..a06fc0d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -5,7 +5,7 @@ on: [push] jobs: black: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - uses: psf/black@stable @@ -13,14 +13,30 @@ jobs: options: "--check --verbose" version: "23.3.0" + isort: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: 'pip' # caching pip dependencies + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install .[dev] + - name : Check import order + run : isort . -c + flake8: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - - name: Set up Python 3.8 + - name: Set up Python 3.10 uses: actions/setup-python@v4 with: - python-version: "3.8" + python-version: "3.10" cache: 'pip' # caching pip dependencies - name: Install dependencies run: | @@ -31,16 +47,16 @@ jobs: # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # option --exit-zero can be added to this line to set these errors as warnings - flake8 . --count --max-complexity=10 --max-line-length=128 --statistics + flake8 . --count --statistics --config setup.cfg mypy: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - - name: Set up Python 3.8 + - name: Set up Python 3.10 uses: actions/setup-python@v4 with: - python-version: "3.8" + python-version: "3.10" cache: 'pip' # caching pip dependencies - name: Install dependencies run: | diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 5461baf..82ca2af 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -5,14 +5,14 @@ on: [pull_request] jobs: tests: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - - name: Set up Python 3.8 + - name: Set up Python 3.10 uses: actions/setup-python@v4 with: - python-version: "3.8" + python-version: "3.10" cache: 'pip' # caching pip dependencies - name: Install dependencies run: | diff --git a/README.md b/README.md index c820e91..2c9826b 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,35 @@ Template code and examples for python project respecting the [PEP8 coding style] [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) ![linter](https://github.com/pollen-robotics/python-coding-guidelines/actions/workflows/lint.yml/badge.svg) ![pytest](https://github.com/pollen-robotics/python-coding-guidelines/actions/workflows/pytest.yml/badge.svg) ![coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/FabienDanieau/58642e8fe4589e710e26627e39ff92d7/raw/covbadge.json) +## Quick start + +List of steps to follow to adapt the template to your project. + +1. Make your repo with "Use this template" button +2. Clone the new repo +3. [Recommended] Make a virtual environment +4. [Optional] Set up [git lfs](#git) and *.gitattributes* +4. Activate pre-commit hook +```console +mv scripts/git_hooks/pre-commit .git/hooks/ +``` +5. Install dev tools +```console +pip install -e .[dev] +``` +6. [Configure](#ide---linting) Visual Code with the linters +7. [Adapt](#unit-tests-and-test-coverage) workflow and bagdes links to this repo + ## Configuration -### Installation +## Installation + +Dependencies are detailed in the `setup.cfg` file. To install this package locally, run: +``` +pip install -e .[dev] +``` +Include *[dev]* for optional development tools. The dependencies are listed in the ```setup.cfg``` file and will be installed if you install this package locally with: ``` @@ -18,28 +43,37 @@ use *[dev]* for optional development tools. Once this is done, you should be able to import the Python package anywhere on your system with: -``` +```console import example ``` +An exemple executable is also installed +```console +example_entry_point +``` + ### git -A *.gitignore* file makes sure that the Python temporary files are not committed. It is adapted from [here](https://github.com/github/gitignore/blob/main/Python.gitignore). +A *.gitignore* file ensures that the Python temporary files are not committed. It is adapted from [here](https://github.com/github/gitignore/blob/main/Python.gitignore). -git LFS could be configured to handle all non script files (3D models, deep learning model, images, etc.). The list of file is defined in *gitattributes_example.* If you want to use LFS rename this file *.gitattributes*. Then run ```git lfs install```. Also, a good practice is to use another repo as a submodule containing all the data. +[git LFS](https://git-lfs.com/) can be configured to manage all non-script files (3D models, deep learning models, images, etc.). The list of files is defined in *gitattributes_example*. If you wish to utilize LFS, rename this file to *.gitattributes* and then run ```git lfs install```. It's also a good practice to maintain another repository as a submodule containing all the data. + +A git hook can be set up to automatically check for PEP8 compliance before committing. Refer to *scripts/git_hooks*. Installing this is recommended as the GitHub workflows will perform the same checks. -A git hook can be installed to automatically checks PEP8 compliance before a commit. See *scripts/git_hooks*. ### IDE - Linting -Visual code is the recommended IDE. Make sure to install the python extension and [configure](https://dev.to/adamlombard/how-to-use-the-black-python-code-formatter-in-vscode-3lo0) VS code to automatically format the code with [black](https://black.readthedocs.io). +Visual Studio Code is the recommended IDE. Ensure you have the Python extension installed and [configure](https://dev.to/adamlombard/how-to-use-the-black-python-code-formatter-in-vscode-3lo0) VS Code to auto-format your code using [black](https://black.readthedocs.io). + +[isort](https://pycqa.github.io/isort/) verifies the order of Python imports. [Set up](https://github.com/microsoft/vscode-isort#import-sorting-on-save) VS Code to handle this automatically. -[Flake8](https://flake8.pycqa.org) will perform complementary checks. Select *flake8* as the [linter for VS Code](https://code.visualstudio.com/docs/python/linting). Errors should be directly indicated within the code. +[Flake8](https://flake8.pycqa.org) will conduct additional checks. Choose **flake8** as the [linter for VS Code](https://code.visualstudio.com/docs/python/linting). Errors will be directly highlighted within the code. -Finaly [mypy](https://mypy.readthedocs.io/en/stable/index.html) will statically check type errors. It runs in *strict* mode in this example. Feel free to [release the constraints](https://mypy.readthedocs.io/en/stable/getting_started.html?highlight=strict#strict-mode-and-configuration) if it is not suitable for your project. +Lastly, [mypy](https://mypy.readthedocs.io/en/stable/index.html) will perform static type checking. In this guide, it's set to run in **strict** mode. Feel free to [adjust the constraints](https://mypy.readthedocs.io/en/stable/getting_started.html?highlight=strict#strict-mode-and-configuration) if they don't align with your project's needs. + +These tools are configured in the **setup.cfg** file. Their versions are predefined to prevent discrepancies between local and remote checks. It's advisable to keep them updated. -These tools are configured in *setup.cfg*. The version of these tools is pre-defined in order to avoid errors between local and remote checks. It is a good practive to keep them up to date. **A code not compliant with PEP8 guidelines will not be merged.** @@ -51,7 +85,8 @@ The git workflow is based on [gitflow](https://www.atlassian.com/git/tutorials/c ![git workflow](https://wac-cdn.atlassian.com/dam/jcr:34c86360-8dea-4be4-92f7-6597d4d5bfae/02%20Feature%20branches.svg?cdnVersion=805) -The default branch is **develop** which is the working branch. **main** is dedicated to stable code (release) only. Both of them are protected, no one should push directly into them. Any new development **MUST** be done in a feature branch, forked from develop, and then merged back to develop through a pull request. When a code considered to be stable, develop can be also merged to main with a pull request. +The default branch is **develop**, which serves as the working branch. **main** is reserved solely for stable code (releases). Both of these branches are protected, meaning direct pushes are disallowed. All new development **MUST** commence in a feature branch, derived from develop, and then be merged back into develop via a pull request. Once the code is deemed stable, the develop branch can also be merged into main through another pull request. + ### Issues diff --git a/scripts/git_hooks/pre-commit b/scripts/git_hooks/pre-commit deleted file mode 100755 index 6192b96..0000000 --- a/scripts/git_hooks/pre-commit +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash -# -# Call black and flake before each commit. -# The command "git commit" with no argument will trigger this script. -# -# To enable this hook, cp this file to .git/hooks/. - -# If any command fails, exit immediately with that command's exit status -set -eo pipefail - -# Run black against all code in the `source_code` directory -black . --check -echo "-------> Black passed!" - -# Run flake8 against all code in the `source_code` directory -flake8 . -echo "-------> Flake8 passed!" - -# Run mypy against all code in the `source_code` directory -mypy . -echo "-------> Mypy passed!" - diff --git a/setup.cfg b/setup.cfg index 7e6ad55..25c86b3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,11 +22,12 @@ install_requires = where=src [options.extras_require] -dev = black==23.3.0 - flake8==6.0.0 - pytest==7.3.1 - coverage==7.2.5 - mypy==1.0.0 +dev = black==23.10.1 + flake8==6.1.0 + pytest==7.4.3 + coverage==7.3.2 + mypy==1.6.1 + isort==5.12.0 [options.entry_points] console_scripts = diff --git a/src/example/xterrabot.py b/src/example/xterrabot.py index 5a97db2..4fb022e 100644 --- a/src/example/xterrabot.py +++ b/src/example/xterrabot.py @@ -1,4 +1,5 @@ import logging + import numpy as np import numpy.typing as npt diff --git a/src/main.py b/src/main.py index 220eb6b..53b190c 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,9 @@ -import sys -import logging import argparse +import logging +import sys -from example.foo import Foo from example.celcius import Celsius +from example.foo import Foo from example.xterrabot import XTerraBot diff --git a/tests/test_xterrabot.py b/tests/test_xterrabot.py index 05962d1..415080a 100644 --- a/tests/test_xterrabot.py +++ b/tests/test_xterrabot.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest from src.example.xterrabot import XTerraBot