Skip to content

Commit e77cd91

Browse files
authored
Merge pull request #169 from jschlyter/ruff_reformat
Reformat with ruff
2 parents f18448c + 69fed0a commit e77cd91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+244
-401
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
run: poetry install
4848
- name: Run pytest
4949
run: |
50-
poetry run pytest -vvv -ra --cov=cryptojwt --cov-report=xml --isort --black
50+
poetry run pytest -vvv -ra --cov=cryptojwt --cov-report=xml
5151
- name: Upload coverage to Codecov
5252
uses: codecov/codecov-action@v4
5353
with:

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repos:
99
- id: check-yaml
1010
- id: check-json
1111
- repo: https://github.com/astral-sh/ruff-pre-commit
12-
rev: v0.4.9
12+
rev: v0.6.8
1313
hooks:
1414
- id: ruff
1515
- id: ruff-format

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all:
2+
3+
test:
4+
poetry run pytest --ruff --ruff-format --cov
5+
6+
reformat:
7+
poetry run ruff check --select I --fix src tests
8+
poetry run ruff format src tests

pyproject.toml

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
# PEP 518: https://www.python.org/dev/peps/pep-0518/
22

3-
[tool.black]
4-
line-length = 100
5-
6-
[tool.isort]
7-
force_single_line = true
8-
known_first_party = "cryptojwt"
9-
include_trailing_comma = true
10-
force_grid_wrap = 0
11-
use_parentheses = true
12-
line_length = 100
13-
14-
[tool.coverage.run]
15-
branch = true
16-
17-
[tool.coverage.report]
18-
exclude_lines = [
19-
"pragma: no cover",
20-
"raise NotImplementedError",
21-
]
22-
233
[tool.poetry]
244
name = "cryptojwt"
255
version = "1.9.2"
@@ -44,23 +24,31 @@ requests = "^2.25.1"
4424

4525
[tool.poetry.group.dev.dependencies]
4626
alabaster = "^0.7.12"
47-
black = "^24.4.2"
48-
isort = "^5.13.2"
4927
pytest = "^8.2.1"
50-
pytest-black = "^0.3.12"
51-
pytest-isort = "^4.0.0"
5228
pytest-cov = "^4.0.0"
5329
responses = "^0.13.0"
5430
sphinx = "^3.5.2"
5531
sphinx-autobuild = "^2021.3.14"
5632
coverage = "^7"
57-
ruff = "^0.4.6"
33+
ruff = "^0.6.3"
5834
pytest-ruff = "^0.3.2"
5935

6036
[build-system]
6137
requires = ["poetry-core>=1.0.0"]
6238
build-backend = "poetry.core.masonry.api"
6339

40+
[tool.coverage.run]
41+
branch = true
42+
43+
[tool.coverage.report]
44+
exclude_lines = [
45+
"pragma: no cover",
46+
"raise NotImplementedError",
47+
]
48+
49+
[tool.ruff]
50+
line-length = 100
51+
6452
[tool.ruff.lint]
6553
select = [
6654
# pycodestyle
@@ -78,3 +66,9 @@ select = [
7866
]
7967
ignore = ["E501", "I001", "SIM102"]
8068
exclude = ["examples/*"]
69+
70+
[tool.ruff.lint.isort]
71+
force-sort-within-sections = false
72+
combine-as-imports = true
73+
split-on-trailing-comma = false
74+
known-first-party = ["cryptojwt"]

src/cryptojwt/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
from cryptojwt.key_jar import KeyJar
1212

1313
from .exception import BadSyntax
14-
from .utils import as_unicode
15-
from .utils import b64d
16-
from .utils import b64encode_item
17-
from .utils import split_token
14+
from .utils import as_unicode, b64d, b64encode_item, split_token
1815

1916
__version__ = version("cryptojwt")
2017

src/cryptojwt/jwe/aes.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
from struct import pack
33

44
from cryptography.hazmat.primitives import hmac
5-
from cryptography.hazmat.primitives.ciphers import Cipher
6-
from cryptography.hazmat.primitives.ciphers import algorithms
7-
from cryptography.hazmat.primitives.ciphers import modes
5+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
86
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
97
from cryptography.hazmat.primitives.padding import PKCS7
108

11-
from ..exception import MissingKey
12-
from ..exception import Unsupported
13-
from ..exception import VerificationError
9+
from ..exception import MissingKey, Unsupported, VerificationError
1410
from . import Encrypter
1511
from .exception import UnsupportedBitLength
1612
from .utils import get_keys_seclen_dgst

src/cryptojwt/jwe/fernet.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import base64
22
import os
3-
from typing import Optional
4-
from typing import Union
3+
from typing import Optional, Union
54

65
from cryptography.fernet import Fernet
76
from cryptography.hazmat.primitives import hashes

src/cryptojwt/jwe/jwe.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
from ..jwk.hmac import SYMKey
77
from ..jwk.jwk import key_from_jwk_dict
88
from ..jwx import JWx
9-
from .exception import DecryptionFailed
10-
from .exception import NoSuitableDecryptionKey
11-
from .exception import NoSuitableEncryptionKey
12-
from .exception import NotSupportedAlgorithm
13-
from .exception import WrongEncryptionAlgorithm
9+
from .exception import (
10+
DecryptionFailed,
11+
NoSuitableDecryptionKey,
12+
NoSuitableEncryptionKey,
13+
NotSupportedAlgorithm,
14+
WrongEncryptionAlgorithm,
15+
)
1416
from .jwe_ec import JWE_EC
1517
from .jwe_hmac import JWE_SYM
1618
from .jwe_rsa import JWE_RSA
@@ -171,10 +173,7 @@ def decrypt(self, token=None, keys=None, alg=None, cek=None):
171173
elif _alg.startswith("ECDH-ES"):
172174
decrypter = JWE_EC(**self._dict)
173175

174-
if isinstance(keys[0], AsymmetricKey):
175-
_key = keys[0].private_key()
176-
else:
177-
_key = keys[0].key
176+
_key = keys[0].private_key() if isinstance(keys[0], AsymmetricKey) else keys[0].key
178177

179178
cek = decrypter.dec_setup(_jwe, key=_key)
180179
else:

src/cryptojwt/jwe/jwe_ec.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@
22
import struct
33

44
from cryptography.hazmat.primitives.asymmetric import ec
5-
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap
6-
from cryptography.hazmat.primitives.keywrap import aes_key_wrap
7-
8-
from ..jwk.ec import NIST2SEC
9-
from ..jwk.ec import ECKey
10-
from ..utils import as_bytes
11-
from ..utils import as_unicode
12-
from ..utils import b64d
13-
from ..utils import b64e
5+
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap, aes_key_wrap
6+
7+
from ..jwk.ec import NIST2SEC, ECKey
8+
from ..utils import as_bytes, as_unicode, b64d, b64e
149
from . import KEY_LEN
1510
from .jwekey import JWEKey
1611
from .jwenc import JWEnc
17-
from .utils import concat_sha256
18-
from .utils import get_random_bytes
12+
from .utils import concat_sha256, get_random_bytes
1913

2014

2115
def ecdh_derive_key(key, epk, apu, apv, alg, dk_len):

src/cryptojwt/jwe/jwe_hmac.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
import logging
33
import zlib
44

5-
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap
6-
from cryptography.hazmat.primitives.keywrap import aes_key_wrap
5+
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap, aes_key_wrap
76

8-
from ..exception import MissingKey
9-
from ..exception import WrongNumberOfParts
7+
from ..exception import MissingKey, WrongNumberOfParts
108
from ..jwk.hmac import SYMKey
11-
from ..utils import as_bytes
12-
from ..utils import intarr2str
9+
from ..utils import as_bytes, intarr2str
1310
from .jwekey import JWEKey
1411
from .jwenc import JWEnc
1512

src/cryptojwt/jwe/jwe_rsa.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
from ..utils import as_bytes
55
from . import SUPPORTED
6-
from .exception import NotSupportedAlgorithm
7-
from .exception import ParameterError
6+
from .exception import NotSupportedAlgorithm, ParameterError
87
from .jwekey import JWEKey
98
from .jwenc import JWEnc
109
from .rsa import RSAEncrypter

src/cryptojwt/jwe/jwekey.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from ..jwx import JWx
22
from . import KEY_LEN_BYTES
3-
from .aes import AES_CBCEncrypter
4-
from .aes import AES_GCMEncrypter
5-
from .exception import DecryptionFailed
6-
from .exception import NotSupportedAlgorithm
7-
from .utils import alg2keytype
8-
from .utils import get_random_bytes
9-
from .utils import split_ctx_and_tag
3+
from .aes import AES_CBCEncrypter, AES_GCMEncrypter
4+
from .exception import DecryptionFailed, NotSupportedAlgorithm
5+
from .utils import alg2keytype, get_random_bytes, split_ctx_and_tag
106

117

128
class JWEKey(JWx):

src/cryptojwt/jwe/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
from math import ceil
44

55
from cryptography.hazmat.primitives import hashes
6-
from cryptography.hazmat.primitives.hashes import SHA256
7-
from cryptography.hazmat.primitives.hashes import SHA384
8-
from cryptography.hazmat.primitives.hashes import SHA512
6+
from cryptography.hazmat.primitives.hashes import SHA256, SHA384, SHA512
97

108
LENMET = {32: (16, SHA256), 48: (24, SHA384), 64: (32, SHA512)}
119

src/cryptojwt/jwk/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from typing import List
66

77
from ..exception import UnsupportedAlgorithm
8-
from ..utils import as_bytes
9-
from ..utils import as_unicode
10-
from ..utils import b64e
11-
from ..utils import base64url_to_long
8+
from ..utils import as_bytes, as_unicode, b64e, base64url_to_long
129
from .utils import DIGEST_HASH
1310

1411
USE = {"sign": "sig", "decrypt": "enc", "encrypt": "enc", "verify": "sig"}
@@ -236,11 +233,7 @@ def __eq__(self, other):
236233
if set(self.__dict__.keys()) != set(other.__dict__.keys()):
237234
return False
238235

239-
for key in self.public_members:
240-
if getattr(other, key) != getattr(self, key):
241-
return False
242-
243-
return True
236+
return all(getattr(other, key) == getattr(self, key) for key in self.public_members)
244237

245238
def keys(self):
246239
return list(self.to_dict().keys())

src/cryptojwt/jwk/asym.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from . import JWK
2-
from . import USE
1+
from . import JWK, USE
32

43

54
class AsymmetricKey(JWK):
@@ -19,7 +18,7 @@ def __init__(
1918
k="",
2019
pub_key=None,
2120
priv_key=None,
22-
**kwargs
21+
**kwargs,
2322
):
2423
JWK.__init__(self, kty, alg, use, kid, x5c, x5t, x5u, **kwargs)
2524
self.k = k

src/cryptojwt/jwk/ec.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
from cryptojwt.exception import KeyNotFound
44

5-
from ..exception import DeSerializationNotPossible
6-
from ..exception import JWKESTException
7-
from ..exception import UnsupportedECurve
8-
from ..utils import as_unicode
9-
from ..utils import deser
10-
from ..utils import long_to_base64
5+
from ..exception import DeSerializationNotPossible, JWKESTException, UnsupportedECurve
6+
from ..utils import as_unicode, deser, long_to_base64
117
from .asym import AsymmetricKey
12-
from .x509 import import_private_key_from_pem_file
13-
from .x509 import import_public_key_from_pem_data
14-
from .x509 import import_public_key_from_pem_file
8+
from .x509 import (
9+
import_private_key_from_pem_file,
10+
import_public_key_from_pem_data,
11+
import_public_key_from_pem_file,
12+
)
1513

1614
# This is used to translate between the curve representation in
1715
# Cryptography and the one used by NIST (and in RFC 7518)

src/cryptojwt/jwk/hmac.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33

44
from cryptojwt.exception import KeyNotFound
55

6-
from ..exception import JWKException
7-
from ..exception import UnsupportedAlgorithm
8-
from ..exception import WrongUsage
9-
from ..utils import as_bytes
10-
from ..utils import as_unicode
11-
from ..utils import b64d
12-
from ..utils import b64e
13-
from . import JWK
14-
from . import USE
15-
from .utils import sha256_digest
16-
from .utils import sha384_digest
17-
from .utils import sha512_digest
6+
from ..exception import JWKException, UnsupportedAlgorithm, WrongUsage
7+
from ..utils import as_bytes, as_unicode, b64d, b64e
8+
from . import JWK, USE
9+
from .utils import sha256_digest, sha384_digest, sha512_digest
1810

1911
logger = logging.getLogger(__name__)
2012

src/cryptojwt/jwk/jwk.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@
22
import json
33
import os
44

5-
from cryptography.hazmat.primitives.asymmetric import ec
6-
from cryptography.hazmat.primitives.asymmetric import ed448
7-
from cryptography.hazmat.primitives.asymmetric import ed25519
8-
from cryptography.hazmat.primitives.asymmetric import rsa
9-
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_dmp1
10-
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_dmq1
11-
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_iqmp
12-
13-
from ..exception import MissingValue
14-
from ..exception import UnknownKeyType
15-
from ..exception import UnsupportedAlgorithm
16-
from ..exception import WrongKeyType
5+
from cryptography.hazmat.primitives.asymmetric import ec, ed448, ed25519, rsa
6+
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_dmp1, rsa_crt_dmq1, rsa_crt_iqmp
7+
8+
from ..exception import MissingValue, UnknownKeyType, UnsupportedAlgorithm, WrongKeyType
179
from ..utils import base64url_to_long
18-
from .ec import NIST2SEC
19-
from .ec import ECKey
10+
from .ec import NIST2SEC, ECKey
2011
from .hmac import SYMKey
2112
from .okp import OKPKey
2213
from .rsa import RSAKey

src/cryptojwt/jwk/okp.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
from typing import Union
22

33
from cryptography.hazmat.primitives import serialization
4-
from cryptography.hazmat.primitives.asymmetric import ed448
5-
from cryptography.hazmat.primitives.asymmetric import ed25519
6-
from cryptography.hazmat.primitives.asymmetric import x448
7-
from cryptography.hazmat.primitives.asymmetric import x25519
4+
from cryptography.hazmat.primitives.asymmetric import ed448, ed25519, x448, x25519
85

96
from cryptojwt.exception import KeyNotFound
107

11-
from ..exception import DeSerializationNotPossible
12-
from ..exception import JWKESTException
13-
from ..exception import UnsupportedOKPCurve
14-
from ..utils import b64d
15-
from ..utils import b64e
8+
from ..exception import DeSerializationNotPossible, JWKESTException, UnsupportedOKPCurve
9+
from ..utils import b64d, b64e
1610
from .asym import AsymmetricKey
17-
from .x509 import import_private_key_from_pem_file
18-
from .x509 import import_public_key_from_pem_data
19-
from .x509 import import_public_key_from_pem_file
11+
from .x509 import (
12+
import_private_key_from_pem_file,
13+
import_public_key_from_pem_data,
14+
import_public_key_from_pem_file,
15+
)
2016

2117
OKPPublicKey = Union[
2218
ed25519.Ed25519PublicKey, ed448.Ed448PublicKey, x25519.X25519PublicKey, x448.X448PublicKey

0 commit comments

Comments
 (0)