Skip to content

Commit

Permalink
[typing] Remove superfluous sqlalchemy-related typing suppressions
Browse files Browse the repository at this point in the history
The most important change was the type hinting of sqlalchemy's
`functions` module[1], which allowed us to have properly type-hinted
`over()` calls.

[1] sqlalchemy/sqlalchemy@045732a
  • Loading branch information
lukasjuhrich committed Jan 25, 2024
1 parent 1f65342 commit 3a7eebc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
6 changes: 2 additions & 4 deletions pycroft/lib/finance/membership_fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,12 @@ def post_transactions_for_membership_fee(
).fetchall()

if not simulate:
# `over` not typed yet,
# see https://github.com/sqlalchemy/sqlalchemy/issues/6810
numbered_users = (
select(
users.c.id,
users.c.fee_account_id.label("fee_account_id"),
users.c.account_id,
func.row_number().over().label("index"), # type: ignore[no-untyped-call]
func.row_number().over().label("index"),
)
.select_from(users)
.cte("membership_fee_numbered_users")
Expand Down Expand Up @@ -289,7 +287,7 @@ def post_transactions_for_membership_fee(
numbered_transactions = (
select(
transactions.c.id,
func.row_number().over().label("index"), # type: ignore[no-untyped-call]
func.row_number().over().label("index"),
)
.select_from(transactions)
.cte("membership_fee_numbered_transactions")
Expand Down
22 changes: 15 additions & 7 deletions pycroft/lib/user_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
This module contains methods concerning user archival and deletion.
"""
from __future__ import annotations
import typing as t
from datetime import timedelta, datetime
from typing import Protocol, Sequence

Expand All @@ -24,6 +25,16 @@ class ArchivableMemberInfo(Protocol):
mem_end: datetime


TP = t.TypeVar("TP")
TO = t.TypeVar("TO")


# mrh, not available in py3.10…
class _WindowArgs(t.TypedDict, t.Generic[TP, TO]):
partition_by: TP
order_by: TO


def get_archivable_members(session: Session) -> Sequence[ArchivableMemberInfo]:
"""Return all the users that qualify for being archived right now.
Expand All @@ -33,28 +44,25 @@ def get_archivable_members(session: Session) -> Sequence[ArchivableMemberInfo]:
"""
# see FunctionElement.over
mem_ends_at = func.upper(Membership.active_during)
window_args = {
window_args: _WindowArgs = {
'partition_by': User.id,
'order_by': nulls_last(mem_ends_at),
}
# mypy: ignore[no-untyped-call]
last_mem = (
select(
User.id.label('user_id'),
func.last_value(Membership.id)
.over(**window_args, rows=(None, None)) # type: ignore[no-untyped-call]
.over(**window_args, rows=(None, None))
.label("mem_id"),
func.last_value(mem_ends_at)
.over(**window_args, rows=(None, None)) # type: ignore[no-untyped-call]
.over(**window_args, rows=(None, None))
.label("mem_end"),
)
.select_from(User)
.distinct()
.join(Membership)
.join(Config, Config.member_group_id == Membership.group_id)
).cte(
"last_mem"
) # mypy: ignore[no-untyped-call]
).cte("last_mem")
stmt = (
select(
User,
Expand Down
2 changes: 1 addition & 1 deletion web/blueprints/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def balance_json(account_id: int) -> ResponseReturnValue:

sum_exp: ColumnElement[int] = t.cast(
Over[int],
func.sum(Split.amount).over(order_by=Transaction.valid_on), # type: ignore[no-untyped-call]
func.sum(Split.amount).over(order_by=Transaction.valid_on),
)

if invert:
Expand Down

0 comments on commit 3a7eebc

Please sign in to comment.