Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from trakt.movies import Movie
from trakt.people import Person
from trakt.tv import TVEpisode, TVSeason, TVShow
from trakt.users import (Request, User, UserList, get_all_requests,
from trakt.users import (PublicList, Request, User, UserList, get_all_requests,
get_user_settings)


Expand Down Expand Up @@ -141,3 +141,19 @@ def test_liked_lists():

lists = sean.get_liked_lists('comments')
assert isinstance(lists, list)


def test_process_items_ignores_unknown_fields():
items = [{
'rank': 1,
'id': 101,
'listed_at': '2024-01-01T00:00:00.000Z',
'type': 'movie',
'my_rating': 8,
'movie': {'title': 'TRON', 'year': 1982,
'ids': {'trakt': 1, 'slug': 'tron'}},
}]
entries = list(PublicList._process_items(items))
assert len(entries) == 1
assert entries[0].id == 101
assert entries[0].data['title'] == 'TRON'
8 changes: 6 additions & 2 deletions trakt/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Interfaces to all of the User objects offered by the Trakt.tv API"""

from dataclasses import dataclass
from dataclasses import dataclass, fields
from typing import Any, NamedTuple, Optional, Union

from trakt.core import delete, get, post
Expand Down Expand Up @@ -182,13 +182,17 @@ def _load_items(self):

@staticmethod
def _process_items(items):
entry_fields = {f.name for f in fields(ListEntry)}
for item in items:
if "type" not in item:
continue
data = item.pop(item["type"])
if "show" in item:
data["show"] = item.pop("show")
yield ListEntry(**item, data=data)
# Drop any fields the API returns that ListEntry does not declare
# (e.g. "my_rating") so they don't break construction.
kwargs = {k: v for k, v in item.items() if k in entry_fields}
yield ListEntry(**kwargs, data=data)


class UserList(DataClassMixin(ListDescription), IdsMixin):
Expand Down