Skip to content

Commit

Permalink
Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Dec 23, 2024
1 parent 5041d7a commit 3477d30
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 79 deletions.
38 changes: 6 additions & 32 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
repos:
- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black
language_version: python3.11

<<<<<<< HEAD
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.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]

=======
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.272
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
>>>>>>> 7b39760 (Adopt ruff to replace isort, flake8)
19 changes: 10 additions & 9 deletions django_tables2/columns/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import OrderedDict
from itertools import islice
from typing import Union

from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse
Expand Down Expand Up @@ -36,6 +37,7 @@ def column_for_field(self, field, **kwargs):
Returns
-------
`..Column` object or `None`
"""
if field is None:
return self.columns[0](**kwargs)
Expand Down Expand Up @@ -71,6 +73,8 @@ class LinkTransform:

def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
"""
Attributes for the link tag in a cell.
Arguments:
---------
url (callable): If supplied, the result of this callable will be used as ``href`` attribute.
Expand All @@ -82,12 +86,13 @@ def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
These arguments will then be passed automatically.
reverse_args (dict, tuple): Arguments to ``django.urls.reverse()``. If dict, the arguments are assumed to be
keyword arguments to ``reverse()``, if tuple, a ``(viewname, args)`` or ``(viewname, kwargs)``.
"""
self.url = url
self.attrs = attrs
self.accessor = accessor

if isinstance(reverse_args, list | tuple):
if isinstance(reverse_args, Union[list, tuple]):
viewname, args = reverse_args
reverse_args = {"viewname": viewname}
reverse_args["kwargs" if isinstance(args, dict) else "args"] = args
Expand Down Expand Up @@ -234,6 +239,7 @@ class Blog(models.Model):
default behavior, and sort ascending on "first click". Defaults to `False`.
.. [1] The provided callable object must not expect to receive any arguments.
"""

# Tracks each time a Column instance is created. Used to retain order.
Expand Down Expand Up @@ -378,6 +384,7 @@ def order(self, queryset, is_descending):
Returns
-------
Tuple (QuerySet, boolean)
"""
return (queryset, False)

Expand All @@ -398,6 +405,7 @@ def from_field(cls, field, **kwargs):
If the column is not specialized for the given model field, it should return `None`. This gives other columns
the opportunity to do better.
If the column is specialized, it should return an properly configured instance of itself for the field.
"""
# Since this method is inherited by every subclass, only provide a
# column if this class was asked directly.
Expand Down Expand Up @@ -442,14 +450,6 @@ def __str__(self):
return str(self.header)

@property
<<<<<<< HEAD
=======
def accessor(self):
"""Return the string used to access data for this column out of the data source."""
return self.column.accessor or Accessor(self.name)

@property
>>>>>>> 7b39760 (Adopt ruff to replace isort, flake8)
def attrs(self):
"""
Proxy to `.Column.attrs` but injects some values of our own.
Expand Down Expand Up @@ -709,6 +709,7 @@ class BoundColumns:
Arguments:
---------
table (`.Table`): the table containing the columns
"""

def __init__(self, table, base_columns):
Expand Down
1 change: 1 addition & 0 deletions django_tables2/columns/booleancolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BooleanColumn(Column):
available:
- ``span`` -- adds attributes to the ``<span>`` tag
"""

def __init__(self, null=False, yesno="✔,✘", **kwargs):
Expand Down
1 change: 1 addition & 0 deletions django_tables2/columns/checkboxcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CheckBoxColumn(Column):
rendered table and then *do something* with that. This functionality
is not implemented. If you want something to actually happen, you will
need to implement that yourself.
"""

def __init__(self, attrs=None, checked=None, **extra):
Expand Down
3 changes: 2 additions & 1 deletion django_tables2/columns/datecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class DateColumn(TemplateColumn):
filter (optional)
short (bool): if `format` is not specified, use Django's
``SHORT_DATE_FORMAT`` setting, otherwise use ``DATE_FORMAT``
"""

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
3 changes: 2 additions & 1 deletion django_tables2/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class DateTimeColumn(TemplateColumn):
Note that *format* uses Django's `date` template tag syntax.
short (bool): if `format` is not specified, use Django's
``SHORT_DATETIME_FORMAT``, else ``DATETIME_FORMAT``
"""

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
1 change: 1 addition & 0 deletions django_tables2/columns/emailcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PeopleTable(tables.Table):
# result
# [...]<a href="mailto:[email protected]">[email protected]</a>
"""

def get_url(self, value):
Expand Down
1 change: 1 addition & 0 deletions django_tables2/columns/filecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class FileColumn(BaseLinkColumn):
text (str or callable): Either static text, or a callable. If set, this
will be used to render the text inside the link instead of
the file's ``basename`` (default)
"""

def __init__(self, verify_exists=True, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions django_tables2/columns/linkcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BaseLinkColumn(Column):
The callable gets the record being rendered as argument.
attrs (dict): In addition to ``attrs`` keys supported by `~.Column`, the following are available:
- `a` -- ``<a>`` in ``<td>`` elements.
"""

def __init__(self, text=None, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ExampleTable(tables.Table):
extra_context={"label": "Label"})
Both columns will have the same output.
"""

empty_values = ()
Expand Down
3 changes: 2 additions & 1 deletion django_tables2/columns/timecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class TimeColumn(TemplateColumn):
---------
format (str): format string in same format as Django's ``time`` template filter (optional).
short (bool): if *format* is not specified, use Django's ``TIME_FORMAT`` setting.
"""

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
1 change: 1 addition & 0 deletions django_tables2/columns/urlcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class URLColumn(BaseLinkColumn):
>>> table = CompaniesTable([{"link": "http://google.com"}])
>>> table.rows[0].get_cell("link")
'<a href="http://google.com">http://google.com</a>'
"""

def get_url(self, value):
Expand Down
1 change: 1 addition & 0 deletions django_tables2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def configure(self, table):
Arguments:
---------
table (`~.Table`): table to be configured
"""
table.request = self.request

Expand Down
2 changes: 2 additions & 0 deletions django_tables2/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def order_by(self, aliases):
---------
aliases (`~.utils.OrderByTuple`): optionally prefixed names of columns ('-' indicates descending order) in
order of significance with regard to data ordering.
"""
accessors = []
for alias in aliases:
Expand Down Expand Up @@ -180,6 +181,7 @@ def order_by(self, aliases):
---------
aliases (`~.utils.OrderByTuple`): optionally prefixed names of columns ('-' indicates descending order) in
order of significance with regard to data ordering.
"""
modified_any = False
accessors = []
Expand Down
1 change: 1 addition & 0 deletions django_tables2/export/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def response(self, filename=None):
Arguments:
---------
filename (str): if not `None`, the filename is attached to the `Content-Disposition` header of the response.
"""
response = HttpResponse(content_type=self.content_type())
if filename is not None:
Expand Down
1 change: 1 addition & 0 deletions django_tables2/export/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Table(tables.Table):
dataset_kwargs (dictionary): passed as `**kwargs` to `tablib.Dataset` constructor::
dataset_kwargs = {"tite": "My custom tab title"}
"""

export_class = TableExport
Expand Down
4 changes: 4 additions & 0 deletions django_tables2/rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def get_even_odd_css_class(self):
Return:
------
string: `even` for even records, `odd` otherwise.
"""
return "odd" if self.row_counter % 2 else "even"

Expand Down Expand Up @@ -247,6 +248,7 @@ def attrs(self):
Return:
------
AttributeDict: Attributes for pinned rows.
"""
row_attrs = computed_values(self._table.pinned_row_attrs, kwargs={"record": self._record})
css_class = " ".join(
Expand Down Expand Up @@ -275,6 +277,7 @@ class BoundRows:
... }
This is used for `~.Table.rows`.
"""

def __init__(self, data, table, pinned_data=None):
Expand All @@ -293,6 +296,7 @@ def generator_pinned_row(self, data):
Yields:
------
BoundPinnedRow: Top or bottom `BoundPinnedRow` object for single pinned record.
"""
if data is not None:
if hasattr(data, "__iter__") is False:
Expand Down
10 changes: 10 additions & 0 deletions django_tables2/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class TableOptions:
Arguments:
---------
options (`.Table.Meta`): options for a table from `.Table.Meta`
"""

def __init__(self, options, class_name):
Expand Down Expand Up @@ -250,6 +251,7 @@ class Table(metaclass=DeclarativeColumnsMetaclass):
extra_columns (str, `.Column`): list of `(name, column)`-tuples containing
extra columns to add to the instance. If `column` is `None`, the column
with `name` will be removed from the table.
"""

def __init__(
Expand Down Expand Up @@ -393,6 +395,7 @@ def get_top_pinned_data(self):
... "column_a" : "some value",
... "column_c" : "other value",
... }]
"""
return None

Expand All @@ -419,6 +422,7 @@ def get_bottom_pinned_data(self):
... "column_a" : "some value",
... "column_c" : "other value",
... }]
"""
return None

Expand All @@ -444,6 +448,7 @@ def before_render(self, request):
self.columns.hide('country')
else:
self.columns.show('country')
"""
pass

Expand Down Expand Up @@ -486,6 +491,7 @@ def value_name(self, value):
the value when `as_values()` is called.
Note that any invisible columns will be part of the row iterator.
"""
if exclude_columns is None:
exclude_columns = ()
Expand Down Expand Up @@ -527,6 +533,7 @@ def order_by(self, value):
Arguments:
---------
value: iterable or comma separated string of order by aliases.
"""
# collapse empty values to ()
order_by = () if not value else value
Expand Down Expand Up @@ -577,6 +584,7 @@ def paginate(self, paginator_class=Paginator, per_page=None, page=1, *args, **kw
Pagination exceptions (`~django.core.paginator.EmptyPage` and `~django.core.paginator.PageNotAnInteger`) may be
raised from this method and should be handled by the caller.
"""
per_page = per_page or self._meta.per_page
self.paginator = paginator_class(self.rows, per_page, *args, **kwargs)
Expand Down Expand Up @@ -685,6 +693,7 @@ def get_column_class_names(self, classes_set, bound_column):
classes_set.add(bound_column.name)
return classes_set
"""
return classes_set

Expand All @@ -706,6 +715,7 @@ class Meta:
fields (list of str): Fields displayed in tables
exclude (list of str): Fields exclude in tables
localize (list of str): Fields to localize
"""
attrs = {"model": model}
if fields is not None:
Expand Down
Loading

0 comments on commit 3477d30

Please sign in to comment.