Fixes: 1) Correct default values and type handling in util constructors; 2) Resolve missing or ambiguous `cast` operations for dynamic typing in tests and views; 3) Address potential issues with fallback/default handling in middleware. Extra: 1) Refactor test cases to ensure stricter adherence to typing hints and valid contracts; 2) Update docstrings to align with new type annotations; 3) Cleanup unused imports and add comments for improved maintainability.
42 lines
980 B
Python
42 lines
980 B
Python
from enum import Enum
|
|
from importlib import import_module
|
|
from typing import Any
|
|
|
|
|
|
def create_object(
|
|
module_name: str, class_name: str, *args: list[Any], **kwargs: dict[Any, Any]
|
|
) -> Any:
|
|
module = import_module(module_name)
|
|
|
|
cls = getattr(module, class_name)
|
|
|
|
return cls(*args, **kwargs)
|
|
|
|
|
|
class LoggingError(Exception):
|
|
pass
|
|
|
|
|
|
class LogLevel(Enum):
|
|
DEBUG = "debug"
|
|
INFO = "info"
|
|
WARNING = "warning"
|
|
ERROR = "error"
|
|
CRITICAL = "critical"
|
|
TRACE = "trace"
|
|
|
|
|
|
class RatelimitedError(Exception):
|
|
default_detail = "Rate limit exceeded. Please try again later."
|
|
default_code = "rate_limited"
|
|
status_code = 429
|
|
|
|
def __init__(self, detail: str | None = None, code: str | None = None):
|
|
if detail is None:
|
|
detail = self.default_detail
|
|
if code is None:
|
|
code = self.default_code
|
|
|
|
self.detail: str | None = detail
|
|
self.code: str | None = code
|
|
super().__init__(detail)
|