Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1164) Type annotate reddit.py #1184

Merged
merged 2 commits into from
Dec 29, 2019
Merged
Changes from 1 commit
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
92 changes: 73 additions & 19 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@
session,
)

from typing import (
PythonCoderAS marked this conversation as resolved.
Show resolved Hide resolved
NoReturn,
Optional,
Type,
Union,
Dict,
Any,
Sequence,
Generator,
IO,
)

from . import models
from .config import Config
from .const import __version__, API_PATH, USER_AGENT_FORMAT
from .exceptions import ClientException
from .objector import Objector

Comment = models.Comment
Redditor = models.Redditor
Submission = models.Submission
Subreddit = models.Subreddit


class Reddit:
"""The Reddit class provides convenient access to reddit's API.
Expand All @@ -55,12 +72,12 @@ def _next_unique(self):
return value

@property
def read_only(self):
def read_only(self) -> bool:
"""Return True when using the ReadOnlyAuthorizer."""
return self._core == self._read_only_core

@read_only.setter
def read_only(self, value):
def read_only(self, value: bool) -> NoReturn:
"""Set or unset the use of the ReadOnlyAuthorizer.

Raise :class:`ClientException` when attempting to unset ``read_only``
Expand All @@ -86,10 +103,10 @@ def __exit__(self, *_args):

def __init__(
self,
site_name=None,
requestor_class=None,
requestor_kwargs=None,
**config_settings
site_name: str = None,
requestor_class: Optional[Type[Requestor]] = None,
requestor_kwargs: Dict[str, Any] = None,
**config_settings: str
): # noqa: D207, D301
"""Initialize a Reddit instance.

Expand Down Expand Up @@ -411,8 +428,8 @@ def _prepare_untrusted_prawcore(self, requestor):

def comment(
self, # pylint: disable=invalid-name
id=None, # pylint: disable=redefined-builtin
url=None,
id: Optional[str] = None, # pylint: disable=redefined-builtin
url: Optional[str] = None,
):
"""Return a lazy instance of :class:`~.Comment` for ``id``.

Expand All @@ -427,15 +444,17 @@ def comment(
"""
return models.Comment(self, id=id, url=url)

def domain(self, domain):
def domain(self, domain: str):
"""Return an instance of :class:`.DomainListing`.

:param domain: The domain to obtain submission listings for.

"""
return models.DomainListing(self, domain)

def get(self, path, params=None):
def get(
self, path: str, params: Optional[Union[str, Dict[str, str]]] = None
):
"""Return parsed objects returned from a GET request to ``path``.

:param path: The path to fetch.
Expand All @@ -446,7 +465,11 @@ def get(self, path, params=None):
data = self.request("GET", path, params=params)
return self._objector.objectify(data)

def info(self, fullnames=None, url=None):
def info(
self,
fullnames: Optional[Sequence[str]] = None,
url: Optional[str] = None,
) -> Generator[Union[Subreddit, Comment, Submission], None, None]:
"""Fetch information about each item in ``fullnames`` or from ``url``.

:param fullnames: A list of fullnames for comments, submissions, and/or
Expand Down Expand Up @@ -500,7 +523,13 @@ def generator(url):

return generator(url)

def patch(self, path, data=None):
def patch(
self,
path: str,
data: Optional[
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
) -> Any:
"""Return parsed objects returned from a PATCH request to ``path``.

:param path: The path to fetch.
Expand All @@ -511,7 +540,15 @@ def patch(self, path, data=None):
data = self.request("PATCH", path, data=data)
return self._objector.objectify(data)

def post(self, path, data=None, files=None, params=None):
def post(
self,
path: str,
data: Optional[
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
files: Optional[Dict[str, IO]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
) -> Any:
"""Return parsed objects returned from a POST request to ``path``.

:param path: The path to fetch.
Expand All @@ -528,7 +565,13 @@ def post(self, path, data=None, files=None, params=None):
)
return self._objector.objectify(data)

def put(self, path, data=None):
def put(
self,
path: str,
data: Optional[
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
):
"""Return parsed objects returned from a PUT request to ``path``.

:param path: The path to fetch.
Expand All @@ -539,7 +582,7 @@ def put(self, path, data=None):
data = self.request("PUT", path, data=data)
return self._objector.objectify(data)

def random_subreddit(self, nsfw=False):
def random_subreddit(self, nsfw: bool = False) -> Subreddit:
"""Return a random lazy instance of :class:`~.Subreddit`.

:param nsfw: Return a random NSFW (not safe for work) subreddit
Expand All @@ -556,7 +599,9 @@ def random_subreddit(self, nsfw=False):
path = redirect.path
return models.Subreddit(self, path.split("/")[2])

def redditor(self, name=None, fullname=None):
def redditor(
self, name: Optional[str] = None, fullname: Optional[str] = None
) -> Redditor:
"""Return a lazy instance of :class:`~.Redditor`.

:param name: The name of the redditor.
Expand All @@ -567,7 +612,16 @@ def redditor(self, name=None, fullname=None):
"""
return models.Redditor(self, name=name, fullname=fullname)

def request(self, method, path, params=None, data=None, files=None):
def request(
self,
method: str,
path: str,
params: Optional[Union[str, Dict[str, str]]] = None,
data: Optional[
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
files: Optional[Dict[str, IO]] = None,
) -> Any:
"""Return the parsed JSON data returned from a request to URL.

:param method: The HTTP method (e.g., GET, POST, PUT, DELETE).
Expand All @@ -585,8 +639,8 @@ def request(self, method, path, params=None, data=None, files=None):
)

def submission( # pylint: disable=invalid-name,redefined-builtin
self, id=None, url=None
):
self, id: Optional[str] = None, url: Optional[str] = None
) -> Submission:
"""Return a lazy instance of :class:`~.Submission`.

:param id: A reddit base36 submission ID, e.g., ``2gmzqe``.
Expand Down