format the codebase with ruff

This commit is contained in:
David Paul Graham 2026-03-14 08:46:15 -04:00
parent f5033cafef
commit d7fefc1198
No known key found for this signature in database
GPG key ID: ACDB045B782EF333
4 changed files with 13 additions and 29 deletions

3
.gitignore vendored
View file

@ -7,3 +7,6 @@ dist/
.ruff_cache
.venv
.venv_django_*
# Node
node_modules/

View file

@ -24,9 +24,7 @@ class EncryptedFieldMixin:
def keys(self) -> list[bytes]:
keys = []
salt_keys = (
settings.SALT_KEY
if isinstance(settings.SALT_KEY, list)
else [settings.SALT_KEY]
settings.SALT_KEY if isinstance(settings.SALT_KEY, list) else [settings.SALT_KEY]
)
secret_keys = [settings.SECRET_KEY] + getattr(settings, "SECRET_KEY_FALLBACKS", list())
for secret_key in secret_keys:
@ -39,11 +37,7 @@ class EncryptedFieldMixin:
iterations=100_000,
backend=default_backend(),
)
keys.append(
base64.urlsafe_b64encode(
kdf.derive(secret_key.encode("utf-8"))
)
)
keys.append(base64.urlsafe_b64encode(kdf.derive(secret_key.encode("utf-8"))))
return keys
@cached_property
@ -85,11 +79,7 @@ class EncryptedFieldMixin:
return self.to_python(value)
def to_python(self, value: _TypeAny) -> _TypeAny:
if (
value is None
or not isinstance(value, str)
or hasattr(self, "_already_decrypted")
):
if value is None or not isinstance(value, str) or hasattr(self, "_already_decrypted"):
return value
try:
value = self.f.decrypt(bytes(value, "utf-8")).decode("utf-8")
@ -129,9 +119,7 @@ class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
# they're based on values retrieved from `connection`.
validators_ = [*self.default_validators, *self._validators]
internal_type = models.IntegerField().get_internal_type()
min_value, max_value = BaseDatabaseOperations.integer_field_ranges[
internal_type
]
min_value, max_value = BaseDatabaseOperations.integer_field_ranges[internal_type]
if min_value is not None and not any(
(
isinstance(validator, MinValueValidator)
@ -203,11 +191,7 @@ class EncryptedJSONField(EncryptedFieldMixin, models.JSONField):
return "JSONField"
def to_python(self, value: _TypeAny) -> _TypeAny:
if (
value is None
or not isinstance(value, str)
or hasattr(self, "_already_decrypted")
):
if value is None or not isinstance(value, str) or hasattr(self, "_already_decrypted"):
return value
try:
value = self._decrypt_values(value=json.loads(value))

View file

@ -13,9 +13,7 @@ from .models import TestModel
class FieldTest(TestCase):
def get_db_value(self, field: str, model_id: int) -> None:
cursor = connection.cursor()
cursor.execute(
f"select {field} from package_test_testmodel where id = {model_id};"
)
cursor.execute(f"select {field} from package_test_testmodel where id = {model_id};")
return cursor.fetchone()[0]
def test_char_field_encrypted(self) -> None:
@ -258,14 +256,13 @@ class RotatedSaltTestCase(TestCase):
class RotatedSecretKeyTestCase(TestCase):
@staticmethod
def clear_cached_properties():
# we have to clear the cached properties of EncryptedFieldMixin so we have the right encryption keys
text_field = TestModel._meta.get_field('text')
if hasattr(text_field, 'keys'):
text_field = TestModel._meta.get_field("text")
if hasattr(text_field, "keys"):
del text_field.keys
if hasattr(text_field, 'f'):
if hasattr(text_field, "f"):
del text_field.f
@classmethod
@ -308,4 +305,3 @@ class RotatedSecretKeyTestCase(TestCase):
assert old_record.text.endswith("=")
# assert that old record cannot be decrypted now
assert old_record.text != plaintext

View file

@ -5,6 +5,7 @@
fix = true
extend-include = ["package_test/**/*.py"]
target-version = "py310"
line-length = 100
[tool.ruff.lint]
fixable = ["ALL"]