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

experiment: Use TypedDict for team_info response #10

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'Werkzeug >= 2, <3',
'simplejson >=3.6, <4',
'python-dateutil >=2.2, <3',
'typing-extensions >=3.7.4.2, <5',
'typing-extensions >=4, <5',
PeterJCLaw marked this conversation as resolved.
Show resolved Hide resolved
],
python_requires='>=3.7',
entry_points={
Expand Down
31 changes: 16 additions & 15 deletions sr/comp/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sr.comp.types import ArenaName, MatchNumber, Region, RegionName, TLA

from .query_utils import MatchInfo
from .structs import NameWithURL, TeamInfo, TeamScores

app = Flask('sr.comp.http')
app.json_encoder = JsonEncoder # type: ignore[assignment]
Expand Down Expand Up @@ -121,24 +122,24 @@ def get_location(name: str) -> Response:
return jsonify(format_location(location))


def team_info(comp: SRComp, team: Team) -> dict[str, Any]:
def team_info(comp: SRComp, team: Team) -> TeamInfo:
scores = comp.scores.league.teams[team.tla]
league_pos = comp.scores.league.positions[team.tla]
location = comp.venue.get_team_location(team.tla)
info = {
'name': team.name,
'get': url_for('get_team', tla=team.tla),
'tla': team.tla,
'league_pos': league_pos,
'location': {
'name': location,
'get': url_for('get_location', name=location),
},
'scores': {
'league': scores.league_points,
'game': scores.game_points,
},
}
info = TeamInfo(
name=team.name,
get=url_for('get_team', tla=team.tla),
tla=team.tla,
league_pos=league_pos,
location=NameWithURL(
name=location,
get=url_for('get_location', name=location),
),
scores=TeamScores(
league=scores.league_points,
game=scores.game_points,
),
)

if os.path.exists(os.path.join(
g.comp_man.root_dir,
Expand Down
31 changes: 31 additions & 0 deletions sr/comp/http/structs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import annotations
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: I've tended to call files such as this types.py (the core srcomp project has such a file).


from typing_extensions import NotRequired, TypedDict
PeterJCLaw marked this conversation as resolved.
Show resolved Hide resolved

from league_ranker import LeaguePoints

from sr.comp.scores import LeaguePosition
from sr.comp.types import GamePoints


class NameWithURL(TypedDict):

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd rather not have blank lines at the top of classes please.

name: str
get: str


class TeamScores(TypedDict):

league: LeaguePoints
game: GamePoints


class TeamInfo(TypedDict):

name: str
get: str
tla: str
league_pos: LeaguePosition
location: NameWithURL
scores: TeamScores
image_url: NotRequired[str]