Skip to content

Commit

Permalink
Clean up code, update workflows, fix ruff issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Aug 12, 2024
1 parent 59ae5b6 commit 526a4c1
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 103 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
10 changes: 5 additions & 5 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
permissions: read-all
- cron: 0 15 * * 1
permissions: read-all
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
files: ^(.*\.toml)$

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
rev: v0.5.7
hooks:
- id: ruff
args: [ --exit-non-zero-on-fix, --fix ]
Expand All @@ -51,9 +51,9 @@ repos:
- repo: https://github.com/psf/black
hooks:
- id: black
rev: 24.4.2
rev: 24.8.0

- repo: https://github.com/LilSpazJoekp/docstrfmt
hooks:
- id: docstrfmt
rev: v1.6.1
rev: v1.8.0
54 changes: 27 additions & 27 deletions asyncpraw/models/comment_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,6 @@ class CommentForest:
"""

@staticmethod
def _gather_more_comments(
tree: list[asyncpraw.models.MoreComments],
*,
parent_tree: list[asyncpraw.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

async def __aiter__(self) -> AsyncIterator[asyncpraw.models.Comment]:
"""Allow CommentForest to be used as an AsyncIterator.
Expand Down Expand Up @@ -123,11 +101,6 @@ def _insert_comment(self, comment: asyncpraw.models.Comment):
parent = self._submission._comments_by_id[comment.parent_id]
parent.replies._comments.append(comment)

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

def list( # noqa: A003
self,
) -> (
Expand Down Expand Up @@ -170,6 +143,28 @@ async def async_func():
return async_func()
return comments

@staticmethod
def _gather_more_comments(
tree: list[asyncpraw.models.MoreComments],
*,
parent_tree: list[asyncpraw.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: asyncpraw.models.Submission,
Expand All @@ -186,6 +181,11 @@ def __init__(
self._comments = comments
self._submission = submission

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

@_deprecate_args("limit", "threshold")
async def replace_more(
self, *, limit: int | None = 32, threshold: int = 0
Expand Down
2 changes: 1 addition & 1 deletion asyncpraw/models/reddit/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async def add(
upload_url = f"https:{upload_lease['action']}"

# TODO(@LilSpazJoekp): This is a blocking operation. It should be made async.
with file.open("rb") as image: # noqa: ASYNC101
with file.open("rb") as image: # noqa: ASYNC230
upload_data["file"] = image
response = await self._reddit._core._requestor._http.post(
upload_url, data=upload_data
Expand Down
4 changes: 2 additions & 2 deletions asyncpraw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ async def _upload_image(
) -> dict[str, Any]:
file = Path(image_path)
# TODO(@LilSpazJoekp): This is a blocking operation. It should be made async.
with file.open("rb") as image: # noqa: ASYNC101
with file.open("rb") as image: # noqa: ASYNC230
header = image.read(len(JPEG_HEADER))
image.seek(0)
data["img_type"] = "jpg" if header == JPEG_HEADER else "png"
Expand Down Expand Up @@ -1847,7 +1847,7 @@ async def _upload_style_asset(self, *, image_path: str, image_type: str) -> str:
upload_url = f"https:{upload_lease['action']}"

# TODO(@LilSpazJoekp): This is a blocking operation. It should be made async.
with file.open("rb") as image: # noqa: ASYNC101
with file.open("rb") as image: # noqa: ASYNC230
upload_data["file"] = image
response = await self.subreddit._reddit._core._requestor._http.post(
upload_url, data=upload_data
Expand Down
2 changes: 1 addition & 1 deletion asyncpraw/models/reddit/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ async def upload_image(self, file_path: str) -> str:
upload_url = f"https:{upload_lease['action']}"

# TODO(@LilSpazJoekp): This is a blocking operation. It should be made async.
with file.open("rb") as image: # noqa: ASYNC101
with file.open("rb") as image: # noqa: ASYNC230
upload_data["file"] = image
response = await self._reddit._core._requestor._http.post(
upload_url, data=upload_data
Expand Down
8 changes: 4 additions & 4 deletions asyncpraw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,6 @@ def _prepare_untrusted_asyncprawcore(self, requestor: Requestor):
self._read_only_core = session(read_only_authorizer)
self._prepare_common_authorizer(authenticator)

async def close(self):
"""Close the requestor."""
await self.requestor.close()

async def _resolve_share_url(self, url: str) -> str:
"""Return the canonical URL for a given share URL."""
parts = urlparse(url).path.rstrip("/").split("/")
Expand All @@ -718,6 +714,10 @@ async def _resolve_share_url(self, url: str) -> str:
return e.response.headers.get("location")
return url

async def close(self):
"""Close the requestor."""
await self.requestor.close()

@_deprecate_args("id", "url", "fetch")
@deprecate_lazy
async def comment(
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ test = [
"pytest ==7.*",
"pytest-asyncio ==0.18.*",
"pytest-vcr ==1.*",
"testfixtures ==6.*",
"urllib3 ==1.*",
"vcrpy ==4.2.1"
]
Expand Down Expand Up @@ -148,7 +147,6 @@ select = [
"W", # pycodestyle warnings
"UP" # pyupgrade
]
ignore-init-module-imports = true

[tool.ruff.lint.flake8-annotations]
allow-star-arg-any = true
Expand Down
48 changes: 24 additions & 24 deletions tests/unit/models/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,6 @@ def test_permissions_string__with_additional_permissions(self):


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

async def generate(limit, **kwargs):
nonlocal counter
counter += 1
if counter % 2 == 0:
things = initial_things
else:
things = [Thing(counter)] + initial_things[:-1]
for thing in things:
yield thing

stream = stream_generator(generate)
seen = set()
async for thing in stream:
assert thing not in seen
seen.add(thing)
counter += 1
if counter == 400:
break

async def test_comments__with_continue_after_id(
self,
):
Expand Down Expand Up @@ -161,3 +137,27 @@ async def generate(limit, params=None, **kwargs):
thing = await self.async_next(stream)
assert thing.fullname == expected_fullname, thing
expected_fullname += 1

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

async def generate(limit, **kwargs):
nonlocal counter
counter += 1
if counter % 2 == 0:
things = initial_things
else:
things = [Thing(counter)] + initial_things[:-1]
for thing in things:
yield thing

stream = stream_generator(generate)
seen = set()
async for thing in stream:
assert thing not in seen
seen.add(thing)
counter += 1
if counter == 400:
break

0 comments on commit 526a4c1

Please sign in to comment.