Skip to content

Commit

Permalink
add more general type annotations (list -> collections.abc.Iterable)
Browse files Browse the repository at this point in the history
  • Loading branch information
wchistow committed Jul 20, 2023
1 parent d8f60b1 commit 519a19d
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/pystackapi/site.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, cast, TypedDict
from collections.abc import Iterable

import requests

Expand Down Expand Up @@ -47,7 +48,7 @@ def get(self, query: str, **kwargs: Any) -> RawResponseDict:
# CONTRIBUTORS: Please, sort methods by alphabet in pairs
# with first `get_<plural>` and then, get_<singular>`.

def get_answers(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_answers(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, all the undeleted answers in the system,
else the set of answers identified by `ids`.
Expand All @@ -65,7 +66,7 @@ def get_answer(self, a_id: int, **kwargs: Any) -> Item | None:
except IndexError:
return None

def get_articles(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_articles(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, all articles on the site,
else the articles identified by `ids`.
Expand All @@ -87,7 +88,7 @@ def get_badges(self, **kwargs: Any) -> list[Item]:
"""Returns all badges in the system."""
return [Item(data) for data in self.get('badges/', **kwargs)['items']]

def get_badges_recipients(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_badges_recipients(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, recently awarded badges in the system,
constrained to a certain set of badges, else recently awarded badges in the system.
Expand All @@ -106,7 +107,7 @@ def get_non_tag_based_badges(self, **kwargs: Any) -> list[Item]:
"""Returns all non-tagged-based badges in alphabetical order."""
return [Item(data) for data in self.get('badges/name', **kwargs)['items']]

def get_collectives(self, slugs: list[str] | None = None, **kwargs: Any) -> list[Item]:
def get_collectives(self, slugs: Iterable[str] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `slugs` is set, collectives in `slugs` found on the site,
else, all collectives in the system.
Expand All @@ -124,7 +125,7 @@ def get_collective(self, slug: str, **kwargs: Any) -> Item | None:
except IndexError:
return None

def get_comments(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_comments(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, the comments identified by `ids`,
else, all comments on the site.
Expand All @@ -142,7 +143,7 @@ def get_comment(self, c_id: int, **kwargs: Any) -> Item | None:
except IndexError:
return None

def get_comments_on_answers(self, ids: list[int], **kwargs: Any) -> list[Item]:
def get_comments_on_answers(self, ids: Iterable[int], **kwargs: Any) -> list[Item]:
"""Returns the comments on a set of answers."""
if not ids:
raise BadArgumentsError('the `ids` argument should be a non empty list.')
Expand All @@ -153,7 +154,7 @@ def get_info(self) -> Item:
"""Returns a collection of statistics about the site."""
return Item(self.get('info/')['items'][0]) # here can't be `IndexError`

def get_posts(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_posts(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, the posts identified by `ids`,
else, all posts on the site.
Expand All @@ -175,7 +176,7 @@ def get_privileges(self, **kwargs: Any) -> list[Item]:
"""Returns the earnable privileges on a site."""
return [Item(data) for data in self.get('privileges/', **kwargs)['items']]

def get_questions(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_questions(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, all the undeleted questions in the system,
else the set of questions identified by `ids`.
Expand All @@ -193,7 +194,7 @@ def get_question(self, q_id: int, **kwargs: Any) -> Item | None:
except IndexError:
return None

def get_revisions(self, ids: list[int], **kwargs: Any) -> list[Item]:
def get_revisions(self, ids: Iterable[int], **kwargs: Any) -> list[Item]:
"""Returns edit revisions identified by `ids`."""
addition = ';'.join(map(str, ids or []))
return [Item(data) for data in self.get(f'revisions/{addition}', **kwargs)['items']]
Expand All @@ -219,7 +220,7 @@ def get_similar(self, title: str, **kwargs: Any) -> list[Item]:
"""
return [Item(data) for data in self.get('similar/', title=title, **kwargs)['items']]

def get_suggested_edits(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_suggested_edits(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, the suggested edits identified by `ids`,
else all suggested edits on the site.
Expand All @@ -231,7 +232,7 @@ def get_tags(self, **kwargs: Any) -> list[Item]:
"""Returns all tags in the system."""
return [Item(data) for data in self.get('tags/', **kwargs)['items']]

def get_users(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]:
def get_users(self, ids: Iterable[int] | None = None, **kwargs: Any) -> list[Item]:
"""
Returns, if `ids` is set, the users identified by `ids`,
else all users on a site.
Expand Down

0 comments on commit 519a19d

Please sign in to comment.