Skip to content

Commit

Permalink
Uplift types
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee committed Jun 13, 2024
1 parent c2dd278 commit f0b4e30
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions sqladmin/application.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import inspect
import io
import logging
Expand All @@ -7,12 +9,7 @@
Any,
Awaitable,
Callable,
List,
Optional,
Sequence,
Tuple,
Type,
Union,
cast,
no_type_check,
)
Expand Down Expand Up @@ -66,14 +63,14 @@ class BaseAdmin:
def __init__(
self,
app: Starlette,
engine: Optional[ENGINE_TYPE] = None,
session_maker: Optional[sessionmaker] = None,
engine: ENGINE_TYPE | None = None,
session_maker: sessionmaker | None = None,
base_url: str = "/admin",
title: str = "Admin",
logo_url: Optional[str] = None,
logo_url: str | None = None,
templates_dir: str = "templates",
middlewares: Optional[Sequence[Middleware]] = None,
authentication_backend: Optional[AuthenticationBackend] = None,
middlewares: Sequence[Middleware] | None = None,
authentication_backend: AuthenticationBackend | None = None,
) -> None:
self.app = app
self.engine = engine
Expand All @@ -100,7 +97,7 @@ def __init__(

self.admin = Starlette(middleware=middlewares)
self.templates = self.init_templating_engine()
self._views: List[Union[BaseView, ModelView]] = []
self._views: list[BaseView | ModelView] = []
self._menu = Menu()

def init_templating_engine(self) -> Jinja2Templates:
Expand All @@ -120,7 +117,7 @@ def init_templating_engine(self) -> Jinja2Templates:
return templates

@property
def views(self) -> List[Union[BaseView, ModelView]]:
def views(self) -> list[BaseView | ModelView]:
"""Get list of ModelView and BaseView instances lazily.
Returns:
Expand All @@ -136,7 +133,7 @@ def _find_model_view(self, identity: str) -> ModelView:

raise HTTPException(status_code=404)

def add_view(self, view: Union[Type[ModelView], Type[BaseView]]) -> None:
def add_view(self, view: type[ModelView] | type[BaseView]) -> None:
"""Add ModelView or BaseView classes to Admin.
This is a shortcut that will handle both `add_model_view` and `add_base_view`.
"""
Expand All @@ -149,10 +146,10 @@ def add_view(self, view: Union[Type[ModelView], Type[BaseView]]) -> None:

def _find_decorated_funcs(
self,
view: Type[Union[BaseView, ModelView]],
view_instance: Union[BaseView, ModelView],
view: type[BaseView | ModelView],
view_instance: BaseView | ModelView,
handle_fn: Callable[
[MethodType, Type[Union[BaseView, ModelView]], Union[BaseView, ModelView]],
[MethodType, type[BaseView | ModelView], BaseView | ModelView],
None,
],
) -> None:
Expand All @@ -164,8 +161,8 @@ def _find_decorated_funcs(
def _handle_action_decorated_func(
self,
func: MethodType,
view: Type[Union[BaseView, ModelView]],
view_instance: Union[BaseView, ModelView],
view: type[BaseView | ModelView],
view_instance: BaseView | ModelView,
) -> None:
if hasattr(func, "_action"):
view_instance = cast(ModelView, view_instance)
Expand Down Expand Up @@ -194,8 +191,8 @@ def _handle_action_decorated_func(
def _handle_expose_decorated_func(
self,
func: MethodType,
view: Type[Union[BaseView, ModelView]],
view_instance: Union[BaseView, ModelView],
view: type[BaseView | ModelView],
view_instance: BaseView | ModelView,
) -> None:
if hasattr(func, "_exposed"):
self.admin.add_route(
Expand All @@ -208,7 +205,7 @@ def _handle_expose_decorated_func(

view.identity = getattr(func, "_identity")

def add_model_view(self, view: Type[ModelView]) -> None:
def add_model_view(self, view: type[ModelView]) -> None:
"""Add ModelView to the Admin.
???+ usage
Expand Down Expand Up @@ -237,7 +234,7 @@ class UserAdmin(ModelView, model=User):
self._views.append(view_instance)
self._build_menu(view_instance)

def add_base_view(self, view: Type[BaseView]) -> None:
def add_base_view(self, view: type[BaseView]) -> None:
"""Add BaseView to the Admin.
???+ usage
Expand Down Expand Up @@ -265,7 +262,7 @@ async def test_page(self, request: Request):
self._views.append(view_instance)
self._build_menu(view_instance)

def _build_menu(self, view: Union[ModelView, BaseView]) -> None:
def _build_menu(self, view: ModelView | BaseView) -> None:
if view.category:
menu = CategoryMenu(name=view.category)
menu.add_child(ViewMenu(view=view, name=view.name, icon=view.icon))
Expand Down Expand Up @@ -338,15 +335,15 @@ class UserAdmin(ModelView, model=User):
def __init__(
self,
app: Starlette,
engine: Optional[ENGINE_TYPE] = None,
session_maker: Optional[Union[sessionmaker, "async_sessionmaker"]] = None,
engine: ENGINE_TYPE | None = None,
session_maker: sessionmaker | "async_sessionmaker" | None = None,
base_url: str = "/admin",
title: str = "Admin",
logo_url: Optional[str] = None,
middlewares: Optional[Sequence[Middleware]] = None,
logo_url: str | None = None,
middlewares: Sequence[Middleware] | None = None,
debug: bool = False,
templates_dir: str = "templates",
authentication_backend: Optional[AuthenticationBackend] = None,
authentication_backend: AuthenticationBackend | None = None,
) -> None:
"""
Args:
Expand Down Expand Up @@ -374,7 +371,7 @@ def __init__(

async def http_exception(
request: Request, exc: Exception
) -> Union[Response, Awaitable[Response]]:
) -> Response | Awaitable[Response]:
assert isinstance(exc, HTTPException)
context = {
"status_code": exc.status_code,
Expand Down Expand Up @@ -662,7 +659,7 @@ async def ajax_lookup(self, request: Request) -> Response:

def get_save_redirect_url(
self, request: Request, form: FormData, model_view: ModelView, obj: Any
) -> Union[str, URL]:
) -> str | URL:
"""
Get the redirect URL after a save action
which is triggered from create/edit page.
Expand All @@ -687,7 +684,7 @@ async def _handle_form_data(self, request: Request, obj: Any = None) -> FormData
"""

form = await request.form()
form_data: List[Tuple[str, Union[str, UploadFile]]] = []
form_data: list[tuple[str, str | UploadFile]] = []
for key, value in form.multi_items():
if not isinstance(value, UploadFile):
form_data.append((key, value))
Expand Down Expand Up @@ -728,8 +725,8 @@ def _denormalize_wtform_data(self, form_data: dict, obj: Any) -> dict:
def expose(
path: str,
*,
methods: List[str] = ["GET"],
identity: Optional[str] = None,
methods: list[str] = ["GET"],
identity: str | None = None,
include_in_schema: bool = True,
) -> Callable[..., Any]:
"""Expose View with information."""
Expand All @@ -748,8 +745,8 @@ def wrap(func):

def action(
name: str,
label: Optional[str] = None,
confirmation_message: Optional[str] = None,
label: str | None = None,
confirmation_message: str | None = None,
*,
include_in_schema: bool = True,
add_in_detail: bool = True,
Expand Down

0 comments on commit f0b4e30

Please sign in to comment.