From aaba1390101418c1d414ad395191bf857831ecfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 2 Jan 2023 23:11:51 +0200 Subject: [PATCH 1/3] Add dataclasses dependency for Python 3.6 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index b3cf464..55f5d38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ deprecated~=1.2.13 requests-oauthlib>=1.3 requests>=2.25 +dataclasses; python_version<"3.7" From 87f0881d4169229189c6edfda1341a347b066456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 18 Apr 2023 23:03:28 +0300 Subject: [PATCH 2/3] Change UserListTuple to UserListEntry using datacass --- trakt/users.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/trakt/users.py b/trakt/users.py index 4a8b33e..9fab669 100644 --- a/trakt/users.py +++ b/trakt/users.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Interfaces to all of the User objects offered by the Trakt.tv API""" +from dataclasses import dataclass from typing import Any, NamedTuple from trakt.core import delete, get, post @@ -64,7 +65,8 @@ def unfollow(user_name): yield 'users/{username}/follow'.format(username=slugify(user_name)) -class UserListTuple(NamedTuple): +@dataclass(frozen=True) +class UserListEntry: name: str description: str privacy: str @@ -83,7 +85,7 @@ class UserListTuple(NamedTuple): creator: str -class UserList(DataClassMixin(UserListTuple), IdsMixin): +class UserList(DataClassMixin(UserListEntry), IdsMixin): """A list created by a Trakt.tv :class:`User`""" def __init__(self, ids=None, **kwargs): From e96b1174351129b6c8060ce741ce1e6340992829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 25 Apr 2023 17:09:29 +0300 Subject: [PATCH 3/3] Update DataClassMixinClass to pass only defined properties --- trakt/mixins.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/trakt/mixins.py b/trakt/mixins.py index d4b0d15..3859dee 100644 --- a/trakt/mixins.py +++ b/trakt/mixins.py @@ -3,14 +3,20 @@ __author__ = 'Jon Nappi, Elan Ruusamäe' +from dataclasses import fields + def data_class_factory(data_class): """ A Mixin for inheriting @dataclass or NamedTuple, via composition rather inheritance. """ + field_names = set(f.name for f in fields(data_class)) + class DataClassMixinClass: def __init__(self, **kwargs): - self.data = data_class(**kwargs) + # https://stackoverflow.com/questions/54678337/how-does-one-ignore-extra-arguments-passed-to-a-dataclass/54678706#54678706 + values = {k: v for k, v in kwargs.items() if k in field_names} + self.data = data_class(**values) def __getattr__(self, item): return getattr(self.data, item)