From e2a42e1132fa28526002983063febaabbecdd660 Mon Sep 17 00:00:00 2001 From: George Marshall Date: Tue, 18 Apr 2023 20:52:52 -0700 Subject: [PATCH] Upgrade linting to ruff (#98) --- .pre-commit-config.yaml | 22 +++------------------- django_cryptography/core/signing.py | 10 ++++------ django_cryptography/utils/crypto.py | 12 ++++++------ pyproject.toml | 22 ++++++++++++++++++++++ 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5887e37..1b79d83 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/django_cryptography/core/signing.py b/django_cryptography/core/signing.py index 5a8032a..db7a6ac 100644 --- a/django_cryptography/core/signing.py +++ b/django_cryptography/core/signing.py @@ -1,6 +1,5 @@ import binascii import datetime -import re import struct import time import zlib @@ -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 @@ -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( @@ -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: diff --git a/django_cryptography/utils/crypto.py b/django_cryptography/utils/crypto.py index bc83090..913d195 100644 --- a/django_cryptography/utils/crypto.py +++ b/django_cryptography/utils/crypto.py @@ -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 @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 10ad6b2..a98641d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = """