Skip to content

Commit 23828bc

Browse files
authored
Fix Stream.fetch_game erroring for streams without a set game (#478)
* Fix Stream.fetch_game erroring for streams without a set game Signed-off-by: Lilly Rose Berner <[email protected]> * Address review comments Signed-off-by: Lilly Rose Berner <[email protected]> --------- Signed-off-by: Lilly Rose Berner <[email protected]>
1 parent c4d22d2 commit 23828bc

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

twitchio/models/streams.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ class Stream:
5858
The current stream ID.
5959
user: twitchio.PartialUser
6060
The user who is streaming.
61-
game_id: str
62-
Current game ID being played on the channel.
63-
game_name: str
64-
Name of the game being played on the channel.
61+
game_id: str | None
62+
Current game ID being played on the channel. Could be `None` if no category / game has been set.
63+
game_name: str | None
64+
Name of the game being played on the channel. Could be `None` if no category / game has been set.
6565
type: str
6666
Whether the stream is "live" or not.
6767
title: str
@@ -101,8 +101,8 @@ def __init__(self, data: StreamsResponseData, *, http: HTTPClient) -> None:
101101

102102
self.id: str = data["id"]
103103
self.user = PartialUser(data["user_id"], data["user_login"], data["user_name"], http=http)
104-
self.game_id: str = data["game_id"]
105-
self.game_name: str = data["game_name"]
104+
self.game_id: str | None = data["game_id"]
105+
self.game_name: str | None = data["game_name"]
106106
self.type: str = data["type"]
107107
self.title: str = data["title"]
108108
self.viewer_count: int = data["viewer_count"]
@@ -115,19 +115,25 @@ def __init__(self, data: StreamsResponseData, *, http: HTTPClient) -> None:
115115
def __repr__(self) -> str:
116116
return f"<Stream id={self.id} user={self.user} title={self.title} started_at={self.started_at}>"
117117

118-
async def fetch_game(self) -> Game:
118+
async def fetch_game(self) -> Game | None:
119119
"""Fetches the :class:`~twitchio.Game` associated with this stream.
120120
121121
The :class:`~twitchio.Game` returned is current from the time the :class:`~twitchio.Stream`
122122
instance was created.
123123
124+
Could be `None` if no category / game was set at the time the :class:`~twitchio.Stream`
125+
instance was created.
126+
124127
Returns
125128
-------
126-
twitchio.Game
127-
The game associated with this :class:`~twitchio.Stream` instance.
129+
twitchio.Game | None
130+
The game associated with this :class:`~twitchio.Stream` instance, or `None`.
128131
"""
132+
if self.game_id is None:
133+
return None
134+
129135
payload: GamesResponse = await self._http.get_games(ids=[self.game_id])
130-
return Game(payload["data"][0], http=self._http)
136+
return Game(payload["data"][0], http=self._http) if payload["data"] else None
131137

132138

133139
class StreamMarker:

twitchio/types_/responses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,8 +1451,8 @@ class StreamsResponseData(TypedDict):
14511451
user_id: str
14521452
user_login: str
14531453
user_name: str
1454-
game_id: str
1455-
game_name: str
1454+
game_id: str | None
1455+
game_name: str | None
14561456
type: Literal["live", ""]
14571457
title: str
14581458
tags: list[str]

0 commit comments

Comments
 (0)