Skip to content

Commit

Permalink
[FEAT] add ruff and pre-commit (#19)
Browse files Browse the repository at this point in the history
* [FEAT] add ruff and pre-commit
* and removing isort and flake8
* formatting
  • Loading branch information
dni authored Aug 17, 2023
1 parent babc3d6 commit 7ef3dd6
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 117 deletions.
7 changes: 0 additions & 7 deletions .flake8

This file was deleted.

5 changes: 1 addition & 4 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: format
name: black
on: [push, pull_request]
jobs:
build:
Expand All @@ -24,6 +24,3 @@ jobs:
- name: Check black
run: |
make checkblack
- name: Check isort
run: |
make checkisort
8 changes: 8 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [ push, pull_request ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-docstring-first
- id: check-json
- id: debug-statements
- id: mixed-line-ending
- id: check-case-conflict
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.283
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
21 changes: 9 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
all: black isort mypy flake8 test
format: black isort
lint: flake8 mypy
check: checkblack checkisort
all: black ruff mypy test
format: black ruff isort
lint: ruff mypy
check: checkblack checkruff

black:
poetry run black .

isort:
poetry run isort .
ruff:
poetry run ruff check . --fix

checkruff:
poetry run ruff check .

checkblack:
poetry run black --check .

checkisort:
poetry run isort --check-only .

mypy:
poetry run mypy .

flake8:
poetry run flake8

test:
poetry run pytest tests --cov=lnurl --cov-report=xml
8 changes: 6 additions & 2 deletions lnurl/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def strict_rfc3986_validator(value: str) -> str:

class ReprMixin:
def __repr__(self) -> str:
attrs = [n for n in [n for n in self.__slots__ if not n.startswith("_")] if getattr(self, n) is not None] # type: ignore
attrs = [ # type: ignore
outer_slot # type: ignore
for outer_slot in [slot for slot in self.__slots__ if not slot.startswith("_")] # type: ignore
if getattr(self, outer_slot) # type: ignore
] # type: ignore
extra = ", " + ", ".join(f"{n}={getattr(self, n).__repr__()}" for n in attrs) if attrs else ""
return f"{self.__class__.__name__}({super().__repr__()}{extra})"

Expand All @@ -47,7 +51,7 @@ class Bech32(ReprMixin, str):

__slots__ = ("hrp", "data")

def __new__(cls, bech32: str, **kwargs) -> "Bech32":
def __new__(cls, bech32: str, **_) -> "Bech32":
return str.__new__(cls, bech32)

def __init__(self, bech32: str, *, hrp: Optional[str] = None, data: Optional[List[int]] = None):
Expand Down
238 changes: 183 additions & 55 deletions poetry.lock

Large diffs are not rendered by default.

61 changes: 53 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,18 @@ pydantic = "^1"
bech32 = "^1.2.0"

[tool.poetry.group.dev.dependencies]
flake8 = "^6.1.0"
black = "^23.7.0"
pytest = "^7.4.0"
pytest-cov = "^4.1.0"
types-requests = "^2.31.0.2"
mypy = "^1.5.1"
isort = "^5.12.0"
ruff = "^0.0.284"
pre-commit = "^3.3.3"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.black]
line-length = 120

[tool.isort]
profile = "black"

[tool.mypy]
ignore_missing_imports = "True"
files = "lnurl"
Expand All @@ -46,3 +40,54 @@ files = "lnurl"
testpaths = [
"tests"
]

[tool.black]
line-length = 120
preview = true

[tool.ruff]
# Same as Black. but black has a 10% overflow rule
line-length = 120

# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
# (`I`) means isorting
select = ["E", "F", "I"]
ignore = []

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

target-version = "py39"

[tool.ruff.mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10
20 changes: 11 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class TestDecode:
"bech32, url",
[
(
"LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU",
(
"LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU"
),
"https://service.io/?q=3fc3645b439ce8e7f2553a69e5267081d96dcd340693afabe04be7b0ccd178df",
)
],
Expand Down Expand Up @@ -48,8 +50,10 @@ class TestEncode:
"bech32, url",
[
(
"LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU",
(
"LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU"
),
"https://service.io/?q=3fc3645b439ce8e7f2553a69e5267081d96dcd340693afabe04be7b0ccd178df",
)
],
Expand All @@ -72,10 +76,8 @@ class TestHandle:
@pytest.mark.parametrize(
"bech32",
[
(
"LNURL1DP68GURN8GHJ7MR9VAJKUEPWD3HXY6T5WVHXXMMD9AMKJARGV3EXZAE0V9CXJTMKXYH"
"KCMN4WFKZ7MJT2C6X2NRK0PDRYJNGWVU9WDN2G4V8XK2VSZA2RC"
)
"LNURL1DP68GURN8GHJ7MR9VAJKUEPWD3HXY6T5WVHXXMMD9AMKJARGV3EXZAE0V9CXJTMKXYH"
"KCMN4WFKZ7MJT2C6X2NRK0PDRYJNGWVU9WDN2G4V8XK2VSZA2RC"
],
)
def test_handle_withdraw(self, bech32):
Expand Down Expand Up @@ -103,7 +105,7 @@ class TestPayFlow:
@pytest.mark.xfail(raises=NotImplementedError)
@pytest.mark.parametrize(
"bech32",
[("LNURL1DP68GURN8GHJ7MR9VAJKUEPWD3HXY6T5WVHXXMMD9AKXUATJD3CZ7JN9F4EHQJQC25ZZY")],
["LNURL1DP68GURN8GHJ7MR9VAJKUEPWD3HXY6T5WVHXXMMD9AKXUATJD3CZ7JN9F4EHQJQC25ZZY"],
)
def test_pay_flow(self, bech32):
res = handle(bech32)
Expand Down
20 changes: 7 additions & 13 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ def test_success_response(self, d):
assert (
res.json()
== res.json(by_alias=True)
== (
f'{{"tag": "payRequest", "callback": "https://service.io/pay", '
f'"minSendable": 1000, "maxSendable": 2000, "metadata": {json.dumps(metadata)}}}'
)
== '{"tag": "payRequest", "callback": "https://service.io/pay", '
f'"minSendable": 1000, "maxSendable": 2000, "metadata": {json.dumps(metadata)}}}'
)
assert (
res.dict()
Expand Down Expand Up @@ -154,11 +152,9 @@ def test_success_response(self, d):
assert (
res.json()
== res.json(by_alias=True)
== (
f'{{"tag": "payRequest", "callback": "https://service.io/pay", '
f'"minSendable": 1000, "maxSendable": 2000, "metadata": {json.dumps(metadata)}, '
f'"commentAllowed": 555}}'
)
== '{"tag": "payRequest", "callback": "https://service.io/pay", '
f'"minSendable": 1000, "maxSendable": 2000, "metadata": {json.dumps(metadata)}, '
'"commentAllowed": 555}'
)
assert (
res.dict()
Expand Down Expand Up @@ -227,10 +223,8 @@ def test_success_response(self, d):
assert (
res.json()
== res.json(by_alias=True)
== (
'{"tag": "withdrawRequest", "callback": "https://service.io/withdraw", "k1": "c3RyaW5n", '
'"minWithdrawable": 100, "maxWithdrawable": 200, "defaultDescription": ""}'
)
== '{"tag": "withdrawRequest", "callback": "https://service.io/withdraw", "k1": "c3RyaW5n", '
'"minWithdrawable": 100, "maxWithdrawable": 200, "defaultDescription": ""}'
)
assert (
res.dict()
Expand Down
20 changes: 13 additions & 7 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ class TestLightningInvoice:
"bech32, hrp, prefix, amount, h",
[
(
"lntb20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd"
"5d7xmw5fk98klysy043l2ahrqsfpp3x9et2e20v6pu37c5d9vax37wxq72un98k6vcx9fz94w0qf237cm2rqv9pmn5lnexfvf55"
"79slr4zq3u8kmczecytdx0xg9rwzngp7e6guwqpqlhssu04sucpnz4axcv2dstmknqq6jsk2l",
(
"lntb20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd"
"5d7xmw5fk98klysy043l2ahrqsfpp3x9et2e20v6pu37c5d9vax37wxq72un98k6vcx9fz94w0qf237cm2rqv9pmn5lnexfvf55"
"79slr4zq3u8kmczecytdx0xg9rwzngp7e6guwqpqlhssu04sucpnz4axcv2dstmknqq6jsk2l"
),
"lntb20m",
"lntb",
20,
Expand Down Expand Up @@ -115,13 +117,17 @@ class TestLnurl:
"lightning, url",
[
(
"LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU",
(
"LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU"
),
"https://service.io/?q=3fc3645b439ce8e7f2553a69e5267081d96dcd340693afabe04be7b0ccd178df",
),
(
"lightning:LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU",
(
"lightning:LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWE3JX56NXCFK89JN2V3K"
"XUCRSVTY8YMXGCMYXV6RQD3EXDSKVCTZV5CRGCN9XA3RQCMRVSCNWWRYVCYAE0UU"
),
"https://service.io/?q=3fc3645b439ce8e7f2553a69e5267081d96dcd340693afabe04be7b0ccd178df",
),
],
Expand Down

0 comments on commit 7ef3dd6

Please sign in to comment.