Skip to content

Commit

Permalink
Adds types for all the methods in the Reddit class
Browse files Browse the repository at this point in the history
- Adds types for all the methods in the Reddit class
- Adds asyncprawcore-stubs as dependency
- Adds typing for some modules that are necessary for the Reddit class. However, some stubs are partially implemented
  • Loading branch information
isFakeAccount committed Apr 13, 2024
1 parent 4ef9da8 commit 385aa11
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 14 deletions.
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ python = "^3.9"
asyncpraw = "^7.7.1"
asyncprawcore = "^2.4.0"
typing-extensions = "^4.10.0"
asyncprawcore-stubs = "^0.0.1"


[tool.poetry.group.typing.dependencies]
Expand Down
13 changes: 13 additions & 0 deletions src/asyncpraw-stubs/models/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .listing.domain import DomainListing
from .reddit.comment import Comment
from .reddit.redditor import Redditor
from .reddit.submission import Submission
from .reddit.subreddit import Subreddit

__all__ = [
"Comment",
"DomainListing",
"Redditor",
"Submission",
"Subreddit",
]
5 changes: 5 additions & 0 deletions src/asyncpraw-stubs/models/base.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Provide the AsyncPRAWBase superclass."""

from __future__ import annotations

class AsyncPRAWBase: ...
Empty file.
9 changes: 9 additions & 0 deletions src/asyncpraw-stubs/models/listing/domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Provide the DomainListing class."""

from __future__ import annotations

from .mixins import BaseListingMixin, RisingListingMixin


class DomainListing(BaseListingMixin, RisingListingMixin):
...
6 changes: 6 additions & 0 deletions src/asyncpraw-stubs/models/listing/mixins/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Package providing models that pertain to listing mixins."""

from .base import BaseListingMixin
from .rising import RisingListingMixin

__all__ = ["BaseListingMixin", "RisingListingMixin"]
7 changes: 7 additions & 0 deletions src/asyncpraw-stubs/models/listing/mixins/base.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Provide the BaseListingMixin class."""

from __future__ import annotations

from ...base import AsyncPRAWBase

class BaseListingMixin(AsyncPRAWBase): ...
7 changes: 7 additions & 0 deletions src/asyncpraw-stubs/models/listing/mixins/rising.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Provide the RisingListingMixin class."""

from __future__ import annotations

from ...base import AsyncPRAWBase

class RisingListingMixin(AsyncPRAWBase): ...
Empty file.
9 changes: 9 additions & 0 deletions src/asyncpraw-stubs/models/reddit/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Provide the RedditBase class."""

from __future__ import annotations

from asyncpraw.models.base import AsyncPRAWBase


class RedditBase(AsyncPRAWBase):
...
7 changes: 7 additions & 0 deletions src/asyncpraw-stubs/models/reddit/comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Provide the Comment class."""

from __future__ import annotations


class Comment: # incomplete
...
3 changes: 3 additions & 0 deletions src/asyncpraw-stubs/models/reddit/redditor.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Provide the Redditor class."""

class Redditor: ...
1 change: 1 addition & 0 deletions src/asyncpraw-stubs/models/reddit/submission.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Submission: ...
1 change: 1 addition & 0 deletions src/asyncpraw-stubs/models/reddit/subreddit.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Subreddit: ...
7 changes: 7 additions & 0 deletions src/asyncpraw-stubs/models/util.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Provide helper classes used by other models."""

from __future__ import annotations

from typing import Any, Callable

def deprecate_lazy(func: Callable[..., Any]) -> Callable[..., Any]: ...
3 changes: 1 addition & 2 deletions src/asyncpraw-stubs/objector.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ from .exceptions import RedditAPIException

if TYPE_CHECKING:
import asyncpraw

from .models.reddit.base import RedditBase
from asyncpraw.models.reddit.base import RedditBase

class Objector:
@classmethod
Expand Down
141 changes: 137 additions & 4 deletions src/asyncpraw-stubs/reddit.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,36 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, Optional, TypedDict
from typing import IO, TYPE_CHECKING, Any, AsyncGenerator, Iterable, TypedDict

import asyncprawcore.auth
from asyncpraw import models
from asyncpraw.exceptions import (
RedditAPIException,
)
from asyncpraw.models.util import deprecate_lazy
from asyncpraw.util.deprecated_args import _deprecate_args
from asyncprawcore.requestor import Requestor
from typing_extensions import NotRequired, Unpack

if TYPE_CHECKING:
import asyncpraw
import asyncpraw.models
import asyncprawcore

from .util.token_manager import BaseTokenManager

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

class ConfigSettings(TypedDict):
"""Represents a configuration options that can be passed through initializer of the Reddit class."""

client_id: str
"""The OAuth client ID associated with your registered Reddit application."""
client_secret: NotRequired[Optional[str]]
client_secret: NotRequired[str | None]
"""The OAuth client secret associated with your registered Reddit application. This option is required for all application types, however, the value must
be set to `None` for installed applications."""
user_agent: str
Expand Down Expand Up @@ -60,7 +75,7 @@ class ConfigSettings(TypedDict):

class Reddit:
update_checked: bool
_ratelimit_regex: re.Pattern
_ratelimit_regex: re.Pattern[str]

@property
def _next_unique(self) -> int: ...
Expand All @@ -82,8 +97,126 @@ class Reddit:
site_name: str | None = None,
*,
config_interpolation: str | None = None,
requestor_class: type[asyncprawcore.requestor.Requestor] | None = None,
requestor_class: type[Requestor] | None = None,
requestor_kwargs: dict[str, Any] | None = None,
token_manager: BaseTokenManager | None = None,
**config_settings: Unpack[ConfigSettings],
) -> None: ...
def _check_for_update(self) -> None: ...
def _handle_rate_limit(self, exception: RedditAPIException) -> int | float | None: ...
async def _objectify_request(
self,
*,
data: dict[str, str | Any] | bytes | IO | str | None = None, # type: ignore
files: dict[str, IO] | None = None, # type: ignore
json: dict[Any, Any] | list[Any] | None = None,
method: str = "",
params: str | dict[str, str] | None = None,
path: str = "",
) -> Any: ...
def _prepare_asyncprawcore(
self,
*,
requestor_class: type[Requestor] | None = None,
requestor_kwargs: Any | None = None,
) -> Requestor: ...
def _prepare_common_authorizer(self, authenticator: asyncprawcore.auth.BaseAuthenticator) -> None: ...
def _prepare_objector(self) -> None: ...
def _prepare_trusted_asyncprawcore(self, requestor: Requestor) -> None: ...
def _prepare_untrusted_asyncprawcore(self, requestor: Requestor) -> None: ...
async def close(self) -> None: ...
async def _resolve_share_url(self, url: str) -> str: ...
@_deprecate_args("id", "url", "fetch")
@deprecate_lazy
async def comment(
self,
id: str | None = None,
*,
fetch: bool = True,
url: str | None = None,
**_: Any,
) -> models.Comment: ...
@_deprecate_args("path", "data", "json", "params")
async def delete(
self,
path: str,
*,
data: dict[str, str | Any] | bytes | IO | str | None = None, # type: ignore
json: dict[Any, Any] | list[Any] | None = None,
params: str | dict[str, str] | None = None,
) -> Any: ...
def domain(self, domain: str) -> models.DomainListing: ...
@_deprecate_args("path", "params")
async def get(
self,
path: str,
*,
params: str | dict[str, str | int] | None = None,
) -> Any: ...
@_deprecate_args("fullnames", "url", "subreddits")
def info(
self,
*,
fullnames: Iterable[str] | None = None,
subreddits: Iterable[asyncpraw.models.Subreddit | str] | None = None,
url: str | None = None,
) -> AsyncGenerator[asyncpraw.models.Subreddit | asyncpraw.models.Comment | asyncpraw.models.Submission, None,]: ...
@_deprecate_args("path", "data", "json")
async def patch(
self,
path: str,
*,
data: dict[str, str | Any] | bytes | IO | str | None = None,
json: dict[Any, Any] | list[Any] | None = None,
params: str | dict[str, str] | None = None,
) -> Any: ...
@_deprecate_args("path", "data", "files", "params", "json")
async def post(
self,
path: str,
*,
data: dict[str, str | Any] | bytes | IO | str | None = None,
files: dict[str, IO] | None = None,
json: dict[Any, Any] | list[Any] | None = None,
params: str | dict[str, str] | None = None,
) -> Any: ...
@_deprecate_args("path", "data", "json")
async def put(
self,
path: str,
*,
data: dict[str, str | Any] | bytes | IO | str | None = None,
json: dict[Any, Any] | list[Any] | None = None,
) -> Any: ...
@_deprecate_args("nsfw")
async def random_subreddit(self, *, nsfw: bool = False) -> asyncpraw.models.Subreddit: ...
@_deprecate_args("name", "fullname", "fetch")
async def redditor(
self,
name: str | None = None,
*,
fetch: bool = False,
fullname: str | None = None,
) -> asyncpraw.models.Redditor: ...
@_deprecate_args("method", "path", "params", "data", "files", "json")
async def request(
self,
*,
data: dict[str, str | Any] | bytes | IO | str | None = None,
files: dict[str, IO] | None = None,
json: dict[Any, Any] | list[Any] | None = None,
method: str,
params: str | dict[str, str | int] | None = None,
path: str,
) -> Any: ...
@_deprecate_args("id", "url", "fetch")
@deprecate_lazy
async def submission(
self,
id: str | None = None,
*,
fetch: bool = True,
url: str | None = None,
**_,
) -> asyncpraw.models.Submission: ...
async def username_available(self, name: str) -> bool: ...
14 changes: 7 additions & 7 deletions src/asyncpraw-stubs/util/token_manager.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
import aiosqlite
import asyncpraw
import asyncprawcore
from asyncprawcore.auth import BaseAuthorizer

class BaseTokenManager(ABC):
@abstractmethod
def post_refresh_callback(self, authorizer: asyncprawcore.auth.BaseAuthorizer) -> None: ...
async def post_refresh_callback(self, authorizer: BaseAuthorizer) -> None: ...
@abstractmethod
def pre_refresh_callback(self, authorizer: asyncprawcore.auth.BaseAuthorizer) -> None: ...
async def pre_refresh_callback(self, authorizer: BaseAuthorizer) -> None: ...
@property
def reddit(self) -> asyncpraw.Reddit: ...
@reddit.setter
def reddit(self, value: asyncpraw.Reddit) -> None: ...

class FileTokenManager(BaseTokenManager):
def __init__(self, filename: str) -> None: ...
async def post_refresh_callback(self, authorizer: asyncprawcore.auth.BaseAuthorizer) -> None: ...
async def pre_refresh_callback(self, authorizer: asyncprawcore.auth.BaseAuthorizer) -> None: ...
async def post_refresh_callback(self, authorizer: BaseAuthorizer) -> None: ...
async def pre_refresh_callback(self, authorizer: BaseAuthorizer) -> None: ...

class SQLiteTokenManager(BaseTokenManager):
def __init__(self, database: str, key: str) -> None: ...
async def close(self) -> None: ...
async def connection(self) -> "aiosqlite.Connection": ...
async def is_registered(self) -> bool: ...
async def post_refresh_callback(self, authorizer: asyncprawcore.auth.BaseAuthorizer) -> None: ...
async def pre_refresh_callback(self, authorizer: asyncprawcore.auth.BaseAuthorizer) -> None: ...
async def post_refresh_callback(self, authorizer: BaseAuthorizer) -> None: ...
async def pre_refresh_callback(self, authorizer: BaseAuthorizer) -> None: ...
async def register(self, refresh_token: str) -> bool: ...

0 comments on commit 385aa11

Please sign in to comment.