Skip to content

Commit

Permalink
Merge pull request #2027 from praw-dev/clean-up
Browse files Browse the repository at this point in the history
Clean up code, update workflows, fix ruff issues
  • Loading branch information
LilSpazJoekp authored Aug 12, 2024
2 parents 1c829ab + 54456fb commit 37afb7d
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[report]
exclude_lines =
@abstract
if TYPE_CHECKING:
pragma: no cover
31 changes: 0 additions & 31 deletions .github/workflows/codeql.yml

This file was deleted.

7 changes: 4 additions & 3 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ jobs:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: '3.x'
cache: pip
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/scorecards-analysis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jobs:
analysis:
name: Scorecards analysis
name: Scorecard analysis
permissions:
id-token: write
security-events: write
Expand All @@ -23,14 +23,14 @@ jobs:
path: results.sarif
retention-days: 5
- name: Upload to code-scanning
uses: github/codeql-action/upload-sarif@1b1aada464948af03b950897e5eb522f92603cc2 # v3.24.9
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
name: Scorecards supply-chain security
name: Scorecard supply-chain security
on:
branch_protection_rule:
push:
branches: [ master ]
schedule:
- cron: 30 1 * * 6
- cron: 0 15 * * 1
permissions: read-all
2 changes: 1 addition & 1 deletion praw/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def authorize(self, code: str) -> str | None:
return authorizer.refresh_token

@_deprecate_args("access_token", "expires_in", "scope")
def implicit(self, *, access_token: str, expires_in: int, scope: str) -> None:
def implicit(self, *, access_token: str, expires_in: int, scope: str):
"""Set the active authorization to be an implicit authorization.
:param access_token: The access_token obtained from Reddit's callback.
Expand Down
54 changes: 27 additions & 27 deletions praw/models/comment_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@ class CommentForest:
"""

@staticmethod
def _gather_more_comments(
tree: list[praw.models.MoreComments],
*,
parent_tree: list[praw.models.MoreComments] | None = None,
) -> list[MoreComments]:
"""Return a list of :class:`.MoreComments` objects obtained from tree."""
more_comments = []
queue = [(None, x) for x in tree]
while queue:
parent, comment = queue.pop(0)
if isinstance(comment, MoreComments):
heappush(more_comments, comment)
if parent:
comment._remove_from = parent.replies._comments
else:
comment._remove_from = parent_tree or tree
else:
for item in comment.replies:
queue.append((comment, item))
return more_comments

def __getitem__(self, index: int) -> praw.models.Comment:
"""Return the comment at position ``index`` in the list.
Expand Down Expand Up @@ -80,11 +58,6 @@ def _insert_comment(self, comment: praw.models.Comment):
parent = self._submission._comments_by_id[comment.parent_id]
parent.replies._comments.append(comment)

def _update(self, comments: list[praw.models.Comment]):
self._comments = comments
for comment in comments:
comment.submission = self._submission

def list( # noqa: A003
self,
) -> list[praw.models.Comment | praw.models.MoreComments]:
Expand All @@ -103,6 +76,28 @@ def list( # noqa: A003
queue.extend(comment.replies)
return comments

@staticmethod
def _gather_more_comments(
tree: list[praw.models.MoreComments],
*,
parent_tree: list[praw.models.MoreComments] | None = None,
) -> list[MoreComments]:
"""Return a list of :class:`.MoreComments` objects obtained from tree."""
more_comments = []
queue = [(None, x) for x in tree]
while queue:
parent, comment = queue.pop(0)
if isinstance(comment, MoreComments):
heappush(more_comments, comment)
if parent:
comment._remove_from = parent.replies._comments
else:
comment._remove_from = parent_tree or tree
else:
for item in comment.replies:
queue.append((comment, item))
return more_comments

def __init__(
self,
submission: praw.models.Submission,
Expand All @@ -119,6 +114,11 @@ def __init__(
self._comments = comments
self._submission = submission

def _update(self, comments: list[praw.models.Comment]):
self._comments = comments
for comment in comments:
comment.submission = self._submission

@_deprecate_args("limit", "threshold")
def replace_more(
self, *, limit: int | None = 32, threshold: int = 0
Expand Down
2 changes: 1 addition & 1 deletion praw/models/listing/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __len__(self) -> int:
"""Return the number of items in the Listing."""
return len(getattr(self, self.CHILD_ATTRIBUTE))

def __setattr__(self, attribute: str, value: Any) -> None:
def __setattr__(self, attribute: str, value: Any):
"""Objectify the ``CHILD_ATTRIBUTE`` attribute."""
if attribute == self.CHILD_ATTRIBUTE:
value = self._reddit._objector.objectify(value)
Expand Down
2 changes: 1 addition & 1 deletion praw/models/reddit/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def __len__(self) -> int:
"""
return len(self.link_ids)

def __setattr__(self, attribute: str, value: Any) -> None:
def __setattr__(self, attribute: str, value: Any):
"""Objectify author, subreddit, and sorted_links attributes."""
if attribute == "author_name":
self.author = self._reddit.redditor(value)
Expand Down
2 changes: 1 addition & 1 deletion praw/models/reddit/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def user_selection(self) -> PollOption | None:
return None
return self.option(self._user_selection)

def __setattr__(self, attribute: str, value: Any) -> None:
def __setattr__(self, attribute: str, value: Any):
"""Objectify the options attribute, and save user_selection."""
if attribute == "options" and isinstance(value, list):
value = [PollOption(self._reddit, option) for option in value]
Expand Down
3 changes: 2 additions & 1 deletion praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def read_only(self) -> bool:
return self._core == self._read_only_core

@read_only.setter
def read_only(self, value: bool) -> None:
def read_only(self, value: bool):
"""Set or unset the use of the ReadOnlyAuthorizer.
:raises: :class:`.ClientException` when attempting to unset ``read_only`` and
Expand Down Expand Up @@ -441,6 +441,7 @@ def request(self, *args, **kwargs):
def _check_for_async(self):
if self.config.check_for_async: # pragma: no cover
try:
# noinspection PyUnresolvedReferences
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return
Expand Down
2 changes: 1 addition & 1 deletion praw/util/token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class FileTokenManager(BaseTokenManager):
"""

def __init__(self, filename: str) -> None:
def __init__(self, filename: str):
"""Initialize a :class:`.FileTokenManager` instance.
:param filename: The file the contains the refresh token.
Expand Down
4 changes: 0 additions & 4 deletions pytest.ini

This file was deleted.

41 changes: 21 additions & 20 deletions tests/unit/models/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,6 @@ def test_permissions_string__with_additional_permissions(self):


class TestStream(UnitTest):
def test_stream(
self,
):
Thing = namedtuple("Thing", ["fullname"])
initial_things = [Thing(n) for n in reversed(range(100))]
counter = 99

def generate(limit, **kwargs):
nonlocal counter
counter += 1
if counter % 2 == 0:
return initial_things
return [Thing(counter)] + initial_things[:-1]

stream = stream_generator(generate)
seen = set()
for _ in range(400):
thing = next(stream)
assert thing not in seen
seen.add(thing)

def test_comments__with_continue_after_id(
self,
Expand Down Expand Up @@ -155,3 +135,24 @@ def generate(limit, params=None, **kwargs):
thing = next(stream)
assert thing.fullname == expected_fullname, thing
expected_fullname += 1

def test_stream(
self,
):
Thing = namedtuple("Thing", ["fullname"])
initial_things = [Thing(n) for n in reversed(range(100))]
counter = 99

def generate(limit, **kwargs):
nonlocal counter
counter += 1
if counter % 2 == 0:
return initial_things
return [Thing(counter)] + initial_things[:-1]

stream = stream_generator(generate)
seen = set()
for _ in range(400):
thing = next(stream)
assert thing not in seen
seen.add(thing)
4 changes: 2 additions & 2 deletions tools/static_word_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class StaticChecker:
"""Run simple checks on the entire document or specific lines."""

def __init__(self, replace: bool) -> None:
def __init__(self, replace: bool):
"""Initialize a :class:`.StaticChecker` instance.
:param replace: Whether or not to make replacements.
:param replace: Whether to make replacements.
"""
self.full_file_checks = [
Expand Down

0 comments on commit 37afb7d

Please sign in to comment.