Skip to content

Commit 24d73a0

Browse files
authored
Replace black, isort, flake8, pyupgrade with Ruff (#983)
1 parent 5f95b7b commit 24d73a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+281
-464
lines changed

.pre-commit-config.yaml

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: 24.10.0
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.8.0
44
hooks:
5-
- id: black
6-
language_version: python3.11
7-
8-
- repo: https://github.com/asottile/pyupgrade
9-
rev: v3.19.0
10-
hooks:
11-
- id: pyupgrade
12-
args: [--py39-plus]
5+
- id: ruff
6+
args: [--fix, --exit-non-zero-on-fix]
7+
- id: ruff-format
8+
types_or: [ python, pyi ]
139

1410
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
1511
rev: v2.14.0
1612
hooks:
1713
- id: pretty-format-toml
1814
args: [--autofix]
19-
- repo: https://github.com/pycqa/isort
20-
rev: 5.13.2
21-
hooks:
22-
- id: isort
23-
24-
- repo: https://github.com/PyCQA/flake8
25-
rev: 7.1.1
26-
hooks:
27-
- id: flake8
2815

2916
- repo: https://github.com/adamchainz/django-upgrade
3017
rev: "1.22.1"
3118
hooks:
3219
- id: django-upgrade
3320
args: [--target-version, "4.2"]
34-
- repo: https://github.com/asottile/pyupgrade
35-
rev: v3.19.0
36-
hooks:
37-
- id: pyupgrade
38-
args: [--py39-plus]
3921

django_tables2/columns/base.py

+18-29
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ def column_for_field(self, field, **kwargs):
6262

6363

6464
class LinkTransform:
65-
"""Object used to generate attributes for the `<a>`-tag to wrap the cell content in."""
66-
6765
viewname = None
6866
accessor = None
6967
attrs = None
7068

7169
def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
7270
"""
73-
arguments:
71+
Object used to generate attributes for the `<a>`-tag to wrap the cell content in.
72+
73+
Arguments:
7474
url (callable): If supplied, the result of this callable will be used as ``href`` attribute.
7575
accessor (Accessor): if supplied, the accessor will be used to decide on which object
7676
``get_absolute_url()`` is called.
@@ -120,9 +120,7 @@ def compose_url(self, **kwargs):
120120
return context.get_absolute_url()
121121

122122
def call_reverse(self, record):
123-
"""
124-
Prepares the arguments to reverse() for this record and calls reverse()
125-
"""
123+
"""Prepare the arguments to reverse() for this record and calls reverse()."""
126124

127125
def resolve_if_accessor(val):
128126
return val.resolve(record) if isinstance(val, Accessor) else val
@@ -393,7 +391,7 @@ def order(self, queryset, is_descending):
393391
table or by subclassing `.Column`; but only overrides if second element
394392
in return tuple is True.
395393
396-
returns:
394+
Returns:
397395
Tuple (QuerySet, boolean)
398396
"""
399397
return (queryset, False)
@@ -404,7 +402,9 @@ def from_field(cls, field, **kwargs):
404402
Return a specialized column for the model field or `None`.
405403
406404
Arguments:
407-
field (Model Field instance): the field that needs a suitable column
405+
field (Model Field instance): the field that needs a suitable column.
406+
**kwargs: passed on to the column.
407+
408408
Returns:
409409
`.Column` object or `None`
410410
@@ -429,7 +429,7 @@ class BoundColumn:
429429
In practice, this means that a `.BoundColumn` knows the *"variable name"* given to the `.Column`
430430
when it was declared on the `.Table`.
431431
432-
arguments:
432+
Arguments:
433433
table (`~.Table`): The table in which this column exists
434434
column (`~.Column`): The type of column
435435
name (str): The variable name of the column used when defining the
@@ -465,7 +465,6 @@ def attrs(self):
465465
templates easier. ``tf`` is not actually a HTML tag, but this key name
466466
will be used for attributes for column's footer, if the column has one.
467467
"""
468-
469468
# prepare kwargs for computed_values()
470469
kwargs = {"table": self._table, "bound_column": self}
471470
# BoundRow.items() sets current_record and current_value when iterating over
@@ -503,25 +502,19 @@ def attrs(self):
503502
return attrs
504503

505504
def _get_cell_class(self, attrs):
506-
"""
507-
Return a set of the classes from the class key in ``attrs``.
508-
"""
505+
"""Return a set of the classes from the class key in ``attrs``."""
509506
classes = attrs.get("class", None)
510507
classes = set() if classes is None else set(classes.split(" "))
511508

512509
return self._table.get_column_class_names(classes, self)
513510

514511
def get_td_class(self, td_attrs):
515-
"""
516-
Returns the HTML class attribute for a data cell in this column
517-
"""
512+
"""Return the HTML class attribute for a data cell in this column."""
518513
classes = sorted(self._get_cell_class(td_attrs))
519514
return None if len(classes) == 0 else " ".join(classes)
520515

521516
def get_th_class(self, th_attrs):
522-
"""
523-
Returns the HTML class attribute for a header cell in this column
524-
"""
517+
"""Return the HTML class attribute for a header cell in this column."""
525518
classes = self._get_cell_class(th_attrs)
526519

527520
# add classes for ordering
@@ -539,7 +532,7 @@ def get_th_class(self, th_attrs):
539532

540533
@property
541534
def default(self):
542-
"""Returns the default value for this column."""
535+
"""Return the default value for this column."""
543536
value = self.column.default
544537
if value is None:
545538
value = self._table.default
@@ -698,7 +691,7 @@ def visible(self):
698691

699692
@property
700693
def localize(self):
701-
"""Return `True`, `False` or `None` as described in ``Column.localize``"""
694+
"""Return `True`, `False` or `None` as described in ``Column.localize``."""
702695
return self.column.localize
703696

704697

@@ -742,10 +735,7 @@ def names(self):
742735
return list(self.iternames())
743736

744737
def iterall(self):
745-
"""
746-
Return an iterator that exposes all `.BoundColumn` objects,
747-
regardless of visibility or sortability.
748-
"""
738+
"""Return an iterator that exposes all `.BoundColumn` objects, regardless of visibility or sortability."""
749739
return (column for name, column in self.iteritems())
750740

751741
def all(self):
@@ -759,7 +749,6 @@ def iteritems(self):
759749
consideration all of the ordering and filtering modifiers that a table
760750
supports (e.g. `~Table.Meta.exclude` and `~Table.Meta.sequence`).
761751
"""
762-
763752
for name in self._table.sequence:
764753
if name not in self._table.exclude:
765754
yield (name, self.columns[name])
@@ -769,7 +758,7 @@ def items(self):
769758

770759
def iterorderable(self):
771760
"""
772-
Same as `BoundColumns.all` but only returns orderable columns.
761+
`BoundColumns.all` filtered for whether they can be ordered.
773762
774763
This is useful in templates, where iterating over the full
775764
set and checking ``{% if column.ordarable %}`` can be problematic in
@@ -780,7 +769,7 @@ def iterorderable(self):
780769

781770
def itervisible(self):
782771
"""
783-
Same as `.iterorderable` but only returns visible `.BoundColumn` objects.
772+
Return `.iterorderable` filtered by visibility.
784773
785774
This is geared towards table rendering.
786775
"""
@@ -805,7 +794,7 @@ def show(self, name):
805794
self.columns[name].column.visible = True
806795

807796
def __iter__(self):
808-
"""Convenience API, alias of `.itervisible`."""
797+
"""Alias of `.itervisible` (for convenience)."""
809798
return self.itervisible()
810799

811800
def __contains__(self, item):

django_tables2/columns/booleancolumn.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ def render(self, value, record, bound_column):
5252
return format_html("<span {}>{}</span>", AttributeDict(attrs).as_html(), escape(text))
5353

5454
def value(self, record, value, bound_column):
55-
"""
56-
Returns the content for a specific cell similarly to `.render` however without any html content.
57-
"""
55+
"""Return the content for a specific cell similarly to `.render` however without any html content."""
5856
value = self._get_bool_value(record, value, bound_column)
5957
return str(value)
6058

django_tables2/columns/checkboxcolumn.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ def render(self, value, bound_column, record):
7171
return mark_safe(f"<input {AttributeDict(attrs).as_html()} />")
7272

7373
def is_checked(self, value, record):
74-
"""
75-
Determine if the checkbox should be checked
76-
"""
74+
"""Determine if the checkbox should be checked."""
7775
if self.checked is None:
7876
return False
7977
if self.checked is True:

django_tables2/columns/datecolumn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DateColumn(TemplateColumn):
1919
def __init__(self, format=None, short=True, *args, **kwargs):
2020
if format is None:
2121
format = "SHORT_DATE_FORMAT" if short else "DATE_FORMAT"
22-
template = '{{ value|date:"%s"|default:default }}' % format
22+
template = '{{ value|date:"%s"|default:default }}' % format # noqa: UP031
2323
super().__init__(template_code=template, *args, **kwargs)
2424

2525
@classmethod

django_tables2/columns/datetimecolumn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DateTimeColumn(TemplateColumn):
1919
def __init__(self, format=None, short=True, *args, **kwargs):
2020
if format is None:
2121
format = "SHORT_DATETIME_FORMAT" if short else "DATETIME_FORMAT"
22-
template = '{{ value|date:"%s"|default:default }}' % format
22+
template = '{{ value|date:"%s"|default:default }}' % format # noqa: UP031
2323
super().__init__(template_code=template, *args, **kwargs)
2424

2525
@classmethod

django_tables2/columns/linkcolumn.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ def text_value(self, record, value):
2525
return self.text(record) if callable(self.text) else self.text
2626

2727
def value(self, record, value):
28-
"""
29-
Returns the content for a specific cell similarly to `.render` however
30-
without any html content.
31-
"""
28+
"""Return the content for a specific cell similarly to `.render` without any HTML content."""
3229
return self.text_value(record, value)
3330

3431
def render(self, record, value):
@@ -38,7 +35,7 @@ def render(self, record, value):
3835
@library.register
3936
class LinkColumn(BaseLinkColumn):
4037
"""
41-
Renders a normal value as an internal hyperlink to another page.
38+
Render a normal value as an internal hyperlink to another page.
4239
4340
.. note ::
4441

django_tables2/columns/manytomanycolumn.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@library.register
1010
class ManyToManyColumn(Column):
1111
"""
12-
Display the list of objects from a `ManyRelatedManager`
12+
Display the list of objects from a `ManyRelatedManager`.
1313
1414
Ordering is disabled for this column.
1515
@@ -72,16 +72,11 @@ def __init__(
7272
self.linkify_item = LinkTransform(attrs=self.attrs.get("a", {}), **link_kwargs)
7373

7474
def transform(self, obj):
75-
"""
76-
Transform is applied to each item of the list of objects from the ManyToMany relation.
77-
"""
75+
"""Apply to each item of the list of objects from the ManyToMany relation."""
7876
return force_str(obj)
7977

8078
def filter(self, qs):
81-
"""
82-
Filter is called on the ManyRelatedManager to allow ordering, filtering or limiting
83-
on the set of related objects.
84-
"""
79+
"""Call on the ManyRelatedManager to allow ordering, filtering or limiting on the set of related objects."""
8580
return qs.all()
8681

8782
def render(self, value):

django_tables2/columns/templatecolumn.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
@library.register
99
class TemplateColumn(Column):
1010
"""
11-
A subclass of `.Column` that renders some template code to use as
12-
the cell value.
11+
A subclass of `.Column` that renders some template code to use as the cell value.
1312
1413
Arguments:
1514
template_code (str): template code to render
@@ -69,8 +68,9 @@ def render(self, record, table, value, bound_column, **kwargs):
6968

7069
def value(self, **kwargs):
7170
"""
72-
The value returned from a call to `value()` on a `TemplateColumn` is
73-
the rendered template with `django.utils.html.strip_tags` applied.
71+
Non-HTML value returned from a call to `value()` on a `TemplateColumn`.
72+
73+
By default this is the rendered template with `django.utils.html.strip_tags` applied.
7474
Leading and trailing whitespace is stripped.
7575
"""
7676
html = super().value(**kwargs)

django_tables2/columns/timecolumn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TimeColumn(TemplateColumn):
1717
def __init__(self, format=None, *args, **kwargs):
1818
if format is None:
1919
format = "TIME_FORMAT"
20-
template = '{{ value|date:"%s"|default:default }}' % format
20+
template = '{{ value|date:"%s"|default:default }}' % format # noqa: UP031
2121
super().__init__(template_code=template, *args, **kwargs)
2222

2323
@classmethod

0 commit comments

Comments
 (0)