Improved type hints and added a placeholder for guilds

This commit is contained in:
Profitroll 2025-02-16 13:41:23 +01:00
parent ffcfbbfc3b
commit 4ad79f1445
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
4 changed files with 26 additions and 18 deletions

3
classes/__init__.py Normal file
View File

@ -0,0 +1,3 @@
from .pycord_guild import PycordGuild
from .pycord_guild_colors import PycordGuildColor
from .pycord_user import PycordUser

View File

@ -1,10 +1,10 @@
from typing import Any, Union
from typing import Any
from aiohttp import ClientSession
from discord import User
from libbot.pycord.classes import PycordBot as LibPycordBot
from classes.pycord_user import PycordUser
from classes import PycordUser
from modules.cache_manager import create_cache_client
@ -12,7 +12,7 @@ from modules.cache_manager import create_cache_client
class PycordBot(LibPycordBot):
def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._set_cache_engine()
@ -34,16 +34,18 @@ class PycordBot(LibPycordBot):
if "cache" in self.config and self.config["cache"]["type"] is not None:
self.cache = create_cache_client(self.config, self.config["cache"]["type"])
async def find_user(self, user: Union[int, User]) -> PycordUser:
async def find_user(self, user: int | User) -> PycordUser:
"""Find User by its ID or User object.
### Args:
* user (`Union[int, User]`): ID or User object to extract ID from.
Args:
user (int | User): ID or User object to extract ID from
### Returns:
* `PycordUser`: User in database representation.
Returns:
PycordUser: User object
Raises:
UserNotFoundException: User was not found and creation was not allowed
"""
return (
await PycordUser.from_id(user, cache=self.cache)
if isinstance(user, int)

3
classes/pycord_guild.py Normal file
View File

@ -0,0 +1,3 @@
class PycordGuild:
def __init__(self) -> None:
raise NotImplementedError()

View File

@ -1,16 +1,16 @@
from importlib.util import module_from_spec, spec_from_file_location
import logging
from importlib.util import module_from_spec, spec_from_file_location
from os import getcwd, walk
from pathlib import Path
from types import ModuleType
from typing import List, Union
from typing import List
logger = logging.getLogger(__name__)
# Import functions
# Took from https://stackoverflow.com/a/57892961
def get_py_files(src: Union[str, Path]) -> List[str]:
def get_py_files(src: str | Path) -> List[str]:
cwd = getcwd() # Current Working directory
py_files = []
@ -22,7 +22,7 @@ def get_py_files(src: Union[str, Path]) -> List[str]:
return py_files
def dynamic_import(module_name: str, py_path: str) -> Union[ModuleType, None]:
def dynamic_import(module_name: str, py_path: str) -> ModuleType | None:
try:
module_spec = spec_from_file_location(module_name, py_path)
@ -38,7 +38,7 @@ def dynamic_import(module_name: str, py_path: str) -> Union[ModuleType, None]:
"Could not load extension %s due to spec loader being None.",
module_name,
)
return
return None
module_spec.loader.exec_module(module)
@ -48,13 +48,13 @@ def dynamic_import(module_name: str, py_path: str) -> Union[ModuleType, None]:
"Could not load extension %s due to invalid syntax. Check logs/errors.log for details.",
module_name,
)
return
return None
except Exception as exc:
logger.warning("Could not load extension %s due to %s", module_name, exc)
return
return None
def dynamic_import_from_src(src: Union[str, Path], star_import=False) -> None:
def dynamic_import_from_src(src: str | Path, star_import=False) -> None:
my_py_files = get_py_files(src)
for py_file in my_py_files:
@ -64,7 +64,7 @@ def dynamic_import_from_src(src: Union[str, Path], star_import=False) -> None:
imported_module = dynamic_import(module_name, py_file)
if imported_module != None:
if imported_module is not None:
if star_import:
for obj in dir(imported_module):
globals()[obj] = imported_module.__dict__[obj]