Skip to content

Commit

Permalink
fix: remove runtime dependency on typing_extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Sep 21, 2024
1 parent 5f2ba9c commit 74d49a6
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 154 deletions.
248 changes: 124 additions & 124 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pydantic = ">=2.9.1"
beautifulsoup4 = ">=4.12.3"
lxml = ">=5.3.0"
torf = ">=4.2.7"
typing-extensions = ">=4.12.2"
strenum = { version = ">=0.4.15", python = "<3.11" }

[tool.poetry.group.dev.dependencies]
Expand All @@ -37,6 +36,7 @@ pytest-asyncio = "^0.23.5.post1"
coverage = "^7.6.1"
types-beautifulsoup4 = "^4.12.0.20240907"
types-xmltodict = "^0.13.0.3"
typing-extensions = "^4.12.2"

[tool.poetry.group.docs.dependencies]
mkdocs-material = "^9.5.34"
Expand Down Expand Up @@ -72,6 +72,11 @@ ignore_missing_imports = true
[tool.coverage.run]
omit = ["src/pynyaa/_version.py", "src/pynyaa/_compat.py", "tests/*"]

[tool.coverage.report]
exclude_also = [
"if TYPE_CHECKING:", # Only used for type-hints
]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
13 changes: 6 additions & 7 deletions src/pynyaa/_clients/_async.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from __future__ import annotations

from io import BytesIO
from types import TracebackType
from typing import Any
from typing import TYPE_CHECKING, Any
from urllib.parse import urljoin

from httpx import AsyncClient
from torf import Torrent
from typing_extensions import AsyncGenerator, Self

from pynyaa._enums import Category, Filter, SortBy
from pynyaa._models import NyaaTorrentPage
from pynyaa._parser import parse_nyaa_search_results, parse_nyaa_torrent_page
from pynyaa._version import __version__

if TYPE_CHECKING:
from typing_extensions import AsyncGenerator, Self


class AsyncNyaa:
def __init__(self, base_url: str = "https://nyaa.si/", client: AsyncClient | None = None) -> None:
Expand All @@ -23,7 +24,7 @@ def __init__(self, base_url: str = "https://nyaa.si/", client: AsyncClient | Non
Parameters
----------
base_url : str, optional
The base URL of Nyaa. Default is `https://nyaa.si/`.
The base URL of Nyaa.
This is used for constructing the full URL from relative URLs.
client : Client, optional
An [httpx.Client](https://www.python-httpx.org/api/#client) instance used to make requests to Nyaa.
Expand All @@ -41,9 +42,7 @@ def base_url(self) -> str:
async def __aenter__(self) -> Self:
return self

async def __aexit__(
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> None:
async def __aexit__(self, *args: object) -> None:
await self.close()

async def close(self) -> None:
Expand Down
11 changes: 5 additions & 6 deletions src/pynyaa/_clients/_sync.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from __future__ import annotations

from io import BytesIO
from types import TracebackType
from typing import Any
from typing import TYPE_CHECKING, Any
from urllib.parse import urljoin

from httpx import Client
from torf import Torrent
from typing_extensions import Generator, Self

from pynyaa._enums import Category, Filter, SortBy
from pynyaa._models import NyaaTorrentPage
from pynyaa._parser import parse_nyaa_search_results, parse_nyaa_torrent_page
from pynyaa._version import __version__

if TYPE_CHECKING:
from typing_extensions import Generator, Self


class Nyaa:
def __init__(self, base_url: str = "https://nyaa.si/", client: Client | None = None) -> None:
Expand Down Expand Up @@ -41,9 +42,7 @@ def base_url(self) -> str:
def __enter__(self) -> Self:
return self

def __exit__(
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> None:
def __exit__(self, *args: object) -> None:
self.close()

def close(self) -> None:
Expand Down
21 changes: 12 additions & 9 deletions src/pynyaa/_enums.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
from __future__ import annotations

from typing import overload

from typing_extensions import Self
from typing import TYPE_CHECKING, overload

from pynyaa._compat import IntEnum, StrEnum
from pynyaa._types import CategoryID, CategoryLiteral, SortByLiteral
from pynyaa._utils import _get_category_id_from_name

if TYPE_CHECKING:
from typing_extensions import Self


class BaseStrEnum(StrEnum):
"""StrEnum with case-insensitive double-sided lookup"""

@classmethod
def _missing_(cls, value: object) -> Self:
caseless_value = str(value).casefold()
for member in cls:
if (member.value.casefold() == caseless_value) or (member.name.casefold() == caseless_value):
return member
message = f"'{value}' is not a valid {cls.__name__}"
raise ValueError(message)
errmsg = f"'{value}' is not a valid {cls.__name__}"

if isinstance(value, str):
for member in cls:
if (member.value.casefold() == value.casefold()) or (member.name.casefold() == value.casefold()):
return member
raise ValueError(errmsg)
raise ValueError(errmsg)


class Category(BaseStrEnum):
Expand Down
4 changes: 2 additions & 2 deletions src/pynyaa/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from pydantic import BaseModel, ConfigDict, HttpUrl, field_serializer, field_validator
from torf import Torrent

from ._enums import Category
from ._types import MagnetUrl, UTCDateTime
from pynyaa._enums import Category
from pynyaa._types import MagnetUrl, UTCDateTime


class ParentModel(BaseModel):
Expand Down
6 changes: 4 additions & 2 deletions src/pynyaa/_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import re
from typing import Any
from typing import TYPE_CHECKING, Any
from urllib.parse import urljoin

from bs4 import BeautifulSoup
from typing_extensions import Generator

if TYPE_CHECKING:
from typing_extensions import Generator


def parse_nyaa_torrent_page(base_url: str, html: str) -> dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion src/pynyaa/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import TYPE_CHECKING

if TYPE_CHECKING: # pragma: no cover
if TYPE_CHECKING:
from pynyaa._types import CategoryID


Expand Down
3 changes: 1 addition & 2 deletions src/pynyaa/_version.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from importlib import metadata

from typing_extensions import NamedTuple
from typing import NamedTuple


class Version(NamedTuple):
Expand Down

0 comments on commit 74d49a6

Please sign in to comment.