Skip to content

Commit 3dbd165

Browse files
committed
More typing
1 parent e7ef9c6 commit 3dbd165

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

django_tables2/columns/base.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import OrderedDict
22
from collections.abc import Callable
33
from itertools import islice
4-
from typing import Union
4+
from typing import TYPE_CHECKING, Any, Union
55

66
from django.core.exceptions import ImproperlyConfigured
77
from django.urls import reverse
@@ -18,6 +18,12 @@
1818
computed_values,
1919
)
2020

21+
if TYPE_CHECKING:
22+
from django.db.models import QuerySet
23+
from django.utils.safestring import SafeString
24+
25+
from ..tables import Table
26+
2127

2228
class Library:
2329
"""A collection of columns."""
@@ -325,14 +331,12 @@ def default(self):
325331
return self._default() if callable(self._default) else self._default
326332

327333
@property
328-
def header(self):
334+
def header(self) -> Union[str, None]:
329335
"""
330336
The value used for the column heading (e.g. inside the ``<th>`` tag).
331337
332338
By default this returns `~.Column.verbose_name`.
333339
334-
:returns: `unicode` or `None`
335-
336340
.. note::
337341
338342
This property typically is not accessed directly when a table is
@@ -344,7 +348,7 @@ def header(self):
344348
"""
345349
return self.verbose_name
346350

347-
def footer(self, bound_column, table):
351+
def footer(self, bound_column: "BoundColumn", table: "Table") -> Union[str, None]:
348352
"""Return the content of the footer, if specified."""
349353
footer_kwargs = {"column": self, "bound_column": bound_column, "table": table}
350354

@@ -359,7 +363,7 @@ def footer(self, bound_column, table):
359363

360364
return ""
361365

362-
def render(self, value):
366+
def render(self, value: Any) -> Any:
363367
"""
364368
Return the content for a specific cell.
365369
@@ -373,34 +377,29 @@ def render(self, value):
373377
"""
374378
return value
375379

376-
def value(self, **kwargs):
380+
def value(self, **kwargs) -> Any:
377381
"""
378382
Return the content for a specific cell for exports.
379383
380-
Similar to `.render` but without any html content.
384+
Similar to `.render` but without any HTML content.
381385
This can be used to get the data in the formatted as it is presented but in a
382-
form that could be added to a csv file.
386+
form that could be added to a CSV file.
383387
384388
The default implementation just calls the `render` function but any
385-
subclasses where `render` returns html content should override this
389+
subclasses where `render` returns HTML content should override this
386390
method.
387391
388392
See `LinkColumn` for an example.
389393
"""
390-
value = call_with_appropriate(self.render, kwargs)
394+
return call_with_appropriate(self.render, kwargs)
391395

392-
return value
393-
394-
def order(self, queryset, is_descending):
396+
def order(self, queryset: "QuerySet", is_descending: bool) -> "tuple[QuerySet, bool]":
395397
"""
396398
Order the QuerySet of the table.
397399
398400
This method can be overridden by :ref:`table.order_FOO` methods on the
399401
table or by subclassing `.Column`; but only overrides if second element
400402
in return tuple is True.
401-
402-
returns:
403-
Tuple (QuerySet, boolean)
404403
"""
405404
return (queryset, False)
406405

@@ -446,7 +445,7 @@ class SimpleTable(tables.Table):
446445
447446
"""
448447

449-
def __init__(self, table, column, name):
448+
def __init__(self, table: "Table", column: Column, name: str):
450449
self._table = table
451450
self.column = column
452451
self.name = name
@@ -458,11 +457,11 @@ def __init__(self, table, column, name):
458457

459458
self.current_value = None
460459

461-
def __str__(self):
460+
def __str__(self) -> str:
462461
return str(self.header)
463462

464463
@property
465-
def attrs(self):
464+
def attrs(self) -> dict:
466465
"""
467466
Proxy to `.Column.attrs` but injects some values of our own.
468467
@@ -642,18 +641,18 @@ def order_by_alias(self):
642641
return order_by
643642

644643
@property
645-
def is_ordered(self):
644+
def is_ordered(self) -> bool:
646645
return self.name in (self._table.order_by or ())
647646

648647
@property
649-
def orderable(self):
648+
def orderable(self) -> bool:
650649
"""Return whether this column supports ordering."""
651650
if self.column.orderable is not None:
652651
return self.column.orderable
653652
return self._table.orderable
654653

655654
@property
656-
def verbose_name(self):
655+
def verbose_name(self) -> Union[str, SafeString]:
657656
"""
658657
Return the verbose name for this column.
659658

0 commit comments

Comments
 (0)