Skip to content

Commit

Permalink
Upgrade linting to ruff (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemarshall authored Apr 19, 2023
1 parent 53c35c5 commit e2a42e1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
22 changes: 3 additions & 19 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,14 @@ repos:
exclude: ^docs/
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
exclude: ^docs/
args: [--py37-plus]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.261
hooks:
- id: flake8
exclude: ^docs/
additional_dependencies:
- flake8-bugbear==23.3.23
- flake8-builtins==2.1.0
- flake8-comprehensions==3.12.0
- id: ruff
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.6
hooks:
Expand Down
10 changes: 4 additions & 6 deletions django_cryptography/core/signing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import binascii
import datetime
import re
import struct
import time
import zlib
Expand All @@ -17,6 +16,7 @@
get_cookie_signer,
)
from django.utils.encoding import force_bytes
from django.utils.regex_helper import _lazy_re_compile

from ..typing import Algorithm, Serializer
from ..utils.crypto import HASHES, InvalidAlgorithm, constant_time_compare, salted_hmac
Expand Down Expand Up @@ -46,9 +46,7 @@
]

_MAX_CLOCK_SKEW = 60
# RemovedInDjango30Warning: when the deprecation ends, replace with:
# _SEP_UNSAFE = _lazy_re_compile(r'^[A-z0-9-_=]*$')
_SEP_UNSAFE = re.compile(r"^[A-z0-9-_=]*$")
_SEP_UNSAFE = _lazy_re_compile(r"^[A-z0-9-_=]*$")


def base64_hmac(
Expand Down Expand Up @@ -307,8 +305,8 @@ def unsign(
fmt = ">cQ%ds%ds" % (len(signed_value) - h_size - d_size, d_size)
try:
version, timestamp, value, sig = struct.unpack(fmt, signed_value)
except struct.error:
raise BadSignature("Signature is not valid")
except struct.error as err:
raise BadSignature("Signature is not valid") from err
if version != self.version:
raise BadSignature("Signature version not supported")
if max_age is not None:
Expand Down
12 changes: 6 additions & 6 deletions django_cryptography/utils/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ def decrypt(self, data: bytes, ttl: Optional[int] = None) -> bytes:
plaintext_padded = decryptor.update(ciphertext)
try:
plaintext_padded += decryptor.finalize()
except ValueError:
raise InvalidToken
except ValueError as err:
raise InvalidToken from err

# Remove padding
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
unpadded = unpadder.update(plaintext_padded)
try:
unpadded += unpadder.finalize()
except ValueError:
raise InvalidToken
except ValueError as err:
raise InvalidToken from err
return unpadded


Expand Down Expand Up @@ -206,6 +206,6 @@ def _encrypt_from_parts(self, data: bytes, current_time: int, iv: bytes) -> byte
def decrypt(self, token: bytes, ttl: Optional[int] = None) -> bytes:
try:
data = base64.urlsafe_b64decode(token)
except (TypeError, Error):
raise InvalidToken
except (TypeError, Error) as err:
raise InvalidToken from err
return super().decrypt(data, ttl)
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ plugins = ['mypy_django_plugin.main']
module = ['appconf']
ignore_missing_imports = true

[tool.ruff]
select = [
"A", # flake8-builtins
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # mccabe
"E", # pycodestyle (Error)
"ERA", # eradicate
"F", # Pyflakes
"I", # isort
"RUF", # Ruff-specific
"TCH", # flake8-type-checking
"UP", # pyupgrade
"W", # pycodestyle (Warning)

]
exclude = ["docs"]
target-version = "py37"

[tool.ruff.per-file-ignores]
"tests/*" = ["ERA"]

[tool.tox]
# language=ini
legacy_tox_ini = """
Expand Down

0 comments on commit e2a42e1

Please sign in to comment.