Skip to content

Commit 7635d24

Browse files
committed
Refactor openingExplorer type to use generic Typedict
1 parent c369abb commit 7635d24

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

berserk/types/opening_explorer.py

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
from __future__ import annotations
22

3-
from typing import Literal, List
3+
from typing import Generic, Literal, List, TypeVar
44
from typing_extensions import TypedDict, NotRequired
5-
from .common import Speed
5+
from .common import Color, Speed
66

77
OpeningExplorerRating = Literal[
88
"0", "1000", "1200", "1400", "1600", "1800", "2000", "2200", "2500"
99
]
1010

1111

12+
MoveT = TypeVar("MoveT")
13+
GameT = TypeVar("GameT")
14+
15+
1216
class Opening(TypedDict):
1317
# The eco code of this opening
1418
eco: str
@@ -27,7 +31,7 @@ class GameWithoutUci(TypedDict):
2731
# The id of the game
2832
id: str
2933
# The winner of the game. Draw if None
30-
winner: Literal["white"] | Literal["black"] | None
34+
winner: Color | None
3135
# The speed of the game
3236
speed: Speed
3337
# The type of game
@@ -46,7 +50,7 @@ class MastersGameWithoutUci(TypedDict):
4650
# The id of the OTB master game
4751
id: str
4852
# The winner of the game. Draw if None
49-
winner: Literal["white"] | Literal["black"] | None
53+
winner: Color | None
5054
# The black player
5155
black: Player
5256
# The white player
@@ -126,7 +130,7 @@ class MastersMove(TypedDict):
126130
opening: Opening | None
127131

128132

129-
class OpeningStatistic(TypedDict):
133+
class BaseOpeningStatistic(TypedDict, Generic[MoveT, GameT]):
130134
# Number of game won by white from this position
131135
white: int
132136
# Number of game won by black from this position
@@ -135,41 +139,24 @@ class OpeningStatistic(TypedDict):
135139
black: int
136140
# Opening info of this position
137141
opening: Opening | None
138-
# The list of moves played by players from this position
139-
moves: List[Move]
140-
# recent games with this opening
141-
recentGames: List[Game]
142+
# The list of moves played from this position
143+
moves: List[MoveT]
144+
145+
146+
class OpeningStatistic(BaseOpeningStatistic[Move, Game]):
142147
# top rating games with this opening
143148
topGames: List[Game]
149+
# recent games with this opening (optional per schema)
150+
recentGames: NotRequired[List[Game]]
144151

145152

146-
class PlayerOpeningStatistic(TypedDict):
147-
# Number of game won by white from this position
148-
white: int
149-
# Number of game won by black from this position
150-
draws: int
151-
# Number draws from this position
152-
black: int
153-
# Opening info of this position
154-
opening: Opening | None
155-
# The list of moves played by the player from this position
156-
moves: List[PlayerMove]
153+
class PlayerOpeningStatistic(BaseOpeningStatistic[PlayerMove, Game]):
154+
# Queue position for indexing (present when wait_for_indexing parameter used)
155+
queuePosition: int
157156
# recent games with this opening
158157
recentGames: List[Game]
159-
# Queue position for indexing (present when wait_for_indexing parameter used)
160-
queuePosition: NotRequired[int]
161158

162159

163-
class MastersOpeningStatistic(TypedDict):
164-
# Number of game won by white from this position
165-
white: int
166-
# Number of game won by black from this position
167-
draws: int
168-
# Number draws from this position
169-
black: int
170-
# Opening info of this position
171-
opening: Opening | None
172-
# The list of moves played by players from this position (OTB masters)
173-
moves: List[MastersMove]
160+
class MastersOpeningStatistic(BaseOpeningStatistic[MastersMove, MastersGame]):
174161
# top rating OTB master games with this opening
175162
topGames: List[MastersGame]

0 commit comments

Comments
 (0)