Skip to content

Commit 88f7317

Browse files
committed
Use a python 3.9-friendly union type syntax
1 parent 7b0254d commit 88f7317

11 files changed

+47
-24
lines changed

django_tables2/columns/base.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22
from itertools import islice
3+
from typing import Union
34

45
from django.core.exceptions import ImproperlyConfigured
56
from django.urls import reverse
@@ -23,7 +24,7 @@ class Library:
2324
def __init__(self):
2425
self.columns = []
2526

26-
def register(self, column):
27+
def register(self, column: "Column"):
2728
if not hasattr(column, "from_field"):
2829
raise ImproperlyConfigured(f"{column.__class__.__name__} is not a subclass of Column")
2930
self.columns.append(column)
@@ -68,7 +69,13 @@ class LinkTransform:
6869
accessor = None
6970
attrs = None
7071

71-
def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
72+
def __init__(
73+
self,
74+
url: Union[callable, None] = None,
75+
accessor: Union[str, Accessor, None] = None,
76+
attrs: Union[dict, None] = None,
77+
reverse_args: Union[list, tuple, None] = None,
78+
):
7279
"""
7380
arguments:
7481
url (callable): If supplied, the result of this callable will be used as ``href`` attribute.
@@ -95,7 +102,7 @@ def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
95102

96103
self.reverse_args = reverse_args or {}
97104

98-
def compose_url(self, **kwargs):
105+
def compose_url(self, **kwargs) -> str:
99106
if self.url and callable(self.url):
100107
return call_with_appropriate(self.url, kwargs)
101108

@@ -119,10 +126,8 @@ def compose_url(self, **kwargs):
119126
)
120127
return context.get_absolute_url()
121128

122-
def call_reverse(self, record):
123-
"""
124-
Prepares the arguments to reverse() for this record and calls reverse()
125-
"""
129+
def call_reverse(self, record) -> str:
130+
"""Prepares the arguments to reverse() for this record and calls reverse()."""
126131

127132
def resolve_if_accessor(val):
128133
return val.resolve(record) if isinstance(val, Accessor) else val
@@ -399,7 +404,7 @@ def order(self, queryset, is_descending):
399404
return (queryset, False)
400405

401406
@classmethod
402-
def from_field(cls, field, **kwargs) -> "Column | None":
407+
def from_field(cls, field, **kwargs) -> "Union[Column, None]":
403408
"""
404409
Return a specialized column for the model field or `None`.
405410

django_tables2/columns/booleancolumn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24
from django.utils.html import escape, format_html
35
from django.utils.safestring import SafeString
@@ -58,7 +60,7 @@ def value(self, record, value, bound_column) -> str:
5860
return str(self._get_bool_value(record, value, bound_column))
5961

6062
@classmethod
61-
def from_field(cls, field, **kwargs) -> "BooleanColumn | None":
63+
def from_field(cls, field, **kwargs) -> "Union[BooleanColumn, None]":
6264
if isinstance(field, models.NullBooleanField):
6365
return cls(null=True, **kwargs)
6466

django_tables2/columns/datecolumn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24

35
from .base import library
@@ -16,13 +18,13 @@ class DateColumn(TemplateColumn):
1618
``SHORT_DATE_FORMAT`` setting, otherwise use ``DATE_FORMAT``
1719
"""
1820

19-
def __init__(self, format=None, short=True, *args, **kwargs):
21+
def __init__(self, format: Union[str, None] = None, short: bool = True, *args, **kwargs):
2022
if format is None:
2123
format = "SHORT_DATE_FORMAT" if short else "DATE_FORMAT"
2224
template = '{{ value|date:"%s"|default:default }}' % format
2325
super().__init__(template_code=template, *args, **kwargs)
2426

2527
@classmethod
26-
def from_field(cls, field, **kwargs) -> "DateColumn | None":
28+
def from_field(cls, field, **kwargs) -> "Union[DateColumn, None]":
2729
if isinstance(field, models.DateField):
2830
return cls(**kwargs)

django_tables2/columns/datetimecolumn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24

35
from .base import library
@@ -16,13 +18,13 @@ class DateTimeColumn(TemplateColumn):
1618
``SHORT_DATETIME_FORMAT``, else ``DATETIME_FORMAT``
1719
"""
1820

19-
def __init__(self, format=None, short=True, *args, **kwargs):
21+
def __init__(self, format: Union[str, None] = None, short: bool = True, *args, **kwargs):
2022
if format is None:
2123
format = "SHORT_DATETIME_FORMAT" if short else "DATETIME_FORMAT"
2224
template = '{{ value|date:"%s"|default:default }}' % format
2325
super().__init__(template_code=template, *args, **kwargs)
2426

2527
@classmethod
26-
def from_field(cls, field, **kwargs) -> "DateTimeColumn | None":
28+
def from_field(cls, field, **kwargs) -> "Union[DateTimeColumn, None]":
2729
if isinstance(field, models.DateTimeField):
2830
return cls(**kwargs)

django_tables2/columns/emailcolumn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24

35
from .base import library
@@ -35,6 +37,6 @@ def get_url(self, value) -> str:
3537
return f"mailto:{value}"
3638

3739
@classmethod
38-
def from_field(cls, field, **kwargs) -> "EmailColumn | None":
40+
def from_field(cls, field, **kwargs) -> "Union[EmailColumn, None]":
3941
if isinstance(field, models.EmailField):
4042
return cls(**kwargs)

django_tables2/columns/filecolumn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import Union
23

34
from django.db import models
45
from django.utils.html import format_html
@@ -35,7 +36,7 @@ class FileColumn(BaseLinkColumn):
3536
the file's ``basename`` (default)
3637
"""
3738

38-
def __init__(self, verify_exists=True, **kwargs):
39+
def __init__(self, verify_exists: bool = True, **kwargs):
3940
self.verify_exists = verify_exists
4041
super().__init__(**kwargs)
4142

@@ -81,6 +82,6 @@ def render(self, record, value) -> SafeString:
8182
)
8283

8384
@classmethod
84-
def from_field(cls, field, **kwargs) -> "FileColumn | None":
85+
def from_field(cls, field, **kwargs) -> "Union[FileColumn, None]":
8586
if isinstance(field, models.FileField):
8687
return cls(**kwargs)

django_tables2/columns/jsoncolumn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from typing import Union
23

34
from django.db.models import JSONField
45
from django.utils.html import format_html
@@ -47,6 +48,6 @@ def render(self, record, value) -> SafeString:
4748
)
4849

4950
@classmethod
50-
def from_field(cls, field, **kwargs) -> "JSONColumn | None":
51+
def from_field(cls, field, **kwargs) -> "Union[JSONColumn, None]":
5152
if isinstance(field, (JSONField, HStoreField)):
5253
return cls(**kwargs)

django_tables2/columns/manytomanycolumn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24
from django.utils.encoding import force_str
35
from django.utils.html import conditional_escape
@@ -96,6 +98,6 @@ def render(self, value) -> SafeString:
9698
return mark_safe(conditional_escape(self.separator).join(items))
9799

98100
@classmethod
99-
def from_field(cls, field, **kwargs) -> "ManyToManyColumn | None":
101+
def from_field(cls, field, **kwargs) -> "Union[ManyToManyColumn, None]":
100102
if isinstance(field, models.ManyToManyField):
101103
return cls(**kwargs)

django_tables2/columns/timecolumn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24

35
from .base import library
@@ -14,13 +16,13 @@ class TimeColumn(TemplateColumn):
1416
short (bool): if *format* is not specified, use Django's ``TIME_FORMAT`` setting.
1517
"""
1618

17-
def __init__(self, format=None, *args, **kwargs):
19+
def __init__(self, format: Union[str, None] = None, *args, **kwargs):
1820
if format is None:
1921
format = "TIME_FORMAT"
2022
template = '{{ value|date:"%s"|default:default }}' % format
2123
super().__init__(template_code=template, *args, **kwargs)
2224

2325
@classmethod
24-
def from_field(cls, field, **kwargs) -> "TimeColumn | None":
26+
def from_field(cls, field, **kwargs) -> "Union[TimeColumn, None]":
2527
if isinstance(field, models.TimeField):
2628
return cls(**kwargs)

django_tables2/columns/urlcolumn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.db import models
24

35
from .base import library
@@ -28,6 +30,6 @@ def get_url(self, value: str) -> str:
2830
return value
2931

3032
@classmethod
31-
def from_field(cls, field, **kwargs) -> "URLColumn | None":
33+
def from_field(cls, field, **kwargs) -> "Union[URLColumn, None]":
3234
if isinstance(field, models.URLField):
3335
return cls(**kwargs)

django_tables2/paginators.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from django.core.paginator import EmptyPage, Page, PageNotAnInteger, Paginator
24
from django.utils.translation import gettext as _
35

@@ -62,7 +64,7 @@ def __init__(self, object_list, per_page, look_ahead=None, **kwargs):
6264

6365
super().__init__(object_list, per_page, **kwargs)
6466

65-
def validate_number(self, number: float | int) -> int:
67+
def validate_number(self, number: Union[float, int]) -> int:
6668
"""Validate the given 1-based page number."""
6769
try:
6870
if isinstance(number, float) and not number.is_integer():
@@ -74,7 +76,7 @@ def validate_number(self, number: float | int) -> int:
7476
raise EmptyPage(_("That page number is less than 1"))
7577
return number
7678

77-
def page(self, number: float | int) -> Page:
79+
def page(self, number: Union[float, int]) -> Page:
7880
# Number might be None, because the total number of pages is not known in this paginator.
7981
# If an unknown page is requested, serve the first page.
8082
number = self.validate_number(number or 1)
@@ -98,7 +100,7 @@ def page(self, number: float | int) -> Page:
98100
self._final_num_pages = number
99101
return Page(objects, number, self)
100102

101-
def is_last_page(self, number: float | int) -> bool:
103+
def is_last_page(self, number: Union[float, int]) -> bool:
102104
return number == self._final_num_pages
103105

104106
def _get_count(self):

0 commit comments

Comments
 (0)