Skip to content

Commit c97f69e

Browse files
committed
Composition over inehritance solution or dataclass
Problem is that dataclasses can't inherit IdsMixin.
1 parent 2cd1d66 commit c97f69e

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

trakt/mixins.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class IdsMixin:
1414

1515
__ids = ['imdb', 'slug', 'tmdb', 'trakt']
1616

17-
def __init__(self):
18-
self._ids = {}
17+
def __init__(self, ids=None):
18+
if ids is None:
19+
ids = {}
20+
self._ids = ids
1921

2022
@property
2123
def ids(self):
@@ -51,3 +53,11 @@ def tvdb(self):
5153
@property
5254
def tvrage(self):
5355
return self._ids.get('tvrage', None)
56+
57+
@property
58+
def slug(self):
59+
return self._ids.get('slug', None)
60+
61+
@slug.setter
62+
def slug(self, value):
63+
self._ids['slug'] = value

trakt/users.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
22
"""Interfaces to all of the User objects offered by the Trakt.tv API"""
3-
from collections import namedtuple
4-
from dataclasses import dataclass
3+
from collections import UserDict, namedtuple
4+
from dataclasses import dataclass, field
5+
from typing import Optional, Union
56

67
from trakt.core import delete, get, post
78
from trakt.mixins import IdsMixin
@@ -63,8 +64,8 @@ def unfollow(user_name):
6364
yield 'users/{username}/follow'.format(username=slugify(user_name))
6465

6566

66-
@dataclass
67-
class UserList(IdsMixin):
67+
@dataclass(frozen=True)
68+
class UserListData:
6869
name: str
6970
description: str
7071
privacy: str
@@ -79,26 +80,33 @@ class UserList(IdsMixin):
7980
item_count: str
8081
comment_count: str
8182
likes: str
82-
trakt: str
83-
slug: str
8483
user: str
8584
creator: str
8685

86+
87+
def DataClassMixin(data_class):
88+
class DataClassMixinClass:
89+
def __init__(self, **kwargs):
90+
self.data = data_class(**kwargs)
91+
92+
def __getattr__(self, item):
93+
return getattr(self.data, item)
94+
95+
return DataClassMixinClass
96+
97+
98+
class UserList(DataClassMixin(UserListData), IdsMixin):
8799
"""A list created by a Trakt.tv :class:`User`"""
88100

89-
def __init__(self, *args, ids=None, **kwargs):
90-
super().__init__()
101+
def __init__(self, ids=None, **kwargs):
102+
super().__init__(**kwargs)
91103
self._ids = ids
92-
self._items = list()
104+
self._items = []
93105

94106
def __iter__(self):
95107
"""Iterate over the items in this user list"""
96108
return self._items.__iter__()
97109

98-
@property
99-
def slug(self):
100-
return self._ids.get('slug', None)
101-
102110
@classmethod
103111
@post
104112
def create(cls, name, creator, description=None, privacy='private',

0 commit comments

Comments
 (0)