From 2e6200296a77bd752f93e484aeeed448df4e287e Mon Sep 17 00:00:00 2001 From: NicKoehler Date: Sun, 18 Jun 2023 19:40:38 +0200 Subject: [PATCH] python 3.8 compatible static typing --- pyproject.toml | 2 +- pythorhead/auth.py | 6 ++++-- pythorhead/lemmy.py | 4 +++- pythorhead/post.py | 42 +++++++++++++++++++++--------------------- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6c812e6..9a201cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ authors = [ ] version = "v0.1.0" readme = "README.md" -requires-python = ">=3.10,<3.11" +requires-python = ">=3.8,<3.11" license = { file="LICENSE" } classifiers = [ "Programming Language :: Python :: 3", diff --git a/pythorhead/auth.py b/pythorhead/auth.py index 0793144..360e0a2 100644 --- a/pythorhead/auth.py +++ b/pythorhead/auth.py @@ -1,3 +1,5 @@ +from typing import Optional + import requests from loguru import logger @@ -15,8 +17,8 @@ def __call__(cls, *args, **kwargs): class Authentication(metaclass=Singleton): - token: str | None = None - api_base_url: str | None = None + token: Optional[str] = None + api_base_url: Optional[str] = None def log_in(self, username_or_email: str, password: str) -> bool: payload = { diff --git a/pythorhead/lemmy.py b/pythorhead/lemmy.py index 94999ee..871f7ed 100644 --- a/pythorhead/lemmy.py +++ b/pythorhead/lemmy.py @@ -1,3 +1,5 @@ +from typing import Optional + import requests from loguru import logger @@ -18,7 +20,7 @@ def __init__(self, api_base_url: str) -> None: def log_in(self, username_or_email: str, password: str) -> bool: return Authentication().log_in(username_or_email, password) - def discover_community(self, community_name: str) -> int | None: + def discover_community(self, community_name: str) -> Optional[int]: if community_name in self._known_communities: return self._known_communities[community_name] try: diff --git a/pythorhead/post.py b/pythorhead/post.py index c5fafdd..ed0c540 100644 --- a/pythorhead/post.py +++ b/pythorhead/post.py @@ -1,4 +1,4 @@ -from typing import Any, Literal +from typing import Any, Literal, Optional, List import requests from loguru import logger @@ -14,7 +14,7 @@ def __init__(self): def get( self, post_id: int, - comment_id: int | None = None, + comment_id: Optional[int] = None, ) -> dict: """ Get a post. @@ -42,14 +42,14 @@ def get( def list( # noqa: A003 self, - community_id: int | None = None, - community_name: str | None = None, - limit: int | None = None, - page: int | None = None, - saved_only: bool | None = None, - sort: SortType | None = None, - type_: ListingType | None = None, - ) -> list[dict]: + community_id: Optional[int] = None, + community_name: Optional[str] = None, + limit: Optional[int] = None, + page: Optional[int] = None, + saved_only: Optional[bool] = None, + sort: Optional[SortType] = None, + type_: Optional[ListingType] = None, + ) -> List[dict]: """ Get posts, with various filters. @@ -95,11 +95,11 @@ def create( self, community_id: int, name: str, - url: str | None = None, - body: str | None = None, - nsfw: bool | None = None, - honeypot: str | None = None, - language_id: int | None = None, + url: Optional[str] = None, + body: Optional[str] = None, + nsfw: Optional[bool] = None, + honeypot: Optional[str] = None, + language_id: Optional[int] = None, ) -> bool: """ Create a post @@ -161,7 +161,7 @@ def delete(self, post_id: int, deleted: bool) -> bool: logger.error(f"Error encountered while deleting post: {re.text}") return re.ok - def remove(self, post_id: int, removed: bool, reason: str | None = None) -> bool: + def remove(self, post_id: int, removed: bool, reason: Optional[str] = None) -> bool: """ Moderator remove / restore a post. @@ -189,11 +189,11 @@ def remove(self, post_id: int, removed: bool, reason: str | None = None) -> bool def edit( self, post_id: int, - name: str | None = None, - url: str | None = None, - body: str | None = None, - nsfw: bool | None = None, - language_id: int | None = None, + name: Optional[str] = None, + url: Optional[str] = None, + body: Optional[str] = None, + nsfw: Optional[bool] = None, + language_id: Optional[int] = None, ) -> bool: """