Skip to content

added typing to function arguments and return types to pass linting c… #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions fastapi_permissions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def show_item(item:Item = Permission("view", get_item)):

import functools
import itertools
from typing import Any
from typing import Any, Dict, List

from fastapi import Depends, HTTPException
from starlette.status import HTTP_403_FORBIDDEN
Expand All @@ -73,11 +73,11 @@ class _AllPermissions:
but it turns out to be readonly...
"""

def __contains__(self, other):
def __contains__(self, other: Any) -> bool:
""" returns alway true any permission """
return True

def __str__(self):
def __str__(self) -> str:
""" string representation """
return "permissions:*"

Expand All @@ -101,7 +101,7 @@ def __str__(self):
def configure_permissions(
active_principals_func: Any,
permission_exception: HTTPException = permission_exception,
):
) -> functools.partial:
""" sets the basic configuration for the permissions system

active_principals_func:
Expand All @@ -126,7 +126,7 @@ def permission_dependency_factory(
resource: Any,
active_principals_func: Any,
permission_exception: HTTPException,
):
) -> Any:
""" returns a function that acts as a dependable for checking permissions

This is the actual function used for creating the permission dependency,
Expand All @@ -152,9 +152,7 @@ def permission_dependency_factory(
# to get the caller signature right, we need to add only the resource and
# user dependable in the definition
# the permission itself is available through the outer function scope
def permission_dependency(
resource=dependable_resource, principals=active_principals_func
):
def permission_dependency(resource: Any = dependable_resource, principals: Any = active_principals_func) -> Any:
if has_permission(principals, permission, resource):
return resource
raise permission_exception
Expand All @@ -164,7 +162,7 @@ def permission_dependency(

def has_permission(
user_principals: list, requested_permission: str, resource: Any
):
) -> bool:
""" checks if a user has the permission for a resource

The order of the function parameters can be remembered like "Joe eat apple"
Expand All @@ -186,7 +184,7 @@ def has_permission(
return False


def list_permissions(user_principals: list, resource: Any):
def list_permissions(user_principals: list, resource: Any) -> Dict[str, bool]:
""" lists all permissions of a user for a resouce

user_principals: the principals of a user
Expand All @@ -209,7 +207,7 @@ def list_permissions(user_principals: list, resource: Any):
# utility functions


def normalize_acl(resource: Any):
def normalize_acl(resource: Any) -> List[Any]:
""" returns the access controll list for a resource

If the resource is not an acl list itself it needs to have an "__acl__"
Expand All @@ -229,7 +227,7 @@ def normalize_acl(resource: Any):
return []


def is_like_list(something):
def is_like_list(something) -> bool:
""" checks if something is iterable but not a string """
if isinstance(something, str):
return False
Expand Down
48 changes: 48 additions & 0 deletions fastapi_permissions/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
__version__: str

import functools
from typing import Any, Dict, List, Tuple

from fastapi import HTTPException

Allow: str
Deny: str

Everyone: str
Authenticated: str

class _AllPermissions:
def __contains__(self, other: Any) -> bool: ...
def __str__(self) -> str: ...

All: Any

DENY_ALL: Tuple[Any, Any, Any]
ALOW_ALL: Tuple[Any, Any, Any]

permission_exception: HTTPException

def configure_permissions(
active_principals_func: Any,
permission_exception: HTTPException,
) -> functools.partial: ...

def permission_dependency_factory(
permission: str,
resource: Any,
active_principals_func: Any,
permission_exception: HTTPException,
) -> Any:
...

def permission_dependency(resource: Any, principals: Any) -> Any: ...

def has_permission(
user_principals: list, requested_permission: str, resource: Any
) -> bool: ...

def list_permissions(user_principals: list, resource: Any) -> Dict[str, bool]: ...

def normalize_acl(resource: Any) -> List[Any]: ...

def is_like_list(something: Any) -> bool: ...