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

Make primary key typing stricter #904

Merged
merged 7 commits into from
Sep 10, 2024
Merged
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
3 changes: 1 addition & 2 deletions backend/bracket/cronjobs/scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from bracket.sql.users import delete_user_and_owned_clubs, get_expired_demo_users
from bracket.utils.asyncio import AsyncioTasksManager
from bracket.utils.logging import logger
from bracket.utils.types import assert_some

CronjobT = Callable[[], Awaitable[None]]

Expand All @@ -21,7 +20,7 @@ async def delete_demo_accounts() -> None:

for demo_user in demo_users:
assert demo_user.account_type is UserAccountType.DEMO
user_id = assert_some(demo_user.id)
user_id = demo_user.id

await delete_user_and_owned_clubs(user_id)

Expand Down
8 changes: 4 additions & 4 deletions backend/bracket/logic/planning/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def schedule_all_unscheduled_matches(tournament_id: TournamentId) -> None:
for match in round_.matches:
if match.start_time is None and match.position_in_schedule is None:
await sql_reschedule_match_and_determine_duration_and_margin(
assert_some(match.id),
match.id,
court.id,
start_time,
position_in_schedule,
Expand All @@ -63,7 +63,7 @@ async def schedule_all_unscheduled_matches(tournament_id: TournamentId) -> None:

if match.start_time is None and match.position_in_schedule is None:
await sql_reschedule_match_and_determine_duration_and_margin(
assert_some(match.id),
match.id,
courts[-1].id,
start_time,
position_in_schedule,
Expand Down Expand Up @@ -92,7 +92,7 @@ async def reorder_matches_for_court(
last_start_time = tournament.start_time
for i, match_pos in enumerate(matches_this_court):
await sql_reschedule_match_and_determine_duration_and_margin(
assert_some(match_pos.match.id),
match_pos.match.id,
court_id,
last_start_time,
position_in_schedule=i,
Expand Down Expand Up @@ -151,7 +151,7 @@ async def update_start_times_of_matches(tournament_id: TournamentId) -> None:
scheduled_matches = get_scheduled_matches(stages)

for court in courts:
await reorder_matches_for_court(tournament, scheduled_matches, assert_some(court.id))
await reorder_matches_for_court(tournament, scheduled_matches, court.id)


def get_scheduled_matches(stages: list[StageWithStageItems]) -> list[MatchPosition]:
Expand Down
14 changes: 5 additions & 9 deletions backend/bracket/logic/planning/rounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
active_round = next((round_ for round_ in stage_item.rounds if round_.is_active), None)

def is_round_in_future(round_: RoundWithMatches) -> bool:
return (
(assert_some(round_.id) > assert_some(active_round.id))
if active_round is not None
else True
)
return (round_.id > active_round.id) if active_round is not None else True

rounds_chronologically_sorted = sorted(stage_item.rounds, key=lambda r: assert_some(r.id))
rounds_chronologically_sorted = sorted(stage_item.rounds, key=lambda r: r.id)
next_round = next(
(round_ for round_ in rounds_chronologically_sorted if is_round_in_future(round_)),
None,
Expand All @@ -53,7 +49,7 @@
assert len(active_round.matches) <= len(courts)

for i, match in enumerate(active_round.matches):
court_id = assert_some(courts[i].id)
court_id = courts[i].id

Check warning on line 52 in backend/bracket/logic/planning/rounds.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/planning/rounds.py#L52

Added line #L52 was not covered by tests
last_match = (
next((m for m in matches_per_court[court_id][::-1] if m.match.id != match.id), None)
if court_id in matches_per_court
Expand Down Expand Up @@ -83,7 +79,7 @@
)
rescheduling_operations.append(
sql_reschedule_match_and_determine_duration_and_margin(
assert_some(last_match.match.id),
last_match.match.id,
court_id,
assert_some(last_match.match.start_time),
assert_some(last_match.match.position_in_schedule),
Expand All @@ -104,7 +100,7 @@

rescheduling_operations.append(
sql_reschedule_match_and_determine_duration_and_margin(
assert_some(match.id), court_id, start_time, pos_in_schedule, match, tournament
match.id, court_id, start_time, pos_in_schedule, match, tournament
)
)

Expand Down
16 changes: 9 additions & 7 deletions backend/bracket/logic/scheduling/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
build_round_robin_stage_item,
get_number_of_rounds_to_create_round_robin,
)
from bracket.models.db.round import RoundToInsert
from bracket.models.db.round import RoundInsertable
from bracket.models.db.stage_item import StageItem, StageType
from bracket.models.db.stage_item_inputs import (
StageItemInputOptionFinal,
Expand All @@ -19,7 +19,7 @@
from bracket.sql.rounds import get_next_round_name, sql_create_round
from bracket.sql.stage_items import get_stage_item
from bracket.utils.id_types import StageId, TournamentId
from bracket.utils.types import assert_some
from tests.integration_tests.mocks import MOCK_NOW


async def create_rounds_for_new_stage_item(
Expand All @@ -38,16 +38,18 @@ async def create_rounds_for_new_stage_item(

for _ in range(rounds_count):
await sql_create_round(
RoundToInsert(
stage_item_id=assert_some(stage_item.id),
name=await get_next_round_name(tournament_id, assert_some(stage_item.id)),
RoundInsertable(
created=MOCK_NOW,
is_draft=False,
stage_item_id=stage_item.id,
name=await get_next_round_name(tournament_id, stage_item.id),
),
)


async def build_matches_for_stage_item(stage_item: StageItem, tournament_id: TournamentId) -> None:
await create_rounds_for_new_stage_item(tournament_id, stage_item)
stage_item_with_rounds = await get_stage_item(tournament_id, assert_some(stage_item.id))
stage_item_with_rounds = await get_stage_item(tournament_id, stage_item.id)

if stage_item_with_rounds is None:
raise ValueError(
Expand All @@ -73,7 +75,7 @@ def determine_available_inputs(
teams: list[FullTeamWithPlayers],
stages: list[StageWithStageItems],
) -> list[StageItemInputOptionTentative | StageItemInputOptionFinal]:
results_team_ids = [assert_some(team.id) for team in teams]
results_team_ids = [team.id for team in teams]
results_tentative = []

for stage in stages:
Expand Down
9 changes: 4 additions & 5 deletions backend/bracket/logic/scheduling/elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from bracket.sql.rounds import get_rounds_for_stage_item
from bracket.sql.tournaments import sql_get_tournament
from bracket.utils.id_types import TournamentId
from bracket.utils.types import assert_some


def determine_matches_first_round(
Expand All @@ -18,7 +17,7 @@ def determine_matches_first_round(
second_input = stage_item.inputs[i + 1]
suggestions.append(
MatchCreateBody(
round_id=assert_some(round_.id),
round_id=round_.id,
court_id=None,
team1_id=first_input.team_id,
team1_winner_from_stage_item_id=first_input.winner_from_stage_item_id,
Expand Down Expand Up @@ -51,16 +50,16 @@ def determine_matches_subsequent_round(

suggestions.append(
MatchCreateBody(
round_id=assert_some(round_.id),
round_id=round_.id,
court_id=None,
team1_id=None,
team1_winner_from_stage_item_id=None,
team1_winner_position=None,
team2_id=None,
team2_winner_from_stage_item_id=None,
team2_winner_position=None,
team1_winner_from_match_id=assert_some(first_match.id),
team2_winner_from_match_id=assert_some(second_match.id),
team1_winner_from_match_id=first_match.id,
team2_winner_from_match_id=second_match.id,
duration_minutes=tournament.duration_minutes,
margin_minutes=tournament.margin_minutes,
custom_duration_minutes=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def set_team_ids_for_match(
stage_item_x_team_rankings,
)

await sql_update_team_ids_for_match(assert_some(match.id), team1_id, team2_id)
await sql_update_team_ids_for_match(match.id, team1_id, team2_id)


async def get_team_rankings_lookup_for_stage(
Expand Down
8 changes: 4 additions & 4 deletions backend/bracket/logic/scheduling/ladder_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_number_of_teams_played_per_team(
if isinstance(match, MatchWithDetailsDefinitive):
for team in match.teams:
if team.active and team.id not in excluded_team_ids:
result[assert_some(team.id)] += 1
result[team.id] += 1

return result

Expand Down Expand Up @@ -80,7 +80,7 @@ def get_possible_upcoming_matches_for_swiss(
)
for team in teams_to_schedule:
if team.id not in times_played_per_team:
times_played_per_team[assert_some(team.id)] = 0
times_played_per_team[team.id] = 0

min_times_played = min(times_played_per_team.values()) if len(times_played_per_team) > 0 else 0

Expand All @@ -100,8 +100,8 @@ def get_possible_upcoming_matches_for_swiss(
continue

times_played_min = min(
times_played_per_team[assert_some(team1.id)],
times_played_per_team[assert_some(team2.id)],
times_played_per_team[team1.id],
times_played_per_team[team2.id],
)
suggested_match = check_team_combination_adheres_to_filter(
team1, team2, filter_, is_recommended=times_played_min <= min_times_played
Expand Down
3 changes: 1 addition & 2 deletions backend/bracket/logic/scheduling/round_robin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from bracket.sql.matches import sql_create_match
from bracket.sql.tournaments import sql_get_tournament
from bracket.utils.id_types import TournamentId
from bracket.utils.types import assert_some


def get_round_robin_combinations(team_count: int) -> list[list[tuple[int, int]]]:
Expand Down Expand Up @@ -48,7 +47,7 @@ async def build_round_robin_stage_item(
team_1, team_2 = stage_item.inputs[team_1_id], stage_item.inputs[team_2_id]

match = MatchCreateBody(
round_id=assert_some(round_.id),
round_id=round_.id,
team1_id=team_1.team_id,
team1_winner_from_stage_item_id=team_1.winner_from_stage_item_id,
team1_winner_position=team_1.winner_position,
Expand Down
3 changes: 1 addition & 2 deletions backend/bracket/logic/scheduling/upcoming_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from bracket.sql.stages import get_full_tournament_details
from bracket.sql.teams import get_teams_with_members
from bracket.utils.id_types import StageItemId, TournamentId
from bracket.utils.types import assert_some


async def get_draft_round_in_stage_item(
Expand Down Expand Up @@ -40,7 +39,7 @@ async def get_upcoming_matches_for_swiss_round(
if not round_.is_draft:
raise HTTPException(400, "There is no draft round, so no matches can be scheduled.")

rounds = await get_rounds_for_stage_item(tournament_id, assert_some(stage_item.id))
rounds = await get_rounds_for_stage_item(tournament_id, stage_item.id)
teams = await get_teams_with_members(tournament_id, only_active_teams=True)

return get_possible_upcoming_matches_for_swiss(match_filter, rounds, teams)
3 changes: 1 addition & 2 deletions backend/bracket/logic/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from bracket.sql.rankings import sql_create_ranking
from bracket.sql.tournaments import sql_create_tournament
from bracket.utils.id_types import UserId
from bracket.utils.types import assert_some

if TYPE_CHECKING:
from bracket.models.db.user import UserBase
Expand Down Expand Up @@ -80,7 +79,7 @@ async def setup_demo_account(user_id: UserId) -> None:

tournament = TournamentBody(
name="Demo Tournament",
club_id=assert_some(club_inserted.id),
club_id=club_inserted.id,
start_time=datetime_utc.future(hours=1),
dashboard_public=False,
players_can_be_in_multiple_teams=False,
Expand Down
3 changes: 1 addition & 2 deletions backend/bracket/logic/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from bracket.sql.teams import sql_delete_teams_of_tournament
from bracket.sql.tournaments import sql_delete_tournament, sql_get_tournament
from bracket.utils.id_types import TournamentId
from bracket.utils.types import assert_some


async def get_tournament_logo_path(tournament_id: TournamentId) -> str | None:
Expand All @@ -36,7 +35,7 @@ async def sql_delete_tournament_completely(tournament_id: TournamentId) -> None:
for stage_item in stage.stage_items:
await sql_delete_stage_item(stage_item.id)

await sql_delete_stage(tournament_id, assert_some(stage.id))
await sql_delete_stage(tournament_id, stage.id)

for ranking in await get_all_rankings_in_tournament(tournament_id):
await sql_delete_ranking(tournament_id, ranking.id)
Expand Down
7 changes: 5 additions & 2 deletions backend/bracket/models/db/club.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from bracket.utils.id_types import ClubId


class Club(BaseModelORM):
id: ClubId | None = None
class ClubInsertable(BaseModelORM):
name: str
created: datetime_utc


class Club(ClubInsertable):
id: ClubId


class ClubCreateBody(BaseModelORM):
name: str

Expand Down
7 changes: 5 additions & 2 deletions backend/bracket/models/db/court.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from bracket.utils.id_types import CourtId, TournamentId


class Court(BaseModelORM):
id: CourtId | None = None
class CourtInsertable(BaseModelORM):
name: str
created: datetime_utc
tournament_id: TournamentId


class Court(CourtInsertable):
id: CourtId


class CourtBody(BaseModelORM):
name: str

Expand Down
9 changes: 6 additions & 3 deletions backend/bracket/models/db/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from bracket.utils.types import assert_some


class MatchBase(BaseModelORM):
id: MatchId | None = None
class MatchBaseInsertable(BaseModelORM):
created: datetime_utc
start_time: datetime_utc | None = None
duration_minutes: int
Expand All @@ -32,7 +31,7 @@ def end_time(self) -> datetime_utc:
)


class Match(MatchBase):
class MatchInsertable(MatchBaseInsertable):
team1_id: TeamId | None = None
team2_id: TeamId | None = None
team1_winner_position: int | None = None
Expand All @@ -49,6 +48,10 @@ def get_winner_index(self) -> int | None:
return 1 if self.team1_score > self.team2_score else 0


class Match(MatchInsertable):
id: MatchId


class MatchWithDetails(Match):
court: Court | None = None

Expand Down
7 changes: 5 additions & 2 deletions backend/bracket/models/db/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from bracket.utils.id_types import PlayerId, TournamentId


class Player(BaseModelORM):
id: PlayerId | None = None
class PlayerInsertable(BaseModelORM):
active: bool
name: str
created: datetime_utc
Expand All @@ -19,6 +18,10 @@ class Player(BaseModelORM):
draws: int = 0
losses: int = 0


class Player(PlayerInsertable):
id: PlayerId

def __hash__(self) -> int:
return self.id if self.id is not None else int(self.created.timestamp())

Expand Down
5 changes: 2 additions & 3 deletions backend/bracket/models/db/player_x_team.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from bracket.models.db.shared import BaseModelORM
from bracket.utils.id_types import PlayerId, PlayerXTeamId, TeamId
from bracket.utils.id_types import PlayerId, TeamId


class PlayerXTeam(BaseModelORM):
id: PlayerXTeamId | None = None
class PlayerXTeamInsertable(BaseModelORM):
player_id: PlayerId
team_id: TeamId
Loading