Skip to content

Commit

Permalink
[typing] add type hints to bootstrap table code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 4, 2023
1 parent 4063fc8 commit 2e4e5be
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 164 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module = [
"pycroft.helpers.utc",
"web.blueprints",
"web.blueprints.*",
"web.table.table",
]
disallow_untyped_defs = true
disallow_untyped_calls = true
Expand Down
9 changes: 2 additions & 7 deletions web/blueprints/finance/tables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing
import typing as t
from decimal import Decimal

Expand All @@ -10,7 +9,6 @@
from web.table.lazy_join import HasDunderStr, LazilyJoined
from web.table.table import (
lazy_join,
DictValueMixin,
custom_formatter_column,
IbanColumn,
DateColResponse,
Expand All @@ -25,11 +23,8 @@


@custom_formatter_column('table.coloredFormatter')
class ColoredColumn(DictValueMixin, Column):
if typing.TYPE_CHECKING:
@classmethod
def value(cls, value: str, is_positive: bool) -> dict: # type: ignore[override]
...
class ColoredColumn(Column):
pass


class ColoredColResponse(BaseModel):
Expand Down
13 changes: 0 additions & 13 deletions web/blueprints/user/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,6 @@ class Meta:
current_properties = Column("Props", formatter="table.propertiesFormatter")
end_of_membership = DateColumn("EOM")

if typing.TYPE_CHECKING:
@classmethod
def row( # type: ignore[override]
cls,
id: int,
user: dict,
room_shortname: dict,
current_properties: str,
num_hosts: int,
end_of_membership: dict,
) -> dict:
...


class ArchivableMemberRow(BaseModel):
id: int
Expand Down
36 changes: 26 additions & 10 deletions web/table/lazy_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from typing import Generator, Callable, overload


def filled_iter(iter, filler):
_T = t.TypeVar("_T")


def filled_iter(iter: t.Iterable[_T], filler: _T) -> t.Iterator[_T]:
"""Inbetween every iteration, yield a constant filler."""
first = True
for elem in iter:
Expand Down Expand Up @@ -34,7 +37,7 @@ def __init__(
self,
components: t.Iterable[HasDunderStr | None],
glue: str = "",
):
) -> None:
self.glue = glue
self._components = components
self.exhausted = False
Expand All @@ -61,32 +64,45 @@ def _mark_exhausted(self) -> None:
self.exhausted = True


_P = t.ParamSpec("_P")
DecoratedInType = Callable[
..., Generator[HasDunderStr | None, None, None] | t.Iterator[HasDunderStr | None]
_P,
Generator[HasDunderStr | None, None, None]
| t.Iterator[HasDunderStr | None]
| t.Iterator[HasDunderStr],
]
DecoratedOutType = Callable[..., LazilyJoined]
DecoratedOutType = Callable[_P, LazilyJoined]

@overload
def lazy_join(func_or_glue: DecoratedInType) -> DecoratedOutType:
...


@overload
def lazy_join(func_or_glue: str) -> Callable[[DecoratedInType], DecoratedOutType]:
...
def lazy_join(func_or_glue):


def lazy_join(
func_or_glue: str | DecoratedInType,
) -> DecoratedOutType | Callable[[DecoratedInType], DecoratedOutType]:
if type(func_or_glue) == FunctionType:
# Return the wrapped function
return LazyJoinDecorator()(func=func_or_glue)
return LazyJoinDecorator()(func=t.cast(DecoratedInType, func_or_glue))
# Return the decorator
return LazyJoinDecorator(glue=func_or_glue)
return LazyJoinDecorator(glue=t.cast(str, func_or_glue))


class LazyJoinDecorator:
def __init__(self, glue: str = ""):
def __init__(self, glue: str = "") -> None:
self.glue = glue

def __call__(self, func: DecoratedInType) -> DecoratedOutType:
@wraps(func)
def wrapped(*a, **kw):
# error: Argument 1 to "__call__" of "IdentityFunction" has incompatible type
# "Callable[_P, LazilyJoined]"; expected "Callable[_P, LazilyJoined]" [arg-type]
# …go home mypy, you're drunk!
@wraps(func) # type: ignore[arg-type]
def wrapped(*a: _P.args, **kw: _P.kwargs) -> LazilyJoined:
return LazilyJoined(func(*a, **kw), glue=self.glue)

return wrapped
Loading

0 comments on commit 2e4e5be

Please sign in to comment.