Skip to content

Commit

Permalink
Update hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Apr 25, 2024
1 parent 7f680fb commit 3450720
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: ".yarn/|yarn.lock|\\.min\\.(css|js)$"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-builtin-literals
Expand All @@ -14,7 +14,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.15.0
rev: 1.16.0
hooks:
- id: django-upgrade
args: [--target-version, "3.2"]
Expand All @@ -23,7 +23,7 @@ repos:
hooks:
- id: absolufy-imports
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.6"
rev: "v0.4.1"
hooks:
- id: ruff
- id: ruff-format
Expand All @@ -34,10 +34,10 @@ repos:
args: [--list-different, --no-semi]
exclude: "^conf/|.*\\.html$"
- repo: https://github.com/tox-dev/pyproject-fmt
rev: 1.5.1
rev: 1.8.0
hooks:
- id: pyproject-fmt
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.15
rev: v0.16
hooks:
- id: validate-pyproject
15 changes: 9 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ include = ["tree_queries/"]
path = "tree_queries/__init__.py"

[tool.ruff]
fix = true
preview = true
show-fixes = true
target-version = "py38"

[tool.ruff.lint]
extend-select = [
# pyflakes, pycodestyle
"F", "E", "W",
Expand Down Expand Up @@ -90,18 +96,15 @@ extend-ignore = [
# No line length errors
"E501",
]
fix = true
show-fixes = true
target-version = "py38"

[tool.ruff.isort]
[tool.ruff.lint.isort]
combine-as-imports = true
lines-after-imports = 2

[tool.ruff.mccabe]
[tool.ruff.lint.mccabe]
max-complexity = 15

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"*/migrat*/*" = [
# Allow using PascalCase model names in migrations
"N806",
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __str__(self):


class UUIDModel(TreeNode):
id = models.UUIDField(primary_key=True, default=uuid.uuid4) # noqa: A003
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=100)

def __str__(self):
Expand Down
4 changes: 1 addition & 3 deletions tests/testapp/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,7 @@ def test_annotate_tree(self):
else:
qs = qs.annotate(
is_my_field=RawSQL(
'instr(__tree.tree_path, "{sep}{pk}{sep}") <> 0'.format(
pk=pk(tree.child2_1), sep=SEPARATOR
),
f'instr(__tree.tree_path, "{SEPARATOR}{pk(tree.child2_1)}{SEPARATOR}") <> 0',
[],
output_field=models.BooleanField(),
)
Expand Down
30 changes: 14 additions & 16 deletions tree_queries/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,20 @@ def as_sql(self, *args, **kwargs):

extra_fields = self.query.get_extra_fields()
qn = self.connection.ops.quote_name
params.update(
{
"extra_fields_columns": "".join(
f"{qn(column)}, " for column in extra_fields.values()
),
"extra_fields_names": "".join(f"{qn(name)}, " for name in extra_fields),
"extra_fields_initial": "".join(
f"array[T.{qn(column)}]::text[] AS {qn(name)}, "
for name, column in extra_fields.items()
),
"extra_fields_recursive": "".join(
f"__tree.{qn(name)} || T.{qn(column)}, "
for name, column in extra_fields.items()
),
}
)
params.update({
"extra_fields_columns": "".join(
f"{qn(column)}, " for column in extra_fields.values()
),
"extra_fields_names": "".join(f"{qn(name)}, " for name in extra_fields),
"extra_fields_initial": "".join(
f"array[T.{qn(column)}]::text[] AS {qn(name)}, "
for name, column in extra_fields.items()
),
"extra_fields_recursive": "".join(
f"__tree.{qn(name)} || T.{qn(column)}, "
for name, column in extra_fields.items()
),
})

if "__tree" not in self.query.extra_tables: # pragma: no branch - unlikely
tree_params = params.copy()
Expand Down
9 changes: 3 additions & 6 deletions tree_queries/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def extra_fields(self, **extra_fields):
self.query.extra_fields = extra_fields
return self

def as_manager(cls, *, with_tree_fields=False): # noqa: N805
@classmethod
def as_manager(cls, *, with_tree_fields=False):
manager_class = TreeManager.from_queryset(cls)
# Only used in deconstruct:
manager_class._built_with_as_manager = True
Expand All @@ -93,7 +94,6 @@ def as_manager(cls, *, with_tree_fields=False): # noqa: N805
return manager_class()

as_manager.queryset_only = True
as_manager = classmethod(as_manager)

def ancestors(self, of, *, include_self=False):
"""
Expand Down Expand Up @@ -128,10 +128,7 @@ def descendants(self, of, *, include_self=False):
where=[
# XXX This *may* be unsafe with some primary key field types.
# It is certainly safe with integers.
'instr(__tree.tree_path, "{sep}{pk}{sep}") <> 0'.format(
pk=self.model._meta.pk.get_db_prep_value(pk(of), connection),
sep=SEPARATOR,
)
f'instr(__tree.tree_path, "{SEPARATOR}{self.model._meta.pk.get_db_prep_value(pk(of), connection)}{SEPARATOR}") <> 0'
]
)

Expand Down

0 comments on commit 3450720

Please sign in to comment.