Skip to content

Commit 2e27671

Browse files
committed
Finishing touches for cleanup
1 parent 7c4129d commit 2e27671

7 files changed

Lines changed: 34 additions & 26 deletions

File tree

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ body:
8181
type: input
8282
validations:
8383
required: true
84+
- id: sync-praw
85+
attributes:
86+
description: Does this issue also occur in `praw`?
87+
label: Does this issue occur in `praw`?
88+
multiple: false
89+
options:
90+
- "Yes"
91+
- "No"
92+
type: dropdown
93+
validations:
94+
required: true
8495
- id: anything-else
8596
attributes:
8697
description: Anything that will give us more context about the issue you are encountering!

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Unreleased
7171
- Remove ability to use :class:`.CommentForest` as an asynchronous iterator.
7272
- Remove ability to use :class:`.Reddit` as an synchronous context manager.
7373
- Remove key ``reset_timestamp`` from :meth:`.limits`.
74+
- Remove ``Reddit.close`` method. Use ``async with Reddit(...)`` instead.
7475

7576
7.8.1 (2024/12/21)
7677
------------------

asyncpraw/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from __future__ import annotations
44

55
import configparser
6+
import importlib
7+
import importlib.resources
68
import os
7-
import sys
89
from pathlib import Path
910
from threading import Lock
1011
from types import MappingProxyType
@@ -49,7 +50,8 @@ def _load_config(cls, *, config_interpolation: str | None = None) -> None:
4950
interpolator_class = None
5051

5152
config = configparser.ConfigParser(interpolation=interpolator_class)
52-
module_dir = Path(sys.modules[__name__].__file__).parent
53+
with importlib.resources.files(__package__).joinpath("praw.ini").open("r") as hdl:
54+
config.read_file(hdl)
5355

5456
if "APPDATA" in os.environ: # Windows
5557
os_config_path = Path(os.environ["APPDATA"])
@@ -60,10 +62,10 @@ def _load_config(cls, *, config_interpolation: str | None = None) -> None:
6062
else:
6163
os_config_path = None
6264

63-
locations = [str(module_dir / "praw.ini"), "praw.ini"]
65+
locations = ["praw.ini"]
6466

6567
if os_config_path is not None:
66-
locations.insert(1, str(os_config_path / "praw.ini"))
68+
locations.insert(0, str(os_config_path / "praw.ini"))
6769

6870
config.read(locations)
6971
cls.CONFIG = config

asyncpraw/objector.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def parse_error(cls, data: list[Any] | dict[str, dict[str, str]]) -> RedditAPIEx
3434
3535
"""
3636
if isinstance(data, list):
37-
# Fetching a Submission returns a list (of two items).
38-
# Although it's handled manually in `Submission._fetch()`,
39-
# assume it's a possibility here.
37+
# Fetching a Submission returns a list (of two items). Although it's handled
38+
# manually in `Submission._fetch()`, assume it's a possibility here.
4039
return None
4140

4241
errors = data.get("json", {}).get("errors")

asyncpraw/reddit.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636

3737
UPDATE_CHECKER_MISSING = False
3838
except ImportError: # pragma: no cover
39+
update_check = None
3940
UPDATE_CHECKER_MISSING = True
4041

4142
if TYPE_CHECKING:
4243
from collections.abc import AsyncGenerator, Iterable
4344

4445
import asyncprawcore
4546

46-
import asyncpraw
4747
import asyncpraw.models
4848

4949
Comment = models.Comment
@@ -110,7 +110,8 @@ async def __aenter__(self): # noqa: ANN204
110110

111111
async def __aexit__(self, *_: object) -> None:
112112
"""Handle the context manager close."""
113-
await self.close()
113+
if self._core is not None:
114+
await self._core.close()
114115

115116
def __deepcopy__(self, memodict: dict[str, Any] | None = None) -> Reddit:
116117
"""Shallow copy on deepcopy.
@@ -209,7 +210,9 @@ async def request(self, *args, **kwargs):
209210
await reddit.close()
210211
211212
"""
212-
self._core = self._authorized_core = self._read_only_core = None
213+
self._core: asyncprawcore.Session | None = None
214+
self._authorized_core: asyncprawcore.Session | None = None
215+
self._read_only_core: asyncprawcore.Session | None = None
213216
self._objector: Objector
214217
self._unique_counter = 0
215218

@@ -243,7 +246,7 @@ async def request(self, *args, **kwargs):
243246
raise MissingRequiredAttributeException(msg)
244247
self._check_for_update()
245248
self._prepare_objector()
246-
self.requestor = self._prepare_asyncprawcore(requestor_class=requestor_class, requestor_kwargs=requestor_kwargs)
249+
self._prepare_asyncprawcore(requestor_class=requestor_class, requestor_kwargs=requestor_kwargs)
247250

248251
self.auth = models.Auth(self, None)
249252
"""An instance of :class:`.Auth`.
@@ -500,7 +503,7 @@ def _prepare_asyncprawcore(
500503
*,
501504
requestor_class: type[Requestor] | None = None,
502505
requestor_kwargs: Any | None = None,
503-
) -> Requestor:
506+
) -> None:
504507
requestor_class = requestor_class or Requestor
505508
requestor_kwargs = requestor_kwargs or {}
506509

@@ -516,8 +519,6 @@ def _prepare_asyncprawcore(
516519
else:
517520
self._prepare_untrusted_asyncprawcore(requestor)
518521

519-
return requestor
520-
521522
def _prepare_common_authorizer(self, authenticator: asyncprawcore.auth.BaseAuthenticator) -> None:
522523
if self.config.refresh_token:
523524
authorizer = Authorizer(authenticator, refresh_token=self.config.refresh_token)
@@ -608,10 +609,6 @@ async def _resolve_share_url(self, url: str) -> str:
608609
return e.response.headers.get("location")
609610
return url
610611

611-
async def close(self) -> None:
612-
"""Close the requestor."""
613-
await self.requestor.close()
614-
615612
async def comment(
616613
self,
617614
id: str | None = None,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ license = {file = "LICENSE.txt"}
3838
maintainers = [{name = "Joel Payne", email = "lilspazjoekp@gmail.com"}]
3939
name = "asyncpraw"
4040
readme = "README.rst"
41-
requires-python = "~=3.9"
41+
requires-python = ">=3.9"
4242

4343
[project.optional-dependencies]
4444
dev = [

tests/unit/test_config.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import os
22
import sys
33
from pathlib import Path
4+
from unittest import mock
45

56
import pytest
67

7-
from unittest import mock
8-
98
from asyncpraw.config import Config
109
from asyncpraw.exceptions import ClientException
1110

@@ -22,10 +21,10 @@ def _assert_config_read(environment, mock_config):
2221
del os.environ[env_name]
2322
os.environ[environment] = "/MOCK"
2423

25-
module_dir = Path(sys.modules["asyncpraw"].__file__).parent
26-
environ_path = Path("/MOCK") / (".config" if environment == "HOME" else "") / "praw.ini"
24+
environ_path = (
25+
Path("/MOCK") / (".config" if environment == "HOME" else "") / "praw.ini"
26+
)
2727
locations = [
28-
str(module_dir / "praw.ini"),
2928
str(environ_path),
3029
"praw.ini",
3130
]
@@ -82,8 +81,7 @@ def test_load_ini_with_no_config_directory(self, mock_config):
8281
prev_environment[key] = os.environ[key]
8382
del os.environ[key]
8483

85-
module_dir = os.path.dirname(sys.modules["asyncpraw"].__file__)
86-
locations = [os.path.join(module_dir, "praw.ini"), "praw.ini"]
84+
locations = ["praw.ini"]
8785

8886
try:
8987
Config._load_config()

0 commit comments

Comments
 (0)