Skip to content

Commit

Permalink
Merge pull request #1212 from PokestarFan/add-types-comment
Browse files Browse the repository at this point in the history
(#1164) Add types to reddit/comment.py
  • Loading branch information
bboe committed Dec 29, 2019
2 parents 9ac95cf + 9192763 commit 4e1da87
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions praw/models/reddit/comment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Provide the Comment class."""
from typing import Any, Dict, Optional, TypeVar, Union

from ...const import API_PATH
from ...exceptions import ClientException
from ...util.cache import cachedproperty
Expand All @@ -12,6 +14,12 @@
)
from .redditor import Redditor

_Comment = TypeVar("_Comment")
_CommentModeration = TypeVar("_CommentModeration")
Reddit = TypeVar("Reddit")
Submission = TypeVar("Submission")
Subreddit = TypeVar("Subreddit")


class Comment(InboxableMixin, UserContentMixin, FullnameMixin, RedditBase):
"""A class that represents a reddit comments.
Expand Down Expand Up @@ -63,7 +71,7 @@ class Comment(InboxableMixin, UserContentMixin, FullnameMixin, RedditBase):
STR_FIELD = "id"

@staticmethod
def id_from_url(url):
def id_from_url(url: str) -> str:
"""Get the ID of a comment from the full URL."""
parts = RedditBase._url_parts(url)
try:
Expand All @@ -81,18 +89,18 @@ def _kind(self):
return self._reddit.config.kinds["comment"]

@property
def is_root(self):
def is_root(self) -> bool:
"""Return True when the comment is a top level comment."""
parent_type = self.parent_id.split("_", 1)[0]
return parent_type == self._reddit.config.kinds["submission"]

@cachedproperty
def mod(self):
def mod(self) -> _CommentModeration:
"""Provide an instance of :class:`.CommentModeration`."""
return CommentModeration(self)

@property
def replies(self):
def replies(self) -> CommentForest:
"""Provide an instance of :class:`.CommentForest`.
This property may return an empty list if the comment
Expand All @@ -114,7 +122,7 @@ def replies(self):
return self._replies

@property
def submission(self):
def submission(self) -> Submission:
"""Return the Submission object this comment belongs to."""
if not self._submission: # Comment not from submission
self._submission = self._reddit.submission(
Expand All @@ -123,7 +131,7 @@ def submission(self):
return self._submission

@submission.setter
def submission(self, submission):
def submission(self, submission: Submission):
"""Update the Submission associated with the Comment."""
submission._comments_by_id[self.name] = self
self._submission = submission
Expand All @@ -133,10 +141,10 @@ def submission(self, submission):

def __init__(
self,
reddit,
id=None, # pylint: disable=redefined-builtin
url=None,
_data=None,
reddit: Reddit,
id: Optional[str] = None, # pylint: disable=redefined-builtin
url: Optional[str] = None,
_data: Optional[Dict[str, Any]] = None,
):
"""Construct an instance of the Comment object."""
if [id, url, _data].count(None) != 2:
Expand All @@ -153,7 +161,11 @@ def __init__(
else:
self._fetched = True

def __setattr__(self, attribute, value):
def __setattr__(
self,
attribute: str,
value: Union[str, Redditor, CommentForest, Subreddit],
):
"""Objectify author, replies, and subreddit."""
if attribute == "author":
value = Redditor.from_data(self._reddit, value)
Expand Down Expand Up @@ -196,7 +208,7 @@ def _extract_submission_id(self):
return self.context.rsplit("/", 4)[1]
return self.link_id.split("_", 1)[1]

def parent(self):
def parent(self) -> Union[_Comment, Submission]:
"""Return the parent of the comment.
The returned parent will be an instance of either
Expand Down Expand Up @@ -326,7 +338,7 @@ class CommentModeration(ThingModerationMixin):

REMOVAL_MESSAGE_API = "removal_comment_message"

def __init__(self, comment):
def __init__(self, comment: Comment):
"""Create a CommentModeration instance.
:param comment: The comment to moderate.
Expand Down

0 comments on commit 4e1da87

Please sign in to comment.