strat-gameplay-webapp/backend/app/models/roster_models.py
Cal Corum a4b99ee53e CLAUDE: Replace black and flake8 with ruff for formatting and linting
Migrated to ruff for faster, modern code formatting and linting:

Configuration changes:
- pyproject.toml: Added ruff 0.8.6, removed black/flake8
- Configured ruff with black-compatible formatting (88 chars)
- Enabled comprehensive linting rules (pycodestyle, pyflakes, isort,
  pyupgrade, bugbear, comprehensions, simplify, return)
- Updated CLAUDE.md: Changed code quality commands to use ruff

Code improvements (490 auto-fixes):
- Modernized type hints: List[T] → list[T], Dict[K,V] → dict[K,V],
  Optional[T] → T | None
- Sorted all imports (isort integration)
- Removed unused imports
- Fixed whitespace issues
- Reformatted 38 files for consistency

Bug fixes:
- app/core/play_resolver.py: Fixed type hint bug (any → Any)
- tests/unit/core/test_runner_advancement.py: Removed obsolete random mock

Testing:
- All 739 unit tests passing (100%)
- No regressions introduced

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 15:33:21 -06:00

121 lines
3.4 KiB
Python

"""Pydantic models for roster link type safety
Provides league-specific type-safe models for roster operations:
- PdRosterLinkData: PD league card-based rosters
- SbaRosterLinkData: SBA league player-based rosters
"""
from abc import ABC, abstractmethod
from uuid import UUID
from pydantic import BaseModel, ConfigDict, field_validator
class BaseRosterLinkData(BaseModel, ABC):
"""Abstract base for roster link data
Common fields shared across all leagues
"""
model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True)
id: int | None = None # Database ID (populated after save)
game_id: UUID
team_id: int
@abstractmethod
def get_entity_id(self) -> int:
"""Get the entity ID (card_id or player_id)"""
pass
@abstractmethod
def get_entity_type(self) -> str:
"""Get entity type identifier ('card' or 'player')"""
pass
class PdRosterLinkData(BaseRosterLinkData):
"""PD league roster link - tracks cards
Used for Paper Dynasty league games where rosters are composed of cards.
Each card represents a player with detailed scouting data.
"""
card_id: int
@field_validator("card_id")
@classmethod
def validate_card_id(cls, v: int) -> int:
if v <= 0:
raise ValueError("card_id must be positive")
return v
def get_entity_id(self) -> int:
return self.card_id
def get_entity_type(self) -> str:
return "card"
class SbaRosterLinkData(BaseRosterLinkData):
"""SBA league roster link - tracks players
Used for SBA league games where rosters are composed of players.
Players are identified directly by player_id without a card system.
"""
player_id: int
@field_validator("player_id")
@classmethod
def validate_player_id(cls, v: int) -> int:
if v <= 0:
raise ValueError("player_id must be positive")
return v
def get_entity_id(self) -> int:
return self.player_id
def get_entity_type(self) -> str:
return "player"
class RosterLinkCreate(BaseModel):
"""Request model for creating a roster link"""
game_id: UUID
team_id: int
card_id: int | None = None
player_id: int | None = None
@field_validator("team_id")
@classmethod
def validate_team_id(cls, v: int) -> int:
if v <= 0:
raise ValueError("team_id must be positive")
return v
def model_post_init(self, __context) -> None:
"""Validate that exactly one ID is populated"""
has_card = self.card_id is not None
has_player = self.player_id is not None
if has_card == has_player: # XOR check (both True or both False = invalid)
raise ValueError("Exactly one of card_id or player_id must be provided")
def to_pd_data(self) -> PdRosterLinkData:
"""Convert to PD roster data (validates card_id is present)"""
if self.card_id is None:
raise ValueError("card_id required for PD roster")
return PdRosterLinkData(
game_id=self.game_id, team_id=self.team_id, card_id=self.card_id
)
def to_sba_data(self) -> SbaRosterLinkData:
"""Convert to SBA roster data (validates player_id is present)"""
if self.player_id is None:
raise ValueError("player_id required for SBA roster")
return SbaRosterLinkData(
game_id=self.game_id, team_id=self.team_id, player_id=self.player_id
)