Skip to content

Commit 541836e

Browse files
committed
change black and isort to ruff
1 parent 1ccff57 commit 541836e

14 files changed

Lines changed: 72 additions & 56 deletions

File tree

.pre-commit-config.yaml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
repos:
2+
- repo: https://github.com/charliermarsh/ruff-pre-commit
3+
rev: v0.14.10
4+
hooks:
5+
# Run the linter.
6+
- id: ruff
7+
args: [ --fix ]
8+
# Run the formatter.
9+
- id: ruff-format
210
- repo: local
311
hooks:
4-
- id: black
5-
name: black
6-
entry: poetry run black src stubs tests
7-
language: system
8-
require_serial: true
9-
types: [python]
10-
pass_filenames: false
11-
- id: isort
12-
name: isort
13-
entry: poetry run isort
14-
language: system
15-
types: [python]
16-
require_serial: true
17-
args: ['--filter-files']
18-
files: "^(src/.*\\.py|tests/.*\\.py|stubs/.*\\.py)"
1912
- id: mypy
2013
name: mypy
2114
entry: poetry run mypy

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# minisignxml
22

3-
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
3+
[![Linter: Ruff](https://img.shields.io/badge/Linter-Ruff-blue)](https://github.com/astral-sh/ruff)
44
[![CircleCI](https://circleci.com/gh/HENNGE/minisignxml.svg?style=svg)](https://circleci.com/gh/HENNGE/minisignxml)
55

66

examples/roundtrip.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import datetime
2-
from typing import Tuple
32

43
from cryptography import x509
54
from cryptography.hazmat.backends import default_backend
@@ -35,7 +34,7 @@ def roundtrip() -> None:
3534
print(serialize_xml(verified).decode("utf-8"))
3635

3736

38-
def make_key_and_cert() -> Tuple[RSAPrivateKey, Certificate]:
37+
def make_key_and_cert() -> tuple[RSAPrivateKey, Certificate]:
3938
"""
4039
Create a private key/certificate pair. In real code you would usually
4140
generate these once and then securely store them somewhere.
@@ -50,8 +49,11 @@ def make_key_and_cert() -> Tuple[RSAPrivateKey, Certificate]:
5049
.issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "test")]))
5150
.public_key(key.public_key())
5251
.serial_number(x509.random_serial_number())
53-
.not_valid_before(datetime.datetime.utcnow())
54-
.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=1))
52+
.not_valid_before(datetime.datetime.now(tz=datetime.timezone.utc))
53+
.not_valid_after(
54+
datetime.datetime.now(tz=datetime.timezone.utc)
55+
+ datetime.timedelta(days=1)
56+
)
5557
.sign(key, hashes.SHA256(), backend)
5658
)
5759
return key, cert

pyproject.toml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,33 @@ defusedxml = ">=0.7.1"
3030

3131
[tool.poetry.group.dev.dependencies]
3232
pytest = ">=5.2"
33-
black = "^23"
34-
isort = "^5"
3533
pytest-cov = ">=2.8.1"
3634
mypy = "^1"
3735
lxml-stubs = "^0.4.0"
36+
ruff = "^0.14.13"
3837

39-
[tool.isort]
40-
profile = "black"
38+
[tool.ruff]
39+
line-length = 88
40+
exclude = [
41+
"examples/",
42+
]
43+
44+
[tool.ruff.lint]
45+
ignore = [
46+
"E722", # Do not use bare `except`
47+
"E501", # Line too long (104 > 88 characters)
48+
"E731", # Do not assign a `lambda` expression, use a `def`
49+
"UP046",
50+
"UP047",
51+
]
52+
select = [
53+
"DTZ",
54+
"E",
55+
"F",
56+
"I",
57+
"T201", # Don't commit print statements
58+
"UP",
59+
]
4160

4261
[tool.mypy]
4362
strict = true

src/minisignxml/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

3+
from collections.abc import Collection
34
from dataclasses import dataclass
4-
from typing import Collection, Type
55

66
from cryptography.hazmat.primitives import hashes
77

@@ -18,8 +18,8 @@ def default(cls) -> SigningConfig:
1818

1919
@dataclass(frozen=True)
2020
class VerifyConfig:
21-
allowed_signature_method: Collection[Type[hashes.HashAlgorithm]]
22-
allowed_digest_method: Collection[Type[hashes.HashAlgorithm]]
21+
allowed_signature_method: Collection[type[hashes.HashAlgorithm]]
22+
allowed_digest_method: Collection[type[hashes.HashAlgorithm]]
2323

2424
@classmethod
2525
def default(cls) -> VerifyConfig:

src/minisignxml/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Collection
1+
from collections.abc import Collection
22

33
from cryptography.x509 import Certificate
44

src/minisignxml/internal/namespaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from lxml.builder import ElementMaker
22

3-
from .constants import *
3+
from .constants import XML_EXC_C14N, XMLDSIG
44

55

66
def make_namespace(prefix: str, urn: str) -> ElementMaker:

src/minisignxml/internal/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import base64
2-
from typing import List, Mapping
2+
from collections.abc import Mapping
33

44
from cryptography.hazmat.backends import default_backend
55
from cryptography.hazmat.primitives import hashes
@@ -17,7 +17,7 @@
1717
UnsupportedAlgorithm,
1818
UnsupportedHasher,
1919
)
20-
from .constants import *
20+
from .constants import XMLDSIG_RSA_SHA1, XMLDSIG_RSA_SHA256, XMLDSIG_SHA1, XMLENC_SHA256
2121
from .namespaces import NAMESPACE_MAP
2222

2323

@@ -83,7 +83,7 @@ def find_or_raise(
8383
return exactly_one(element.findall(path, ns_map), path, element)
8484

8585

86-
def exactly_one(elements: List[Element], path: str, parent: Element) -> Element:
86+
def exactly_one(elements: list[Element], path: str, parent: Element) -> Element:
8787
num_results = len(elements)
8888
if num_results < 1:
8989
raise ElementNotFound(path, parent)

src/minisignxml/sign.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .config import SigningConfig
99
from .errors import NoIDAttribute
1010
from .internal import utils
11-
from .internal.constants import *
11+
from .internal.constants import XML_EXC_C14N, XMLDSIG_ENVELOPED_SIGNATURE
1212
from .internal.namespaces import ds
1313

1414
__all__ = ("sign",)

src/minisignxml/verify.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from collections.abc import Collection
12
from hmac import compare_digest
2-
from typing import Collection, List, Tuple, cast
3+
from typing import cast
34

45
from cryptography.exceptions import InvalidSignature
56
from cryptography.hazmat.backends import default_backend
@@ -23,7 +24,7 @@ def extract_verified_element_and_certificate(
2324
certificates: Collection[Certificate],
2425
config: VerifyConfig = VerifyConfig.default(),
2526
attribute: str = "ID",
26-
) -> Tuple[Element, Certificate]:
27+
) -> tuple[Element, Certificate]:
2728
tree = utils.deserialize_xml(xml)
2829
signature = utils.find_or_raise(tree, ".//ds:Signature")
2930
signed_info = utils.find_or_raise(signature, "./ds:SignedInfo")
@@ -77,7 +78,7 @@ def extract_verified_element_and_certificate(
7778
raise UnsupportedAlgorithm(digest_method)
7879
referenced_element = utils.exactly_one(
7980
cast(
80-
List[Element],
81+
list[Element],
8182
XPath(f"descendant-or-self::*[@{attribute} = $reference_id]")(
8283
tree, reference_id=reference_id
8384
),

0 commit comments

Comments
 (0)