Skip to content

Commit

Permalink
Adopt ruff to replace isort, flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Oct 17, 2023
1 parent 44fce19 commit 7b39760
Show file tree
Hide file tree
Showing 55 changed files with 467 additions and 566 deletions.
19 changes: 4 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,8 @@ repos:
- id: black
language_version: python3.11

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.272
hooks:
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort

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

- id: ruff
args: [--fix, --exit-non-zero-on-fix]
244 changes: 102 additions & 142 deletions django_tables2/columns/base.py

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions django_tables2/columns/booleancolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BooleanColumn(Column):
A column suitable for rendering boolean data.
Arguments:
---------
null (bool): is `None` different from `False`?
yesno (str): comma separated values string or 2-tuple to display for
True/False values.
Expand Down Expand Up @@ -52,9 +53,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
5 changes: 2 additions & 3 deletions django_tables2/columns/checkboxcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CheckBoxColumn(Column):
- ``orderable`` defaults to `False`.
Arguments:
---------
attrs (dict): In addition to *attrs* keys supported by `~.Column`, the
following are available:
Expand Down Expand Up @@ -71,9 +72,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
1 change: 1 addition & 0 deletions django_tables2/columns/datecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DateColumn(TemplateColumn):
A column that renders dates in the local timezone.
Arguments:
---------
format (str): format string in same format as Django's ``date`` template
filter (optional)
short (bool): if `format` is not specified, use Django's
Expand Down
1 change: 1 addition & 0 deletions django_tables2/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DateTimeColumn(TemplateColumn):
A column that renders `datetime` instances in the local timezone.
Arguments:
---------
format (str): format string for datetime (optional).
Note that *format* uses Django's `date` template tag syntax.
short (bool): if `format` is not specified, use Django's
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 @@ -10,6 +10,7 @@ class EmailColumn(BaseLinkColumn):
Render email addresses to `mailto:`-links.
Arguments:
---------
attrs (dict): HTML attributes that are added to the rendered
``<a href="...">...</a>`` tag.
text: Either static text, or a callable. If set, this will be used to
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 @@ -26,6 +26,7 @@ class FileColumn(BaseLinkColumn):
`.Column.attrs` keys ``a`` and ``span`` can be used to add additional attributes.
Arguments:
---------
verify_exists (bool): attempt to determine if the file exists
If *verify_exists*, the HTML class ``exists`` or ``missing`` is
added to the element to indicate the integrity of the storage.
Expand Down
3 changes: 2 additions & 1 deletion django_tables2/columns/jsoncolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class JSONColumn(BaseLinkColumn):
used manually without it.
Arguments:
---------
json_dumps_kwargs: kwargs passed to `json.dumps`, defaults to `{'indent': 2}`
attrs (dict): In addition to *attrs* keys supported by `~.Column`, the
following are available:
Expand All @@ -60,5 +61,5 @@ def render(self, record, value):
@classmethod
def from_field(cls, field, **kwargs):
if POSTGRES_AVAILABLE:
if isinstance(field, (JSONField, HStoreField)):
if isinstance(field, JSONField | HStoreField):
return cls(**kwargs)
20 changes: 8 additions & 12 deletions django_tables2/columns/linkcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ class BaseLinkColumn(Column):
The base for other columns that render links.
Arguments:
text (str or callable): If set, this value will be used to render the
text inside link instead of value. 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.
---------
text (str or callable): If set, this value will be used to render the text inside link instead of value.
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 All @@ -25,10 +23,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` however without any html content."""
return self.text_value(record, value)

def render(self, record, value):
Expand Down Expand Up @@ -56,6 +51,7 @@ class LinkColumn(BaseLinkColumn):
rendered ``<a href="...">`` tag.
Arguments:
---------
viewname (str or None): See `~django.urls.reverse`, or use `None`
to use the model's `get_absolute_url`
urlconf (str): See `~django.urls.reverse`.
Expand All @@ -74,7 +70,7 @@ class LinkColumn(BaseLinkColumn):
`~django.urls.reverse` is called.
Example:
-------
.. code-block:: python
# models.py
Expand Down
9 changes: 4 additions & 5 deletions django_tables2/columns/manytomanycolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
@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.
Arguments:
---------
transform: callable to transform each item to text, it gets an item as argument
and must return a string-like representation of the item.
By default, it calls `~django.utils.force_str` on each item.
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__(
link_kwargs = None
if callable(linkify_item):
link_kwargs = dict(url=linkify_item)
elif isinstance(linkify_item, (dict, tuple)):
elif isinstance(linkify_item, dict | tuple):
link_kwargs = dict(reverse_args=linkify_item)
elif linkify_item is True:
link_kwargs = dict()
Expand All @@ -71,9 +72,7 @@ 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.
"""
"""Transform is applied to each item of the list of objects from the ManyToMany relation."""
return force_str(obj)

def filter(self, qs):
Expand Down
16 changes: 9 additions & 7 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class TemplateColumn(Column):
the cell value.
Arguments:
template_code (str): template code to render
template_name (str): name of the template to render
extra_context (dict): optional extra template context
---------
template_code (str): template code to render
template_name (str): name of the template to render
extra_context (dict): optional extra template context
A `~django.template.Template` object is created from the
*template_code* or *template_name* and rendered with a context containing:
Expand All @@ -26,7 +27,7 @@ class TemplateColumn(Column):
- any context variables passed using the `extra_context` argument to `TemplateColumn`.
Example:
-------
.. code-block:: python
class ExampleTable(tables.Table):
Expand Down Expand Up @@ -69,9 +70,10 @@ 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.
Leading and trailing whitespace is stripped.
Value of this column or exports.
The value returned from a call to `value()` on a `TemplateColumn` is the rendered template with
`django.utils.html.strip_tags` applied. Leading and trailing whitespace is stripped.
"""
html = super().value(**kwargs)
return strip_tags(html).strip() if isinstance(html, str) else html
1 change: 1 addition & 0 deletions django_tables2/columns/timecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TimeColumn(TemplateColumn):
A column that renders times in the local timezone.
Arguments:
---------
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.
"""
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 @@ -10,6 +10,7 @@ class URLColumn(BaseLinkColumn):
Renders URL values as hyperlinks.
Arguments:
---------
text (str or callable): Either static text, or a callable. If set, this
will be used to render the text inside link instead of value (default)
attrs (dict): Additional attributes for the ``<a>`` tag
Expand Down
4 changes: 3 additions & 1 deletion django_tables2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class RequestConfig:
A single RequestConfig can be used for multiple tables in one view.
Arguments:
---------
paginate (dict or bool): Indicates whether to paginate, and if so, what
default values to use. If the value evaluates to `False`, pagination
will be disabled. A `dict` can be used to specify default values for
Expand Down Expand Up @@ -35,7 +36,8 @@ def configure(self, table):
Configure a table using information from the request.
Arguments:
table (`~.Table`): table to be configured
---------
table (`~.Table`): table to be configured
"""
table.request = self.request

Expand Down
Loading

0 comments on commit 7b39760

Please sign in to comment.