Skip to main content

Pydantic v2 typed client for the MLB Stats API

Project description

mlb-statsapi-pydantic

CI PyPI Python License: MIT

Pydantic v2 typed client for the MLB Stats API.

Note: This project is in early pre-release. The API surface may change between versions.

Every response is parsed into fully-typed Pydantic models with extra="allow" for forward compatibility. Includes both sync and async clients.

Install

pip install mlb-statsapi-pydantic

Quick start

from mlb_statsapi import MlbClient

with MlbClient() as client:
    # Today's schedule
    schedule = client.schedule(date="07/01/2024")
    for date in schedule.dates:
        for game in date.games:
            print(f"{game.teams.away.team.name} @ {game.teams.home.team.name}")

    # Team info
    teams = client.teams(sport_id=1)
    for team in teams.teams:
        print(f"{team.name} ({team.abbreviation})")

    # Standings
    standings = client.standings(season=2024)
    for record in standings.records:
        for entry in record.team_records:
            print(f"{entry.team.name}: {entry.wins}-{entry.losses}")

    # Live game data
    game = client.game(game_pk=745570)
    print(game.game_data.teams.home.name)

Async

import asyncio
from mlb_statsapi import AsyncMlbClient

async def main():
    async with AsyncMlbClient() as client:
        schedule = await client.schedule(date="07/01/2024")
        print(schedule.dates[0].games[0].teams.home.team.name)

asyncio.run(main())

See examples/ for more details

Features

  • Fully-typed Pydantic v2 models for all major endpoints
  • Sync (MlbClient) and async (AsyncMlbClient) HTTP clients
  • Enum helpers for game types and team IDs
  • Endpoint registry with URL building and parameter validation
  • extra="allow" on all models — new API fields won't break your code
  • PEP 561 compatible (py.typed)

Development

git clone https://github.com/DarkSideOfTheMat/mlb-statsapi-pydantic.git
cd mlb-statsapi-pydantic
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pre-commit install

Run checks

ruff check src/ tests/       # lint
ruff format --check src/     # format check
mypy                         # type check
pytest -q                    # tests
pytest --cov -q              # tests with coverage

Copyright Notice

This package and its author are not affiliated with MLB or any MLB team. This API wrapper interfaces with MLB's Stats API. Use of MLB data is subject to the notice posted at http://gdx.mlb.com/components/copyright.txt.

This project is inspired by MLB-StatsAPI by Todd Roberts, licensed under GPL-3.0.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlb_statsapi_pydantic-0.0.1.tar.gz (483.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlb_statsapi_pydantic-0.0.1-py3-none-any.whl (44.9 kB view details)

Uploaded Python 3

File details

Details for the file mlb_statsapi_pydantic-0.0.1.tar.gz.

File metadata

  • Download URL: mlb_statsapi_pydantic-0.0.1.tar.gz
  • Upload date:
  • Size: 483.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mlb_statsapi_pydantic-0.0.1.tar.gz
Algorithm Hash digest
SHA256 018af901819266b70344b528731e83864041561dad80d8a24f159caa6f0c554d
MD5 491d517c2a0a4664733ea6e2e9ed2821
BLAKE2b-256 5dcc17f4b4da120006dc6aacebfa7b10a73d756c9b4d81be9b8f2bf4fb8ea787

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlb_statsapi_pydantic-0.0.1.tar.gz:

Publisher: publish.yml on DarkSideOfTheMat/mlb-statsapi-pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlb_statsapi_pydantic-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mlb_statsapi_pydantic-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02b15811ccd285c0ab2b11b82672a365b99af898d46f53bfb0c255089023e129
MD5 1235fe5e5d599a0def069b5247857a43
BLAKE2b-256 42cfdf861ffc253000cea48fe32d13ca7379943f40b5a8809e1860094fee22be

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlb_statsapi_pydantic-0.0.1-py3-none-any.whl:

Publisher: publish.yml on DarkSideOfTheMat/mlb-statsapi-pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page