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

Added pre commit support. #18

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
default_stages: [commit]
default_language_version:
python: python3.9
repos:
- repo: local
hooks:
- id: isort
stages: [commit]
name: isort
entry: bash -c 'poetry run isort --check .'
language: system
types: [python]
- id: flake8
stages: [commit]
name: flake8
entry: bash -c 'poetry run flake8 .'
language: system
- id: mypy
stages: [commit]
name: mypy
entry: bash -c 'poetry run mypy .'
language: system
- id: pytest
stages: [commit]
name: pytest
entry: bash -c 'poetry run pytest'
language: system
81 changes: 58 additions & 23 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
- [Forking and Cloning](#forking-and-cloning)
- [Building](#building)
- [Install Prerequisites](#install-prerequisites)
- [Pyenv](#pyenv)
- [Python 3.9](#python-39)
- [Poetry](#poetry)
- [Building and Testing](#building-and-testing)
- [Install Pyenv](#install-pyenv)
- [Install Python 3.9](#install-python-39)
- [Install Poetry](#install-poetry)
- [Install Dependencies](#install-dependencies)
- [Run Tests](#run-tests)
- [Cleanup Code](#cleanup-code)
- [Install Pre Commit](#install-pre-commit)
- [Developing](#developing)
- [Code Linting](#code-linting)
- [Type Checking](#type-checking)
- [License Headers](#license-headers)
- [Visual Studio Code](#visual-studio-code)
- [License Headers](#license-headers)
- [Visual Studio Code](#visual-studio-code)
- [Transport Protocol](#transport-protocol)
- [Overview](#overview)
- [REST Handlers](#rest-handlers)
Expand All @@ -20,21 +21,17 @@

Fork this repository on GitHub, and clone locally with `git clone`.

## Building
## Building and Testing

This project contains a collection of tools to build, test and release `opensearch_sdk_py`.

### Install Prerequisites

#### Pyenv
### Install Pyenv

Use pyenv to manage multiple versions of Python. This can be installed with [pyenv-installer](https://github.com/pyenv/pyenv-installer) on Linux and MacOS, and [pyenv-win](https://github.com/pyenv-win/pyenv-win#installation) on Windows.

```
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
```

#### Python 3.9
### Install Python 3.9

Python projects in this repository use Python 3.9. See the [Python Beginners Guide](https://wiki.python.org/moin/BeginnersGuide) if you have never worked with the language.

Expand All @@ -50,7 +47,7 @@ pyenv install 3.9
pyenv global 3.9
```

#### Poetry
### Install Poetry

This project uses [poetry](https://python-poetry.org/), which is typically installed with `curl -sSL https://install.python-poetry.org | python3 -`. Poetry automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your `pyproject.toml` as you install/uninstall packages. It also generates the ever-important `poetry.lock`, which is used to produce deterministic builds.

Expand All @@ -69,29 +66,67 @@ poetry install

### Run Tests

Run tests and ensure they pass.

```
poetry run pytest -v
```

### Cleanup Code
### Install Pre Commit

This project uses a [pre-commit hook](https://pre-commit.com/) for linting Python code which is then checked in the [lint workflow](.github/workflows/lint.ml).
.

```
$ poetry run pre-commit install
$ poetry run pre-commit run --all-files
```

Pre-commit hook will run `isort`, `flake8`, `mypy` and `pytest` before making a commit.

## Developing

The [lint workflow](.github/workflows/lint.ml) runs `flake8`. Make sure `poetry run flake8 .` is clean before making pull requests. Use the following to auto-clean and auto-format.
### Code Linting

This project uses a set of tools, such as [isort](https://github.com/PyCQA/isort) to ensure that imports are sorted, and [flake8](https://flake8.pycqa.org/en/latest/) to enforce code style.

```
poetry run isort .
poetry run black .
poetry run autoflake -r --in-place --remove-unused-variables --remove-all-unused-imports .
$ poetry run flake8
./src/assemble_workflow/bundle_recorder.py:30:13: W503 line break before binary operator
```

Use `isort .` to fix any sorting order.

```
$ poetry run isort .
Fixing tests/system/test_arch.py
```

Use [black](https://black.readthedocs.io/en/stable/) to auto-format your code.

```
$ poetry run black .
All done! ✨ 🍰 ✨
23 files left unchanged.
```

You can run a combination of these tools by installing [poetry-exec-plugin](https://github.com/keattang/poetry-exec-plugin) once.

```
poetry self add poetry-exec-plugin
```

Then use `poetry exec auto`.

### Type Checking

This project uses [mypy](https://github.com/python/mypy) as an optional static type checker. Make sure that `poetry run mypy .` is clean before making pull requests.

## License Headers
### License Headers

All python code has a copy of the [license header](LICENSE_HEADER.txt) on top of it. To bulk apply license headers use `poetry run licenseheaders -t LICENSE_HEADER.txt -E .py`.

## Visual Studio Code
### Visual Studio Code

After opening Visual Studio Code, use `> Pythin: Select Interpreter` (also in the bottom right of VSCode when you edit a Python file) to properly resolve import paths.

Expand Down
Loading