Skip to content

Commit 77fecd6

Browse files
committed
chore: add shared MLBBaseModel and base tests
- Introduce MLBBaseModel (Pydantic v2) with extra="ignore" and populate_by_name - Export MLBBaseModel from models package - Add unit tests to verify ignoring unknown fields and alias population
1 parent 889899f commit 77fecd6

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

mlbstatsapi/models/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .base import MLBBaseModel
2+
3+
__all__ = ["MLBBaseModel"]

mlbstatsapi/models/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pydantic import BaseModel, ConfigDict
2+
3+
4+
class MLBBaseModel(BaseModel):
5+
"""Common base for all MLB Stats API models.
6+
7+
- Pydantic v2
8+
- Ignores unknown fields to remain resilient to API changes
9+
- populate_by_name allows alias-based population when needed
10+
"""
11+
12+
model_config = ConfigDict(
13+
extra="ignore",
14+
populate_by_name=True,
15+
)

tests/test_base_model.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pydantic import Field
2+
3+
from mlbstatsapi.models import MLBBaseModel
4+
5+
6+
class Sample(MLBBaseModel):
7+
id: int
8+
full_name: str = Field(alias="fullName")
9+
10+
11+
def test_ignore_extra_fields():
12+
obj = Sample(id=1, full_name="Test", extra_field="ignored")
13+
assert obj.id == 1
14+
assert obj.full_name == "Test"
15+
# Extra fields should be ignored and not set as attributes
16+
assert not hasattr(obj, "extra_field")
17+
18+
19+
def test_populate_by_name_alias():
20+
# populate_by_name allows alias population when present
21+
obj = Sample(id=1, fullName="Alias Name") # type: ignore[arg-type]
22+
assert obj.full_name == "Alias Name"

0 commit comments

Comments
 (0)