Skip to content

Commit c2837c3

Browse files
authored
Refactor linting + enable via Github workflow (#126)
* 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
1 parent 5a96552 commit c2837c3

File tree

14 files changed

+749
-808
lines changed

14 files changed

+749
-808
lines changed

.github/workflows/lint.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
8+
concurrency:
9+
# Concurrency group that uses the workflow name and PR number if available
10+
# or commit SHA as a fallback. If a new build is triggered under that
11+
# concurrency group while a previous build is running it will be canceled.
12+
# Repeated pushes to a PR will cancel all previous builds, while multiple
13+
# merges to master will not cancel.
14+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
pre-commit:
19+
runs-on: ubuntu-24.04
20+
strategy:
21+
matrix:
22+
python-version: ['3.10']
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
- name: Set up Python ${{ matrix.python-version }}
27+
uses: actions/setup-python@v5
28+
with:
29+
python-version: ${{ matrix.python-version }}
30+
31+
- name: Install poetry
32+
run: make poetry-download
33+
34+
- name: Set up cache
35+
uses: actions/cache@v4
36+
with:
37+
path: |
38+
.venv
39+
~/.cache/pre-commit
40+
key: venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}-${{ hashFiles('.pre-commit-config.yaml') }}
41+
42+
- name: Install dependencies
43+
run: |
44+
poetry config virtualenvs.in-project true
45+
make install
46+
47+
- name: Lint
48+
run: |
49+
make pre-commit

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,4 @@ MigrationBackup/
618618
testing.py
619619

620620
# End of https://www.gitignore.io/api/osx,python,pycharm,windows,visualstudio,visualstudiocode
621-
examples/config.json
621+
examples/config.json

.pre-commit-config.yaml

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
1-
default_language_version:
2-
python: python3.10
3-
4-
default_stages: [commit, push]
5-
61
repos:
72
- repo: https://github.com/pre-commit/pre-commit-hooks
8-
rev: v2.5.0
3+
rev: v5.0.0
94
hooks:
5+
- id: check-toml
106
- id: check-yaml
117
- id: end-of-file-fixer
12-
exclude: LICENSE
13-
14-
- repo: local
8+
exclude: LICENSE.md
9+
- repo: https://github.com/asottile/pyupgrade
10+
rev: v3.19.1
1511
hooks:
1612
- id: pyupgrade
17-
name: pyupgrade
18-
entry: poetry run pyupgrade --py310-plus
19-
types: [python]
20-
language: system
21-
22-
- repo: local
13+
args: [--py39-plus]
14+
exclude: hyperliquid/utils/types.py$
15+
- repo: https://github.com/PyCQA/isort
16+
rev: 6.0.0
2317
hooks:
2418
- id: isort
25-
name: isort
26-
entry: poetry run isort --settings-path pyproject.toml
27-
types: [python]
28-
language: system
29-
30-
- repo: local
19+
- repo: https://github.com/psf/black-pre-commit-mirror
20+
rev: 25.1.0
3121
hooks:
3222
- id: black
33-
name: black
34-
entry: poetry run black --config pyproject.toml
35-
types: [python]
36-
language: system
37-
23+
- repo: https://github.com/PyCQA/flake8
24+
rev: 7.1.1
25+
hooks:
26+
- id: flake8
27+
# ignoring formatting related lints, which are handled by black
28+
args: ['--ignore=E501,E203,W503']
29+
- repo: https://github.com/PyCQA/bandit
30+
rev: 1.8.2
31+
hooks:
32+
- id: bandit
33+
exclude: tests/.*$
34+
- repo: https://github.com/pylint-dev/pylint
35+
rev: v3.3.4
36+
hooks:
37+
- id: pylint
38+
exclude: examples/.*$
39+
- repo: https://github.com/python-poetry/poetry
40+
rev: 2.0.1
41+
hooks:
42+
- id: poetry-check
43+
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
44+
rev: 0.2.3
45+
hooks:
46+
- id: yamlfmt
47+
args: [--mapping, '2', --offset, '2', --sequence, '4', --implicit_start]
48+
files: .pre-commit-config.yaml|\.github/workflows/.*\.yml$
49+
- repo: https://github.com/python-jsonschema/check-jsonschema
50+
rev: 0.31.1
51+
hooks:
52+
- id: check-github-workflows
3853
- repo: local
3954
hooks:
40-
- id: codestyle
41-
name: codestyle
42-
entry: make codestyle
43-
types: [python]
55+
- id: mypy
56+
name: mypy
57+
entry: poetry run mypy --config-file pyproject.toml ./
58+
pass_filenames: false
4459
language: system
60+
- repo: meta
61+
hooks:
62+
- id: check-hooks-apply
63+
- id: check-useless-excludes

Makefile

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,44 @@
1-
#* Variables
1+
# Define the shell to use when executing commands
22
SHELL := /usr/bin/env bash -o pipefail -o errexit
33

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

8-
lockfile-update-full:
10+
lockfile-update-full: ## Fully regenerate poetry.lock
911
poetry lock -n --regenerate
1012

11-
#* Installation
12-
install:
13+
install: ## Install dependencies from poetry.lock
1314
poetry install -n
1415

15-
install-types:
16+
install-types: ## Find and install additional types for mypy
1617
poetry run mypy --install-types --non-interactive ./
1718

18-
#* Poetry
19-
poetry-download:
19+
poetry-download: ## Download and install poetry
2020
curl -sSL https://install.python-poetry.org | python -
2121

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

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

30-
#* Linting
31-
test:
27+
test: ## Run tests with pytest
3228
poetry run pytest -c pyproject.toml tests/
3329

34-
check-codestyle:
35-
poetry run isort --diff --check-only --settings-path pyproject.toml ./
36-
poetry run black --diff --check --config pyproject.toml ./
37-
poetry run darglint --verbosity 2 hyperliquid tests
38-
39-
check:
40-
poetry run mypy --config-file pyproject.toml ./
41-
42-
check-safety:
43-
poetry check
30+
check-safety: ## Run safety checks on dependencies
4431
poetry run safety check --full-report
45-
poetry run bandit -ll --recursive hyperliquid tests
4632

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

49-
update-dev-deps:
50-
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
51-
poetry add -D --allow-prereleases black@latest
52-
53-
#* Cleaning
54-
pycache-remove:
37+
cleanup: ## Cleanup project
5538
find . | grep -E "(__pycache__|\.pyc|\.pyo$$)" | xargs rm -rf
56-
57-
dsstore-remove:
5839
find . | grep -E ".DS_Store" | xargs rm -rf
59-
60-
mypycache-remove:
6140
find . | grep -E ".mypy_cache" | xargs rm -rf
62-
63-
ipynbcheckpoints-remove:
64-
find . | grep -E ".ipynb_checkpoints" | xargs rm -rf
65-
66-
pytestcache-remove:
6741
find . | grep -E ".pytest_cache" | xargs rm -rf
68-
69-
build-remove:
7042
rm -rf build/
7143

72-
cleanup: pycache-remove dsstore-remove mypycache-remove ipynbcheckpoints-remove pytestcache-remove
73-
7444
.PHONY: all $(MAKECMDGOALS)

README.md

Lines changed: 12 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -60,123 +60,22 @@ make install
6060

6161
### Makefile usage
6262

63-
CLI commands for faster development.
64-
65-
<details>
66-
<summary>Install all dependencies</summary>
67-
<p>
68-
69-
Install requirements:
70-
71-
```bash
72-
make install
73-
```
74-
75-
</p>
76-
</details>
77-
78-
<details>
79-
<summary>Codestyle</summary>
80-
<p>
81-
82-
Install pre-commit hooks which will run isort, black and codestyle on your code:
83-
84-
```bash
85-
make pre-commit-install
86-
```
87-
88-
Automatic formatting uses `pyupgrade`, `isort` and `black`.
89-
90-
```bash
91-
make codestyle
92-
93-
# or use synonym
94-
make formatting
95-
```
96-
97-
Codestyle checks only, without rewriting files:
98-
99-
```bash
100-
make check-codestyle
101-
```
102-
103-
> Note: `check-codestyle` uses `isort`, `black` and `darglint` library
104-
105-
Update all dev libraries to the latest version using one command
106-
107-
```bash
108-
make update-dev-deps
109-
```
110-
111-
</p>
112-
</details>
113-
114-
<details>
115-
<summary>Tests with coverage badges</summary>
116-
<p>
117-
118-
Run `pytest`
119-
120-
```bash
121-
make test
122-
```
123-
124-
</p>
125-
</details>
126-
127-
<details>
128-
<summary>All linters</summary>
129-
<p>
130-
131-
```bash
132-
make lint
133-
```
134-
135-
the same as:
136-
137-
```bash
138-
make test && make check-codestyle && make mypy && make check-safety
139-
```
140-
141-
</p>
142-
</details>
143-
144-
<details>
145-
<summary>Cleanup</summary>
146-
<p>
147-
Delete pycache files
148-
149-
```bash
150-
make pycache-remove
151-
```
152-
153-
Remove package build
154-
155-
```bash
156-
make build-remove
157-
```
158-
159-
Delete .DS_STORE files
63+
CLI commands for faster development. See `make help` for more details.
16064

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

165-
Remove .mypycache
166-
167-
```bash
168-
make mypycache-remove
169-
```
170-
171-
Or to remove all above run:
172-
173-
```bash
174-
make cleanup
175-
```
176-
177-
</p>
178-
</details>
179-
18079
## Releases
18180

18281
You can see the list of available releases on the [GitHub Releases](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/releases) page.

api/info/candle.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,3 @@ paths:
104104
s: "BTC"
105105
t: 1681923600000
106106
v: "0.98639"
107-

api/info/l2book.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,3 @@ paths:
6464
{ "px": "20300", "sz": "3", "n": 3 }
6565
]
6666
]
67-

hyperliquid/utils/signing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def sign_inner(wallet, data):
364364

365365

366366
def float_to_wire(x: float) -> str:
367-
rounded = "{:.8f}".format(x)
367+
rounded = f"{x:.8f}"
368368
if abs(float(rounded) - x) >= 1e-12:
369369
raise ValueError("float_to_wire causes rounding", x)
370370
if rounded == "-0":

0 commit comments

Comments
 (0)