Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace black, isort, flake8, pyupgrade with Ruff #983

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
repos:
- repo: https://github.com/psf/black
rev: 24.10.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: black
language_version: python3.11

- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
hooks:
- id: pyupgrade
args: [--py39-plus]
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
types_or: [ python, pyi ]

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.14.0
hooks:
- id: pretty-format-toml
args: [--autofix]
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8

- repo: https://github.com/adamchainz/django-upgrade
rev: "1.22.1"
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
hooks:
- id: pyupgrade
args: [--py39-plus]

47 changes: 18 additions & 29 deletions django_tables2/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def column_for_field(self, field, **kwargs):


class LinkTransform:
"""Object used to generate attributes for the `<a>`-tag to wrap the cell content in."""

viewname = None
accessor = None
attrs = None

def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
"""
arguments:
Object used to generate attributes for the `<a>`-tag to wrap the cell content in.

Arguments:
url (callable): If supplied, the result of this callable will be used as ``href`` attribute.
accessor (Accessor): if supplied, the accessor will be used to decide on which object
``get_absolute_url()`` is called.
Expand Down Expand Up @@ -120,9 +120,7 @@ def compose_url(self, **kwargs):
return context.get_absolute_url()

def call_reverse(self, record):
"""
Prepares the arguments to reverse() for this record and calls reverse()
"""
"""Prepare the arguments to reverse() for this record and calls reverse()."""

def resolve_if_accessor(val):
return val.resolve(record) if isinstance(val, Accessor) else val
Expand Down Expand Up @@ -393,7 +391,7 @@ def order(self, queryset, is_descending):
table or by subclassing `.Column`; but only overrides if second element
in return tuple is True.

returns:
Returns:
Tuple (QuerySet, boolean)
"""
return (queryset, False)
Expand All @@ -404,7 +402,9 @@ def from_field(cls, field, **kwargs):
Return a specialized column for the model field or `None`.

Arguments:
field (Model Field instance): the field that needs a suitable column
field (Model Field instance): the field that needs a suitable column.
**kwargs: passed on to the column.

Returns:
`.Column` object or `None`

Expand All @@ -429,7 +429,7 @@ class BoundColumn:
In practice, this means that a `.BoundColumn` knows the *"variable name"* given to the `.Column`
when it was declared on the `.Table`.

arguments:
Arguments:
table (`~.Table`): The table in which this column exists
column (`~.Column`): The type of column
name (str): The variable name of the column used when defining the
Expand Down Expand Up @@ -465,7 +465,6 @@ def attrs(self):
templates easier. ``tf`` is not actually a HTML tag, but this key name
will be used for attributes for column's footer, if the column has one.
"""

# prepare kwargs for computed_values()
kwargs = {"table": self._table, "bound_column": self}
# BoundRow.items() sets current_record and current_value when iterating over
Expand Down Expand Up @@ -503,25 +502,19 @@ def attrs(self):
return attrs

def _get_cell_class(self, attrs):
"""
Return a set of the classes from the class key in ``attrs``.
"""
"""Return a set of the classes from the class key in ``attrs``."""
classes = attrs.get("class", None)
classes = set() if classes is None else set(classes.split(" "))

return self._table.get_column_class_names(classes, self)

def get_td_class(self, td_attrs):
"""
Returns the HTML class attribute for a data cell in this column
"""
"""Return the HTML class attribute for a data cell in this column."""
classes = sorted(self._get_cell_class(td_attrs))
return None if len(classes) == 0 else " ".join(classes)

def get_th_class(self, th_attrs):
"""
Returns the HTML class attribute for a header cell in this column
"""
"""Return the HTML class attribute for a header cell in this column."""
classes = self._get_cell_class(th_attrs)

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

@property
def default(self):
"""Returns the default value for this column."""
"""Return the default value for this column."""
value = self.column.default
if value is None:
value = self._table.default
Expand Down Expand Up @@ -698,7 +691,7 @@ def visible(self):

@property
def localize(self):
"""Return `True`, `False` or `None` as described in ``Column.localize``"""
"""Return `True`, `False` or `None` as described in ``Column.localize``."""
return self.column.localize


Expand Down Expand Up @@ -742,10 +735,7 @@ def names(self):
return list(self.iternames())

def iterall(self):
"""
Return an iterator that exposes all `.BoundColumn` objects,
regardless of visibility or sortability.
"""
"""Return an iterator that exposes all `.BoundColumn` objects, regardless of visibility or sortability."""
return (column for name, column in self.iteritems())

def all(self):
Expand All @@ -759,7 +749,6 @@ def iteritems(self):
consideration all of the ordering and filtering modifiers that a table
supports (e.g. `~Table.Meta.exclude` and `~Table.Meta.sequence`).
"""

for name in self._table.sequence:
if name not in self._table.exclude:
yield (name, self.columns[name])
Expand All @@ -769,7 +758,7 @@ def items(self):

def iterorderable(self):
"""
Same as `BoundColumns.all` but only returns orderable columns.
`BoundColumns.all` filtered for whether they can be ordered.

This is useful in templates, where iterating over the full
set and checking ``{% if column.ordarable %}`` can be problematic in
Expand All @@ -780,7 +769,7 @@ def iterorderable(self):

def itervisible(self):
"""
Same as `.iterorderable` but only returns visible `.BoundColumn` objects.
Return `.iterorderable` filtered by visibility.

This is geared towards table rendering.
"""
Expand All @@ -805,7 +794,7 @@ def show(self, name):
self.columns[name].column.visible = True

def __iter__(self):
"""Convenience API, alias of `.itervisible`."""
"""Alias of `.itervisible` (for convenience)."""
return self.itervisible()

def __contains__(self, item):
Expand Down
4 changes: 1 addition & 3 deletions django_tables2/columns/booleancolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def render(self, value, record, bound_column):
return format_html("<span {}>{}</span>", AttributeDict(attrs).as_html(), escape(text))

def value(self, record, value, bound_column):
"""
Returns the content for a specific cell similarly to `.render` however without any html content.
"""
"""Return the content for a specific cell similarly to `.render` however without any html content."""
value = self._get_bool_value(record, value, bound_column)
return str(value)

Expand Down
4 changes: 1 addition & 3 deletions django_tables2/columns/checkboxcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def render(self, value, bound_column, record):
return mark_safe(f"<input {AttributeDict(attrs).as_html()} />")

def is_checked(self, value, record):
"""
Determine if the checkbox should be checked
"""
"""Determine if the checkbox should be checked."""
if self.checked is None:
return False
if self.checked is True:
Expand Down
2 changes: 1 addition & 1 deletion django_tables2/columns/datecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DateColumn(TemplateColumn):
def __init__(self, format=None, short=True, *args, **kwargs):
if format is None:
format = "SHORT_DATE_FORMAT" if short else "DATE_FORMAT"
template = '{{ value|date:"%s"|default:default }}' % format
template = '{{ value|date:"%s"|default:default }}' % format # noqa: UP031
super().__init__(template_code=template, *args, **kwargs)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion django_tables2/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DateTimeColumn(TemplateColumn):
def __init__(self, format=None, short=True, *args, **kwargs):
if format is None:
format = "SHORT_DATETIME_FORMAT" if short else "DATETIME_FORMAT"
template = '{{ value|date:"%s"|default:default }}' % format
template = '{{ value|date:"%s"|default:default }}' % format # noqa: UP031
super().__init__(template_code=template, *args, **kwargs)

@classmethod
Expand Down
7 changes: 2 additions & 5 deletions django_tables2/columns/linkcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def text_value(self, record, value):
return self.text(record) if callable(self.text) else self.text

def value(self, record, value):
"""
Returns the content for a specific cell similarly to `.render` however
without any html content.
"""
"""Return the content for a specific cell similarly to `.render` without any HTML content."""
return self.text_value(record, value)

def render(self, record, value):
Expand All @@ -38,7 +35,7 @@ def render(self, record, value):
@library.register
class LinkColumn(BaseLinkColumn):
"""
Renders a normal value as an internal hyperlink to another page.
Render a normal value as an internal hyperlink to another page.

.. note ::

Expand Down
11 changes: 3 additions & 8 deletions django_tables2/columns/manytomanycolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@library.register
class ManyToManyColumn(Column):
"""
Display the list of objects from a `ManyRelatedManager`
Display the list of objects from a `ManyRelatedManager`.

Ordering is disabled for this column.

Expand Down Expand Up @@ -72,16 +72,11 @@ def __init__(
self.linkify_item = LinkTransform(attrs=self.attrs.get("a", {}), **link_kwargs)

def transform(self, obj):
"""
Transform is applied to each item of the list of objects from the ManyToMany relation.
"""
"""Apply to each item of the list of objects from the ManyToMany relation."""
return force_str(obj)

def filter(self, qs):
"""
Filter is called on the ManyRelatedManager to allow ordering, filtering or limiting
on the set of related objects.
"""
"""Call on the ManyRelatedManager to allow ordering, filtering or limiting on the set of related objects."""
return qs.all()

def render(self, value):
Expand Down
8 changes: 4 additions & 4 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
@library.register
class TemplateColumn(Column):
"""
A subclass of `.Column` that renders some template code to use as
the cell value.
A subclass of `.Column` that renders some template code to use as the cell value.

Arguments:
template_code (str): template code to render
Expand Down Expand Up @@ -69,8 +68,9 @@ def render(self, record, table, value, bound_column, **kwargs):

def value(self, **kwargs):
"""
The value returned from a call to `value()` on a `TemplateColumn` is
the rendered template with `django.utils.html.strip_tags` applied.
Non-HTML value returned from a call to `value()` on a `TemplateColumn`.

By default this is the rendered template with `django.utils.html.strip_tags` applied.
Leading and trailing whitespace is stripped.
"""
html = super().value(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion django_tables2/columns/timecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TimeColumn(TemplateColumn):
def __init__(self, format=None, *args, **kwargs):
if format is None:
format = "TIME_FORMAT"
template = '{{ value|date:"%s"|default:default }}' % format
template = '{{ value|date:"%s"|default:default }}' % format # noqa: UP031
super().__init__(template_code=template, *args, **kwargs)

@classmethod
Expand Down
Loading