Skip to content

Commit 5945b1f

Browse files
committed
refactor: migrate schedules, drafts, and awards models to Pydantic
Models converted: schedules/ (4 classes): - Schedule, ScheduleDates, ScheduleGames, ScheduleHomeAndAway, ScheduleGameTeam drafts/ (4 classes): - Round, DraftPick, DraftHome, DraftSchool awards/ (2 classes): - Awards, Award Key changes: - All fields use snake_case with aliases for API compatibility - Class names: Home -> DraftHome, School -> DraftSchool (avoid conflicts) - Fixed optional fields: DraftSchool.city/state/school_class, DraftHome.state - Field validators handle empty dicts from API as None - Updated all related tests to use new Pythonic field names - Changed TypeError expectations to ValidationError in tests
1 parent ca74821 commit 5945b1f

12 files changed

Lines changed: 302 additions & 329 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .awards import Awards
2-
from .attributes import Award
2+
from .attributes import Award
Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
1-
from typing import Union, Optional
2-
from dataclasses import dataclass
3-
1+
from typing import Optional
2+
from mlbstatsapi.models.base import MLBBaseModel
43
from mlbstatsapi.models.teams import Team
54
from mlbstatsapi.models.people import Person
65

7-
@dataclass(kw_only=True, repr=False)
8-
class Award:
6+
7+
class Award(MLBBaseModel):
98
"""
10-
This class represents an award object
9+
A class representing an award.
1110
1211
Attributes
1312
----------
1413
id : str
15-
Award id
14+
Award ID.
1615
name : str
17-
Name of the award
16+
Name of the award.
1817
date : str
19-
Date of when award was given
18+
Date of when award was given.
2019
season : str
21-
Season award is for/from
20+
Season award is for/from.
2221
team : Team
23-
Team award was to/ Player is from
22+
Team award was to / Player is from.
2423
player : Person
25-
Person award is for
26-
votes : int None
27-
Any votes associated with award
28-
notes : str None
29-
Any notes associated with award
24+
Person award is for.
25+
votes : int
26+
Any votes associated with award.
27+
notes : str
28+
Any notes associated with award.
3029
"""
31-
3230
id: str
3331
name: str
3432
date: str
3533
season: str
36-
team: Union[Team, dict]
37-
player: Union[Person, dict]
34+
team: Team
35+
player: Person
3836
votes: Optional[int] = None
3937
notes: Optional[str] = None
40-
41-
def __post_init__(self):
42-
self.team = Team(**self.team)
43-
self.player = Person(**self.player)
44-
45-
def __repr__(self) -> str:
46-
kws = [f'{key}={value}' for key, value in self.__dict__.items() if value is not None and value]
47-
return "{}({})".format(type(self).__name__, ", ".join(kws))
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
from typing import List
2-
from dataclasses import dataclass
3-
2+
from mlbstatsapi.models.base import MLBBaseModel
43
from .attributes import Award
54

6-
@dataclass
7-
class Awards:
5+
6+
class Awards(MLBBaseModel):
87
"""
9-
This class represents an awards object
8+
A class representing an awards collection.
109
1110
Attributes
1211
----------
1312
awards : List[Award]
14-
Awards
13+
List of awards.
1514
"""
16-
17-
awards: List[Award]
15+
awards: List[Award] = []
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .rounds import Round
1+
from .rounds import Round, DraftPick
2+
from .attributes import DraftHome, DraftSchool
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from dataclasses import dataclass, field
1+
from typing import Optional
2+
from pydantic import Field
3+
from mlbstatsapi.models.base import MLBBaseModel
24

35

4-
@dataclass
5-
class Home:
6+
class DraftHome(MLBBaseModel):
67
"""
7-
A home is a where a draft player is from
8+
A class representing where a draft player is from.
89
910
Attributes
1011
----------
@@ -16,19 +17,19 @@ class Home:
1617
The country where the player is from.
1718
"""
1819
city: str
19-
state: str
2020
country: str
21+
state: Optional[str] = None
2122

22-
@dataclass
23-
class School:
23+
24+
class DraftSchool(MLBBaseModel):
2425
"""
25-
Represents the school the draft player is from.
26+
A class representing the school the draft player is from.
2627
2728
Attributes
2829
----------
2930
name : str
3031
The name of the school.
31-
schoolclass : str
32+
school_class : str
3233
The class the student is in.
3334
city : str
3435
The city where the school is located.
@@ -38,7 +39,7 @@ class School:
3839
The state where the school is located.
3940
"""
4041
name: str
41-
schoolclass: str
42-
city: str
4342
country: str
44-
state: str
43+
state: Optional[str] = None
44+
school_class: Optional[str] = Field(default=None, alias="schoolclass")
45+
city: Optional[str] = None
Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,88 @@
1-
from dataclasses import dataclass, field
2-
from typing import List, Optional, Union
3-
4-
from .attributes import School, Home
1+
from typing import List, Optional
2+
from pydantic import Field
3+
from mlbstatsapi.models.base import MLBBaseModel
54
from mlbstatsapi.models.teams import Team
65
from mlbstatsapi.models.people import Person
76
from mlbstatsapi.models.data import CodeDesc
7+
from .attributes import DraftSchool, DraftHome
8+
89

9-
@dataclass(repr=False)
10-
class DraftPick:
10+
class DraftPick(MLBBaseModel):
1111
"""
12-
Represents a pick made in the MLB draft.
12+
A class representing a pick made in the MLB draft.
1313
1414
Attributes
1515
----------
16-
bisplayerid : int
17-
The unique identifier of the player associated with this draft pick.
18-
pickround : str
16+
team : Team
17+
The team that made this draft pick.
18+
draft_type : CodeDesc
19+
Information about the type of draft in which this pick was made.
20+
is_drafted : bool
21+
Whether or not the player associated with this pick has been drafted.
22+
is_pass : bool
23+
Whether or not the team passed on making a pick in this round.
24+
year : str
25+
The year in which the draft took place.
26+
school : DraftSchool
27+
Information about the player's school or college.
28+
home : DraftHome
29+
Information about the player's home location.
30+
pick_round : str
1931
The round of the draft in which this pick was made.
20-
picknumber : int
32+
pick_number : int
2133
The number of the pick in the round.
22-
displaypicknumber : int
34+
display_pick_number : int
2335
The overall pick number displayed.
24-
roundpicknumber : int
36+
round_pick_number : int
2537
The number of the pick overall in the draft.
38+
headshot_link : str
39+
A link to a headshot image of the player.
40+
person : Person
41+
The person drafted.
42+
bis_player_id : int
43+
The unique identifier of the player associated with this draft pick.
2644
rank : int
2745
The rank of the player among all players eligible for the draft.
28-
pickvalue : str
46+
pick_value : str
2947
The value of the pick, if known.
30-
signingbonus : str
48+
signing_bonus : str
3149
The signing bonus associated with this pick, if known.
32-
home : Home
33-
Information about the player's home location.
34-
scoutingreport : str
35-
A scouting report on the player's abilities.
36-
school : School
37-
Information about the player's school or college.
50+
scouting_report : str
51+
A scouting report on the player's abilities.
3852
blurb : str
39-
A brief summary of the player's background and accomplishments.
40-
headshotlink : str
41-
A link to a headshot image of the player.
42-
team : Team or dict
43-
The team that made this draft pick.
44-
drafttype : CodeDesc
45-
Information about the type of draft in which this pick was made.
46-
isdrafted : bool
47-
Whether or not the player associated with this pick has been drafted.
48-
ispass : bool
49-
Whether or not the team passed on making a pick in this round.
50-
year : str
51-
The year in which the draft took place.
53+
A brief summary of the player's background and accomplishments.
5254
"""
53-
team: Union[Team, dict]
54-
drafttype: Union[CodeDesc, dict]
55-
isdrafted: bool
56-
ispass: bool
55+
team: Team
56+
draft_type: CodeDesc = Field(alias="drafttype")
57+
is_drafted: bool = Field(alias="isdrafted")
58+
is_pass: bool = Field(alias="ispass")
5759
year: str
58-
school: Union[School , dict]
59-
home: Union[Home, dict]
60-
pickround: str
61-
picknumber: int
62-
displaypicknumber: int
63-
roundpicknumber: int
64-
headshotlink: Optional[str] = None
65-
person: Optional[Union[Person, dict]] = None
66-
bisplayerid: Optional[int] = None
60+
school: DraftSchool
61+
home: DraftHome
62+
pick_round: str = Field(alias="pickround")
63+
pick_number: int = Field(alias="picknumber")
64+
display_pick_number: int = Field(alias="displaypicknumber")
65+
round_pick_number: int = Field(alias="roundpicknumber")
66+
headshot_link: Optional[str] = Field(default=None, alias="headshotlink")
67+
person: Optional[Person] = None
68+
bis_player_id: Optional[int] = Field(default=None, alias="bisplayerid")
6769
rank: Optional[int] = None
68-
pickvalue: Optional[str] = None
69-
signingbonus: Optional[str] = None
70-
scoutingreport: Optional[str] = None
70+
pick_value: Optional[str] = Field(default=None, alias="pickvalue")
71+
signing_bonus: Optional[str] = Field(default=None, alias="signingbonus")
72+
scouting_report: Optional[str] = Field(default=None, alias="scoutingreport")
7173
blurb: Optional[str] = None
7274

73-
def __repr__(self) -> str:
74-
kws = [f'{key}={value}' for key, value in self.__dict__.items() if value is not None and value]
75-
return "{}({})".format(type(self).__name__, ", ".join(kws))
7675

77-
@dataclass(repr=False)
78-
class Round:
76+
class Round(MLBBaseModel):
7977
"""
80-
Represents a round of the MLB draft.
78+
A class representing a round of the MLB draft.
8179
8280
Attributes
8381
----------
8482
round : str
8583
The round number of the draft, represented as a string.
8684
picks : List[DraftPick]
87-
A list of DraftPick objects representing the picks made in this round of the draft.
85+
A list of DraftPick objects representing the picks made in this round.
8886
"""
8987
round: str
90-
picks: List[DraftPick]
91-
92-
def __post_init__(self):
93-
self.picks = [DraftPick(**pick) for pick in self.picks]
94-
95-
def __repr__(self) -> str:
96-
kws = [f'{key}={value}' for key, value in self.__dict__.items() if value is not None and value]
97-
return "{}({})".format(type(self).__name__, ", ".join(kws))
88+
picks: List[DraftPick] = []
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .schedule import Schedule
2-
from .attributes import ScheduleGames
2+
from .attributes import ScheduleGames, ScheduleDates, ScheduleHomeAndAway, ScheduleGameTeam

0 commit comments

Comments
 (0)