Skip to content

Commit

Permalink
Refactor linting + enable via Github workflow (#126)
Browse files Browse the repository at this point in the history
* Refactor linting + enable via Github workflow

While the two previous PRs focused on getting the tests running on public
Github runners and updating the supported Python + poetry versions, this
PR now fully focuses on code **linting** + **formatting**.

This PR:
* Delegates all the linting to pre-commit via various hooks (same tools
  as before but managed by pre-commit)
* Lets pre-commit manage the hooks -> reduces the dev dependencies in
  pyproject.toml to mypy*, pytest*, pre-commit and safety.
* Removes darglint as deprecated / discontinued
* Updates all the remaining dev dependencies + regenerates poetry.lock
* Adds a workflow to run pre-commit for PRs
* Updates Makefile targets so that `make help works
* Consolidates the many **linting** + **formatting** Makefile targets
  into just only `make pre-commit` ... the exit code + shown diff tell
  if it changed something, so that there is now no difference between
  formatting or linting... just one single make target... thats how many
  python projects operate nowadays.
* Fixes/handles all pre-commit findings

* Enable running single hook via pre-commit target
  • Loading branch information
dbast authored Feb 6, 2025
1 parent 5a96552 commit c2837c3
Show file tree
Hide file tree
Showing 14 changed files with 749 additions and 808 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Lint
on:
push:
branches:
- master
pull_request:

concurrency:
# Concurrency group that uses the workflow name and PR number if available
# or commit SHA as a fallback. If a new build is triggered under that
# concurrency group while a previous build is running it will be canceled.
# Repeated pushes to a PR will cancel all previous builds, while multiple
# merges to master will not cancel.
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true

jobs:
pre-commit:
runs-on: ubuntu-24.04
strategy:
matrix:
python-version: ['3.10']
steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install poetry
run: make poetry-download

- name: Set up cache
uses: actions/cache@v4
with:
path: |
.venv
~/.cache/pre-commit
key: venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}-${{ hashFiles('.pre-commit-config.yaml') }}

- name: Install dependencies
run: |
poetry config virtualenvs.in-project true
make install
- name: Lint
run: |
make pre-commit
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,4 @@ MigrationBackup/
testing.py

# End of https://www.gitignore.io/api/osx,python,pycharm,windows,visualstudio,visualstudiocode
examples/config.json
examples/config.json
79 changes: 49 additions & 30 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
default_language_version:
python: python3.10

default_stages: [commit, push]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
rev: v5.0.0
hooks:
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
exclude: LICENSE

- repo: local
exclude: LICENSE.md
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.1
hooks:
- id: pyupgrade
name: pyupgrade
entry: poetry run pyupgrade --py310-plus
types: [python]
language: system

- repo: local
args: [--py39-plus]
exclude: hyperliquid/utils/types.py$
- repo: https://github.com/PyCQA/isort
rev: 6.0.0
hooks:
- id: isort
name: isort
entry: poetry run isort --settings-path pyproject.toml
types: [python]
language: system

- repo: local
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.1.0
hooks:
- id: black
name: black
entry: poetry run black --config pyproject.toml
types: [python]
language: system

- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
# ignoring formatting related lints, which are handled by black
args: ['--ignore=E501,E203,W503']
- repo: https://github.com/PyCQA/bandit
rev: 1.8.2
hooks:
- id: bandit
exclude: tests/.*$
- repo: https://github.com/pylint-dev/pylint
rev: v3.3.4
hooks:
- id: pylint
exclude: examples/.*$
- repo: https://github.com/python-poetry/poetry
rev: 2.0.1
hooks:
- id: poetry-check
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.3
hooks:
- id: yamlfmt
args: [--mapping, '2', --offset, '2', --sequence, '4', --implicit_start]
files: .pre-commit-config.yaml|\.github/workflows/.*\.yml$
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.31.1
hooks:
- id: check-github-workflows
- repo: local
hooks:
- id: codestyle
name: codestyle
entry: make codestyle
types: [python]
- id: mypy
name: mypy
entry: poetry run mypy --config-file pyproject.toml ./
pass_filenames: false
language: system
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
66 changes: 18 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,74 +1,44 @@
#* Variables
# Define the shell to use when executing commands
SHELL := /usr/bin/env bash -o pipefail -o errexit

#* Lockfile handling
lockfile-update:
help:
@@grep -h '^[a-zA-Z]' $(MAKEFILE_LIST) | awk -F ':.*?## ' 'NF==2 {printf " %-22s%s\n", $$1, $$2}' | sort

lockfile-update: ## Update poetry.lock
poetry lock -n

lockfile-update-full:
lockfile-update-full: ## Fully regenerate poetry.lock
poetry lock -n --regenerate

#* Installation
install:
install: ## Install dependencies from poetry.lock
poetry install -n

install-types:
install-types: ## Find and install additional types for mypy
poetry run mypy --install-types --non-interactive ./

#* Poetry
poetry-download:
poetry-download: ## Download and install poetry
curl -sSL https://install.python-poetry.org | python -

#* Formatters
codestyle:
poetry run pyupgrade --exit-zero-even-if-changed --py39-plus **/*.py
poetry run isort --settings-path pyproject.toml ./
poetry run black --config pyproject.toml ./
lint: pre-commit ## Alias for the pre-commit target

formatting: codestyle
pre-commit: ## Run linters + formatters via pre-commit, run "make pre-commit hook=black" to run only black
poetry run pre-commit run --all-files --verbose --show-diff-on-failure --color always $(hook)

#* Linting
test:
test: ## Run tests with pytest
poetry run pytest -c pyproject.toml tests/

check-codestyle:
poetry run isort --diff --check-only --settings-path pyproject.toml ./
poetry run black --diff --check --config pyproject.toml ./
poetry run darglint --verbosity 2 hyperliquid tests

check:
poetry run mypy --config-file pyproject.toml ./

check-safety:
poetry check
check-safety: ## Run safety checks on dependencies
poetry run safety check --full-report
poetry run bandit -ll --recursive hyperliquid tests

lint: test check-codestyle mypy check-safety
update-dev-deps: ## Update development dependencies to latest versions
poetry add -D mypy@latest pre-commit@latest pytest@latest safety@latest coverage@latest pytest-cov@latest
poetry run pre-commit autoupdate

update-dev-deps:
poetry add -D bandit@latest darglint@latest "isort[colors]@latest" mypy@latest pre-commit@latest pydocstyle@latest pylint@latest pytest@latest pyupgrade@latest safety@latest coverage@latest coverage-badge@latest pytest-cov@latest
poetry add -D --allow-prereleases black@latest

#* Cleaning
pycache-remove:
cleanup: ## Cleanup project
find . | grep -E "(__pycache__|\.pyc|\.pyo$$)" | xargs rm -rf

dsstore-remove:
find . | grep -E ".DS_Store" | xargs rm -rf

mypycache-remove:
find . | grep -E ".mypy_cache" | xargs rm -rf

ipynbcheckpoints-remove:
find . | grep -E ".ipynb_checkpoints" | xargs rm -rf

pytestcache-remove:
find . | grep -E ".pytest_cache" | xargs rm -rf

build-remove:
rm -rf build/

cleanup: pycache-remove dsstore-remove mypycache-remove ipynbcheckpoints-remove pytestcache-remove

.PHONY: all $(MAKECMDGOALS)
125 changes: 12 additions & 113 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,123 +60,22 @@ make install

### Makefile usage

CLI commands for faster development.

<details>
<summary>Install all dependencies</summary>
<p>

Install requirements:

```bash
make install
```

</p>
</details>

<details>
<summary>Codestyle</summary>
<p>

Install pre-commit hooks which will run isort, black and codestyle on your code:

```bash
make pre-commit-install
```

Automatic formatting uses `pyupgrade`, `isort` and `black`.

```bash
make codestyle

# or use synonym
make formatting
```

Codestyle checks only, without rewriting files:

```bash
make check-codestyle
```

> Note: `check-codestyle` uses `isort`, `black` and `darglint` library
Update all dev libraries to the latest version using one command

```bash
make update-dev-deps
```

</p>
</details>

<details>
<summary>Tests with coverage badges</summary>
<p>

Run `pytest`

```bash
make test
```

</p>
</details>

<details>
<summary>All linters</summary>
<p>

```bash
make lint
```

the same as:

```bash
make test && make check-codestyle && make mypy && make check-safety
```

</p>
</details>

<details>
<summary>Cleanup</summary>
<p>
Delete pycache files

```bash
make pycache-remove
```

Remove package build

```bash
make build-remove
```

Delete .DS_STORE files
CLI commands for faster development. See `make help` for more details.

```bash
make dsstore-remove
check-safety Run safety checks on dependencies
cleanup Cleanup project
install Install dependencies from poetry.lock
install-types Find and install additional types for mypy
lint Alias for the pre-commit target
lockfile-update Update poetry.lock
lockfile-update-full Fully regenerate poetry.lock
poetry-download Download and install poetry
pre-commit Run linters + formatters via pre-commit, run "make pre-commit hook=black" to run only black
test Run tests with pytest
update-dev-deps Update development dependencies to latest versions
```

Remove .mypycache

```bash
make mypycache-remove
```

Or to remove all above run:

```bash
make cleanup
```

</p>
</details>

## Releases

You can see the list of available releases on the [GitHub Releases](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/releases) page.
Expand Down
1 change: 0 additions & 1 deletion api/info/candle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,3 @@ paths:
s: "BTC"
t: 1681923600000
v: "0.98639"

1 change: 0 additions & 1 deletion api/info/l2book.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,3 @@ paths:
{ "px": "20300", "sz": "3", "n": 3 }
]
]

2 changes: 1 addition & 1 deletion hyperliquid/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def sign_inner(wallet, data):


def float_to_wire(x: float) -> str:
rounded = "{:.8f}".format(x)
rounded = f"{x:.8f}"
if abs(float(rounded) - x) >= 1e-12:
raise ValueError("float_to_wire causes rounding", x)
if rounded == "-0":
Expand Down
Loading

0 comments on commit c2837c3

Please sign in to comment.