From 8b5ca320a934e335d0d118bee8c745de9f56adc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sun, 16 Jan 2022 01:44:42 +0200 Subject: [PATCH] Use NamedTuple typing --- trakt/core.py | 36 +++++++++++++++++++++++++++++------- trakt/movies.py | 19 ++++++++++++++----- trakt/tv.py | 8 ++++++-- trakt/users.py | 28 +++++++++++++++++++--------- 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/trakt/core.py b/trakt/core.py index dfd11f99..0b26ce20 100644 --- a/trakt/core.py +++ b/trakt/core.py @@ -7,10 +7,10 @@ import os import sys import time -from collections import namedtuple from datetime import datetime, timedelta, timezone from functools import wraps from json import JSONDecodeError +from typing import NamedTuple from urllib.parse import urljoin import requests @@ -362,12 +362,34 @@ def init(*args, **kwargs): return auth_method.get(AUTH_METHOD, PIN_AUTH)(*args, **kwargs) -Airs = namedtuple('Airs', ['day', 'time', 'timezone']) -Alias = namedtuple('Alias', ['title', 'country']) -Genre = namedtuple('Genre', ['name', 'slug']) -Comment = namedtuple('Comment', ['id', 'parent_id', 'created_at', 'comment', - 'spoiler', 'review', 'replies', 'user', - 'updated_at', 'likes', 'user_rating']) +class Airs(NamedTuple): + day: str + time: str + timezone: str + + +class Alias(NamedTuple): + title: str + country: str + + +class Genre(NamedTuple): + name: str + slug: str + + +class Comment(NamedTuple): + id: str + parent_id: str + created_at: str + comment: str + spoiler: str + review: str + replies: str + user: str + updated_at: str + likes: str + user_rating: str def _validate_token(s): diff --git a/trakt/movies.py b/trakt/movies.py index fea6372c..4d299b7d 100644 --- a/trakt/movies.py +++ b/trakt/movies.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """Interfaces to all of the Movie objects offered by the Trakt.tv API""" -from collections import namedtuple +from typing import NamedTuple from trakt.core import Alias, Comment, Genre, delete, get from trakt.mixins import IdsMixin @@ -16,8 +16,13 @@ 'trending_movies', 'updated_movies', 'Release', 'Movie', 'Translation'] -Translation = namedtuple('Translation', ['title', 'overview', 'tagline', - 'language']) + +# FIXME: same symbol in tv module +class Translation(NamedTuple): + title: str + overview: str + tagline: str + language: str @delete @@ -76,8 +81,12 @@ def updated_movies(timestamp=None): yield to_ret -Release = namedtuple('Release', ['country', 'certification', 'release_date', - 'note', 'release_type']) +class Release(NamedTuple): + country: str + certification: str + release_date: str + note: str + release_type: str class Movie(IdsMixin): diff --git a/trakt/tv.py b/trakt/tv.py index e6291a84..91db5b58 100644 --- a/trakt/tv.py +++ b/trakt/tv.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Interfaces to all of the TV objects offered by the Trakt.tv API""" -from collections import namedtuple from datetime import datetime, timedelta +from typing import NamedTuple from urllib.parse import urlencode from trakt.core import Airs, Alias, Comment, Genre, delete, get @@ -22,7 +22,11 @@ 'TVSeason', 'Translation'] -Translation = namedtuple('Translation', ['title', 'overview', 'language']) +# FIXME: same symbol in movie module +class Translation(NamedTuple): + title: str + overview: str + language: str @delete diff --git a/trakt/users.py b/trakt/users.py index 90717ef5..d54c709a 100644 --- a/trakt/users.py +++ b/trakt/users.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- """Interfaces to all of the User objects offered by the Trakt.tv API""" from collections import namedtuple +from typing import NamedTuple from trakt.core import delete, get, post from trakt.mixins import IdsMixin @@ -14,8 +15,10 @@ 'get_user_settings', 'unfollow'] -class Request(namedtuple('Request', ['id', 'requested_at', 'user'])): - __slots__ = () +class Request(NamedTuple): + id: int + user: str + requested_at: str @post def approve(self): @@ -62,13 +65,20 @@ def unfollow(user_name): yield 'users/{username}/follow'.format(username=slugify(user_name)) -class UserList(namedtuple('UserList', ['name', 'description', 'privacy', - 'share_link', 'type', 'display_numbers', - 'allow_comments', 'sort_by', - 'sort_how', 'created_at', - 'updated_at', 'item_count', - 'comment_count', 'likes', 'ids', - 'user', 'creator']), IdsMixin): +UserListFields = [ + 'name', 'description', 'privacy', + 'share_link', 'type', 'display_numbers', + 'allow_comments', 'sort_by', + 'sort_how', 'created_at', + 'updated_at', 'item_count', + 'comment_count', 'likes', 'ids', + 'user', 'creator', +] +# Can't have NamedTuple and __init__, so this stays as namedtuple() +UserListTuple = namedtuple('UserList', UserListFields) + + +class UserList(UserListTuple, IdsMixin): """A list created by a Trakt.tv :class:`User`""" def __init__(self, *args, ids=None, **kwargs):