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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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]

244 changes: 106 additions & 138 deletions django_tables2/columns/base.py

Large diffs are not rendered by default.

6 changes: 3 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 All @@ -22,6 +23,7 @@ class BooleanColumn(Column):
available:

- ``span`` -- adds attributes to the ``<span>`` tag

"""

def __init__(self, null=False, yesno="✔,✘", **kwargs):
Expand Down Expand Up @@ -52,9 +54,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
6 changes: 3 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 All @@ -42,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 Expand Up @@ -71,9 +73,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
4 changes: 3 additions & 1 deletion django_tables2/columns/datecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ 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
``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
4 changes: 3 additions & 1 deletion django_tables2/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ 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
``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
2 changes: 2 additions & 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 All @@ -29,6 +30,7 @@ class PeopleTable(tables.Table):

# result
# [...]<a href="mailto:[email protected]">[email protected]</a>

"""

def get_url(self, value):
Expand Down
2 changes: 2 additions & 0 deletions django_tables2/columns/filecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ 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.
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/jsoncolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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 Down
19 changes: 8 additions & 11 deletions django_tables2/columns/linkcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ 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:
---------
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.

- `a` -- ``<a>`` in ``<td>`` elements.
"""

def __init__(self, text=None, *args, **kwargs):
Expand All @@ -25,10 +24,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 +52,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 +71,7 @@ class LinkColumn(BaseLinkColumn):
`~django.urls.reverse` is called.

Example:

-------
.. code-block:: python

# models.py
Expand Down
7 changes: 3 additions & 4 deletions django_tables2/columns/manytomanycolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,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 @@ -72,9 +73,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
17 changes: 10 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 All @@ -36,6 +37,7 @@ class ExampleTable(tables.Table):
extra_context={"label": "Label"})

Both columns will have the same output.

"""

empty_values = ()
Expand Down Expand Up @@ -69,9 +71,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
4 changes: 3 additions & 1 deletion django_tables2/columns/timecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ 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.

"""

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
2 changes: 2 additions & 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 All @@ -22,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
5 changes: 4 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,9 @@ 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
Loading