Skip to content

Commit

Permalink
Update mypy and internals (#490)
Browse files Browse the repository at this point in the history
Update mypy
  • Loading branch information
tarsil authored Feb 9, 2025
1 parent c7667b1 commit bfbfc75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
40 changes: 38 additions & 2 deletions esmerald/permissions/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import TYPE_CHECKING, Callable, Optional
from typing import TYPE_CHECKING, Any, Callable, Optional, Union, cast

from lilya.permissions.base import DefinePermission

from esmerald.exceptions import PermissionDenied
from esmerald.utils.helpers import is_async_callable
from esmerald.utils.helpers import is_async_callable, is_class_and_subclass

if TYPE_CHECKING: # pragma: no cover
from esmerald.permissions import BasePermission
Expand Down Expand Up @@ -40,3 +42,37 @@ def permission_denied(request: "Request", message: Optional[str] = None) -> None
If request is not permitted, determine what kind of exception to raise.
"""
raise PermissionDenied(detail=message, status_code=403)


def is_esmerald_permission(permission: Union["BasePermission", Any]) -> bool:
"""
Checks if the given permission is an instance or subclass of BasePermission.
Args:
permission (Union["BasePermission", Any]): The permission to check.
Returns:
bool: True if the permission is an instance or subclass of BasePermission, False otherwise.
"""

from esmerald.permissions import BasePermission

return is_class_and_subclass(permission, BasePermission)


def wrap_permission(
permission: Union["BasePermission", Any],
) -> "BasePermission":
"""
Wraps the given permission into a BasePermission instance if it is not already one.
Or else it will assume its a Lilya permission and wraps it.
Args:
permission (Union["BasePermission", Any]): The permission to be wrapped.
Returns:
BasePermission: The wrapped permission instance.
"""

if is_esmerald_permission(permission):
return permission

return cast("BasePermission", DefinePermission(cast(Any, permission)))
2 changes: 1 addition & 1 deletion esmerald/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def __call__(self, test_func: Any) -> Any:
"""

@wraps(test_func)
def inner(*args: P.args, **kwargs: P.kwargs) -> Any:
def inner(*args: Any, **kwargs: Any) -> Any:
with self:
return test_func(*args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ testing = [
"flask>=1.1.2,<4.0.0",
"freezegun>=1.2.2,<2.0.0",
"mongoz>=0.6.0",
"mypy==1.14.1",
"mypy==1.15.0",
"pytest>=7.1.3,<9.0.0",
"pytest-cov>=4.1.0,<7.0.0",
"pytest-asyncio>=0.20.0",
Expand Down

0 comments on commit bfbfc75

Please sign in to comment.