Skip to content

Commit ca74821

Browse files
committed
refactor: migrate game, gamepace, and homerunderby models to Pydantic
Models converted: game/ (41 classes): - Game, MetaData, GameData, GameDataGame, GameDatetime, GameStatus - GameTeams, GameWeather, GameInfo, ReviewInfo, GameReview, GameFlags - GameProbablePitchers, MoundVisits, LiveData, GameDecisions, GameLeaders - Plays, Play, PlayAbout, PlayResult, PlayReviewDetails, PlayMatchup - PlayMatchupSplits, PlayEvent, PlayRunner, RunnerMovement, RunnerDetails - RunnerCredits, PlayByInning, PlayByInningHits, HitsByTeam, HitCoordinates - BoxScore, TopPerformer, BoxScoreTeam, BoxScoreTeams, BoxScoreOfficial - BoxScoreGameStatus, PlayersDictPerson, BoxScoreVL, BoxScoreTeamInfo - Linescore, LinescoreTeamScoring, LinescoreInning, LinescoreTeams - LinescoreOffense, LinescoreDefense gamepace/ (3 classes): - GamePace, GamePaceData, PrPortalCalculatedFields homerunderby/ (11 classes): - HomeRunDerby, Info, EventType, Status, Round, Matchup, Seed - Hits, HitData, Coordinates, TrajectoryData Key changes: - All fields use snake_case with aliases for API compatibility - Class names standardized to PascalCase (Gamepace -> GamePace, etc.) - Field validators handle empty dicts from API as None - Fixed type mismatches: TrajectoryData uses float, Coordinates optional - Updated mlb_api.py imports and return types - Updated all related tests to use new class/field names
1 parent 78d9a66 commit ca74821

43 files changed

Lines changed: 1516 additions & 1579 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

mlbstatsapi/mlb_api.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from mlbstatsapi.models.seasons import Season
1717
from mlbstatsapi.models.drafts import Round
1818
from mlbstatsapi.models.awards import Award
19-
from mlbstatsapi.models.gamepace import Gamepace
20-
from mlbstatsapi.models.homerunderby import Homerunderby
19+
from mlbstatsapi.models.gamepace import GamePace
20+
from mlbstatsapi.models.homerunderby import HomeRunDerby
2121
from mlbstatsapi.models.standings import Standings
2222

2323
from .mlb_dataadapter import MlbDataAdapter
@@ -1099,7 +1099,7 @@ def get_game_ids(self, date: str = None,
10991099

11001100
return game_ids
11011101

1102-
def get_gamepace(self, season: str, sport_id=1, **params) -> Union[Gamepace, None]:
1102+
def get_gamepace(self, season: str, sport_id=1, **params) -> Union[GamePace, None]:
11031103
"""
11041104
Get pace of game metrics for specific sport, league or team.
11051105
@@ -1164,7 +1164,7 @@ def get_gamepace(self, season: str, sport_id=1, **params) -> Union[Gamepace, Non
11641164
or 'leagues' in mlb_data.data and mlb_data.data['leagues']
11651165
or 'sports' in mlb_data.data and mlb_data.data['sports']):
11661166

1167-
return Gamepace(**mlb_data.data)
1167+
return GamePace(**mlb_data.data)
11681168

11691169
def get_venue(self, venue_id: int, **params) -> Union[Venue, None]:
11701170
"""
@@ -2016,7 +2016,7 @@ def get_awards(self, award_id: str, **params) -> List[Award]:
20162016

20172017
return awards_list
20182018

2019-
def get_homerun_derby(self, game_id, **params) -> Union[Homerunderby, None]:
2019+
def get_homerun_derby(self, game_id, **params) -> Union[HomeRunDerby, None]:
20202020
"""
20212021
The homerun derby endpoint on the Stats API allows for users to
20222022
request information from the MLB database pertaining to the
@@ -2036,7 +2036,7 @@ def get_homerun_derby(self, game_id, **params) -> Union[Homerunderby, None]:
20362036
20372037
Returns
20382038
-------
2039-
Homerunderby object
2039+
HomeRunDerby object
20402040
20412041
See Also
20422042
--------
@@ -2049,7 +2049,7 @@ def get_homerun_derby(self, game_id, **params) -> Union[Homerunderby, None]:
20492049
None
20502050

20512051
if 'status' in mlb_data.data and mlb_data.data['status']:
2052-
return Homerunderby(**mlb_data.data)
2052+
return HomeRunDerby(**mlb_data.data)
20532053

20542054

20552055
def get_team_stats(self, team_id: int, stats: list, groups: list, **params) -> dict:
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
from .game import Game
22
from .attributes import MetaData
3-
from .livedata.plays import Plays
4-
from .livedata.linescore import Linescore
5-
from .livedata.boxscore import BoxScore
3+
from .gamedata import (
4+
GameData,
5+
GameDataGame,
6+
GameDatetime,
7+
GameStatus,
8+
GameTeams,
9+
GameWeather,
10+
GameInfo,
11+
ReviewInfo,
12+
GameReview,
13+
GameFlags,
14+
GameProbablePitchers,
15+
MoundVisits,
16+
)
17+
from .livedata import (
18+
LiveData,
19+
GameDecisions,
20+
GameLeaders,
21+
Plays,
22+
Play,
23+
PlayAbout,
24+
PlayResult,
25+
PlayReviewDetails,
26+
PlayMatchup,
27+
PlayMatchupSplits,
28+
PlayEvent,
29+
PlayRunner,
30+
RunnerMovement,
31+
RunnerDetails,
32+
RunnerCredits,
33+
PlayByInning,
34+
PlayByInningHits,
35+
HitsByTeam,
36+
HitCoordinates,
37+
BoxScore,
38+
TopPerformer,
39+
BoxScoreVL,
40+
BoxScoreTeamInfo,
41+
BoxScoreGameStatus,
42+
PlayersDictPerson,
43+
BoxScoreTeam,
44+
BoxScoreTeams,
45+
BoxScoreOfficial,
46+
Linescore,
47+
LinescoreTeamScoring,
48+
LinescoreInning,
49+
LinescoreTeams,
50+
LinescoreOffense,
51+
LinescoreDefense,
52+
)
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
from typing import List
2-
from dataclasses import dataclass
2+
from pydantic import Field
3+
from mlbstatsapi.models.base import MLBBaseModel
34

4-
@dataclass
5-
class MetaData:
5+
6+
class MetaData(MLBBaseModel):
67
"""
7-
A class to represent a Game's metaData.
8+
A class to represent a Game's metadata.
89
910
Attributes
1011
----------
1112
wait : int
12-
No idea what this wait signifies
13+
Wait value.
1314
timestamp : str
14-
The timeStamp
15-
gameevents : List[str]
16-
Current game events for this game
17-
logicalevents : List[str]
18-
Current logical events for this game
15+
The timestamp.
16+
game_events : List[str]
17+
Current game events for this game.
18+
logical_events : List[str]
19+
Current logical events for this game.
1920
"""
2021
wait: int
2122
timestamp: str
22-
gameevents: List[str]
23-
logicalevents: List[str]
23+
game_events: List[str] = Field(alias="gameevents")
24+
logical_events: List[str] = Field(alias="logicalevents")

mlbstatsapi/models/game/game.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
1-
from typing import Union, Optional
2-
from dataclasses import dataclass
3-
1+
from pydantic import Field
2+
from mlbstatsapi.models.base import MLBBaseModel
43
from mlbstatsapi.models.game.gamedata import GameData
54
from mlbstatsapi.models.game.livedata import LiveData
6-
75
from .attributes import MetaData
86

97

10-
@dataclass
11-
class Game:
8+
class Game(MLBBaseModel):
129
"""
1310
A class to represent a Game.
1411
1512
Attributes
1613
----------
17-
gamepk : int
18-
id number of this game
14+
game_pk : int
15+
ID number of this game.
1916
link : str
20-
link to the api address for this game
21-
copyright : str
22-
MLB AM copyright information
17+
Link to the API address for this game.
2318
metadata : MetaData
24-
metaData of this game
25-
gamedata : GameData
26-
gameData of this game
27-
livedata : LiveData
28-
liveData of this game
29-
30-
Methods
31-
-------
32-
id():
33-
returns this games id
19+
Metadata of this game.
20+
game_data : GameData
21+
Game data of this game.
22+
live_data : LiveData
23+
Live data of this game.
3424
"""
35-
gamepk: int
25+
game_pk: int = Field(alias="gamepk")
3626
link: str
37-
metadata: Union[MetaData, dict]
38-
gamedata: Union[GameData, dict]
39-
livedata: Union[LiveData, dict]
40-
41-
def __post_init__(self):
42-
self.metadata = MetaData(**self.metadata)
43-
self.gamedata = GameData(**self.gamedata)
44-
self.livedata = LiveData(**self.livedata)
27+
metadata: MetaData
28+
game_data: GameData = Field(alias="gamedata")
29+
live_data: LiveData = Field(alias="livedata")
4530

4631
@property
47-
def id(self):
48-
return self.gamepk
32+
def id(self) -> int:
33+
"""Returns this game's ID."""
34+
return self.game_pk
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
from .gamedata import GameData
2-
from .attributes import GameStatus, MoundVisits
2+
from .attributes import (
3+
GameDataGame,
4+
GameDatetime,
5+
GameStatus,
6+
GameTeams,
7+
GameWeather,
8+
GameInfo,
9+
ReviewInfo,
10+
GameReview,
11+
GameFlags,
12+
GameProbablePitchers,
13+
MoundVisits,
14+
)

0 commit comments

Comments
 (0)