Skip to content

Commit d0e0f75

Browse files
Season str typing (#65)
- Change typing for File.season_id to `int | str | None` - Change typing for Season.id to `int | str`
1 parent e81662d commit d0e0f75

File tree

6 files changed

+35
-64
lines changed

6 files changed

+35
-64
lines changed

mediux_posters/mediux/schemas.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@
1212

1313
from datetime import date, datetime
1414
from enum import Enum
15+
from typing import Annotated
1516

16-
from pydantic import AliasPath, Field
17+
from pydantic import AliasPath, BeforeValidator, Field, TypeAdapter, ValidationError
1718

1819
from mediux_posters.utils import BaseModel
1920

2021

22+
def int_or_str(val: int | str | None) -> int | str | None:
23+
try:
24+
return TypeAdapter(int).validate_python(val)
25+
except ValidationError:
26+
return val
27+
28+
2129
class MediuxModel(BaseModel, extra="ignore"): ...
2230

2331

@@ -37,11 +45,17 @@ class File(MediuxModel):
3745
id: str
3846
file_type: FileType
3947
last_updated: datetime = Field(alias="modified_on")
40-
show_id: int | None = Field(validation_alias=AliasPath("show", "id"), default=None)
41-
season_id: int | None = Field(validation_alias=AliasPath("season", "id"), default=None)
42-
episode_id: int | None = Field(validation_alias=AliasPath("episode", "id"), default=None)
43-
collection_id: int | None = Field(validation_alias=AliasPath("collection", "id"), default=None)
44-
movie_id: int | None = Field(validation_alias=AliasPath("movie", "id"), default=None)
48+
show_id: Annotated[int | None, Field(validation_alias=AliasPath("show", "id"))] = None
49+
season_id: Annotated[
50+
int | str | None,
51+
BeforeValidator(int_or_str),
52+
Field(validation_alias=AliasPath("season", "id")),
53+
] = None
54+
episode_id: Annotated[int | None, Field(validation_alias=AliasPath("episode", "id"))] = None
55+
collection_id: Annotated[int | None, Field(validation_alias=AliasPath("collection", "id"))] = (
56+
None
57+
)
58+
movie_id: Annotated[int | None, Field(validation_alias=AliasPath("movie", "id"))] = None
4559

4660

4761
class Episode(MediuxModel):
@@ -52,7 +66,7 @@ class Episode(MediuxModel):
5266

5367
class Season(MediuxModel):
5468
episodes: list[Episode]
55-
id: int
69+
id: Annotated[int | str, BeforeValidator(int_or_str)]
5670
number: int = Field(alias="season_number")
5771
title: str | None = Field(alias="season_name", default=None)
5872

mediux_posters/services/_base/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
class BaseService(ABC, Generic[T, S, E, C, M]):
25-
def __init__(self, cache: ServiceCache) -> None:
25+
def __init__(self, cache: ServiceCache | None = None) -> None:
2626
self.cache = cache
2727

2828
@abstractmethod

mediux_posters/services/jellyfin/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
class Jellyfin(BaseService[Show, Season, Episode, Collection, Movie]):
32-
def __init__(self, base_url: str, token: str, cache: ServiceCache):
32+
def __init__(self, base_url: str, token: str, cache: ServiceCache | None = None):
3333
super().__init__(cache=cache)
3434
self.client = Client(
3535
base_url=base_url,

mediux_posters/services/plex/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
class Plex(BaseService[Show, Season, Episode, Collection, Movie]):
31-
def __init__(self, base_url: str, token: str, cache: ServiceCache):
31+
def __init__(self, base_url: str, token: str, cache: ServiceCache | None = None):
3232
super().__init__(cache=cache)
3333
self.client = Client(
3434
base_url=base_url,

tests/application_test.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/mediux_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ def test_get_show_set_invalid(mediux_session: Mediux) -> None:
6060
assert result is None
6161

6262

63+
def test_show_set_season_id_typing(mediux_session: Mediux) -> None:
64+
result = mediux_session.get_show_set(set_id=6578)
65+
assert result is not None
66+
67+
assert isinstance(result.files[0].season_id, str)
68+
assert result.files[0].season_id == "ALT15773"
69+
assert isinstance(result.show.seasons[1].id, str)
70+
assert result.show.seasons[1].id == "ALT26311"
71+
72+
6373
def test_list_collection_sets(mediux_session: Mediux) -> None:
6474
results = mediux_session.list_collection_sets(tmdb_id=573436)
6575
assert len(results) != 0
@@ -81,7 +91,7 @@ def test_list_collection_sets(mediux_session: Mediux) -> None:
8191
assert result.files[0].movie_id == 324857
8292
assert result.files[0].collection_id is None
8393
assert result.id == 24404
84-
assert result.set_title == "Spider-Man: Spider-Verse Collection"
94+
assert result.set_title == "Spider-Man: Spider-Verse Collection Set"
8595
assert result.username == "willtong93"
8696

8797

0 commit comments

Comments
 (0)