type hinting enhancements, bugfix in user commands handler
Some checks failed
Build Docs / changes (push) Failing after 2s
Build Docs / build-docs (push) Has been skipped
Build Docs / deploy-docs (push) Has been skipped

This commit is contained in:
Alexander Kalinovsky
2025-05-12 18:21:30 +07:00
parent dcacd31bbc
commit eb57a4ff78
9 changed files with 201 additions and 174 deletions

View File

@@ -7,6 +7,7 @@ from babel.support import LazyProxy
from dataclasses import dataclass, field
from fastapi.datastructures import State
from sqlmodel.ext.asyncio.session import AsyncSession
from sqlalchemy.orm import InstrumentedAttribute
from .role import RoleBase
from . import EntityPermission
@@ -23,26 +24,25 @@ if TYPE_CHECKING:
@dataclass
class FieldEditButton:
field_name: str
caption: str | LazyProxy | Callable[["BotEntity", "BotContext"], str] | None = None
visibility: Callable[["BotEntity", "BotContext"], bool] | None = None
class FieldEditButton[T: "BotEntity"]:
field: str | Callable[[type[T]], InstrumentedAttribute]
caption: str | LazyProxy | Callable[[T, "BotContext"], str] | None = None
visibility: Callable[[T, "BotContext"], bool] | None = None
@dataclass
class CommandButton:
command: ContextData | Callable[["BotEntity", "BotContext"], ContextData] | str
caption: str | LazyProxy | Callable[["BotEntity", "BotContext"], str] | None = None
visibility: Callable[["BotEntity", "BotContext"], bool] | None = None
class CommandButton[T: "BotEntity"]:
command: ContextData | Callable[[T, "BotContext"], ContextData] | str
caption: str | LazyProxy | Callable[[T, "BotContext"], str] | None = None
visibility: Callable[[T, "BotContext"], bool] | None = None
@dataclass
class InlineButton:
class InlineButton[T: "BotEntity"]:
inline_button: (
InlineKeyboardButton
| Callable[["BotEntity", "BotContext"], InlineKeyboardButton]
InlineKeyboardButton | Callable[[T, "BotContext"], InlineKeyboardButton]
)
visibility: Callable[["BotEntity", "BotContext"], bool] | None = None
visibility: Callable[[T, "BotContext"], bool] | None = None
@dataclass
@@ -69,11 +69,11 @@ class Filter:
@dataclass
class EntityList:
class EntityList[T: "BotEntity"]:
caption: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
) = None
item_repr: Callable[["BotEntity", "BotContext"], str] | None = None
item_repr: Callable[[T, "BotContext"], str] | None = None
show_add_new_button: bool = True
item_form: str | None = None
pagination: bool = True
@@ -84,16 +84,17 @@ class EntityList:
@dataclass
class EntityForm:
item_repr: Callable[["BotEntity", "BotContext"], str] | None = None
class EntityForm[T: "BotEntity"]:
item_repr: Callable[[T, "BotContext"], str] | None = None
edit_field_sequence: list[str] = None
form_buttons: list[list[FieldEditButton | CommandButton | InlineButton]] = None
show_edit_button: bool = True
show_delete_button: bool = True
before_open: Callable[[T, "BotContext"], None] | None = None
@dataclass(kw_only=True)
class _BaseFieldDescriptor:
class _BaseFieldDescriptor[T: "BotEntity"]:
icon: str = None
caption: (
str | LazyProxy | Callable[["FieldDescriptor", "BotContext"], str] | None
@@ -104,19 +105,17 @@ class _BaseFieldDescriptor:
edit_prompt: (
str
| LazyProxy
| Callable[["FieldDescriptor", Union["BotEntity", Any], "BotContext"], str]
| Callable[["FieldDescriptor", Union[T, Any], "BotContext"], str]
| None
) = None
caption_value: (
Callable[["FieldDescriptor", Union["BotEntity", Any], "BotContext"], str] | None
) = None
is_visible: (
bool | Callable[["FieldDescriptor", "BotEntity", "BotContext"], bool] | None
Callable[["FieldDescriptor", Union[T, Any], "BotContext"], str] | None
) = None
is_visible: bool | Callable[["FieldDescriptor", T, "BotContext"], bool] | None = (
None
)
is_visible_in_edit_form: (
bool
| Callable[["FieldDescriptor", Union["BotEntity", Any], "BotContext"], bool]
| None
bool | Callable[["FieldDescriptor", Union[T, Any], "BotContext"], bool] | None
) = None
validator: Callable[[Any, "BotContext"], Union[bool, str]] | None = None
localizable: bool = False
@@ -126,12 +125,14 @@ class _BaseFieldDescriptor:
ep_parent_field: str | None = None
ep_child_field: str | None = None
dt_type: Literal["date", "datetime"] = "date"
options: list[Any] | Callable[[T, "BotContext"], list[Any]] | None = None
show_skip_in_editor: Literal[False, "Auto"] = "Auto"
default: Any = None
default_factory: Callable[[], Any] | None = None
@dataclass(kw_only=True)
class EntityField(_BaseFieldDescriptor):
class EntityField[T: "BotEntity"](_BaseFieldDescriptor[T]):
name: str | None = None
sm_descriptor: Any = None
@@ -142,7 +143,7 @@ class Setting(_BaseFieldDescriptor):
@dataclass(kw_only=True)
class FormField(_BaseFieldDescriptor):
class FormField[T: "BotEntity"](_BaseFieldDescriptor[T]):
name: str | None = None
type_: type
@@ -163,7 +164,7 @@ class FieldDescriptor(_BaseFieldDescriptor):
@dataclass(kw_only=True)
class _BaseEntityDescriptor:
class _BaseEntityDescriptor[T: "BotEntity"]:
icon: str = "📘"
full_name: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
@@ -174,7 +175,7 @@ class _BaseEntityDescriptor:
description: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
) = None
item_repr: Callable[["BotEntity", "BotContext"], str] | None = None
item_repr: Callable[[T, "BotContext"], str] | None = None
default_list: EntityList = field(default_factory=EntityList)
default_form: EntityForm = field(default_factory=EntityForm)
lists: dict[str, EntityList] = field(default_factory=dict[str, EntityList])
@@ -196,23 +197,19 @@ class _BaseEntityDescriptor:
}
)
before_create: Callable[["BotContext"], Union[bool, str]] | None = None
before_create_save: (
Callable[["BotEntity", "BotContext"], Union[bool, str]] | None
) = None
before_create_save: Callable[[T, "BotContext"], Union[bool, str]] | None = None
before_update_save: (
Callable[[dict[str, Any], dict[str, Any], "BotContext"], Union[bool, str]]
| None
) = None
before_delete: Callable[["BotEntity", "BotContext"], Union[bool, str]] | None = None
on_created: Callable[["BotEntity", "BotContext"], None] | None = None
on_deleted: Callable[["BotEntity", "BotContext"], None] | None = None
on_updated: Callable[[dict[str, Any], "BotEntity", "BotContext"], None] | None = (
None
)
before_delete: Callable[[T, "BotContext"], Union[bool, str]] | None = None
on_created: Callable[[T, "BotContext"], None] | None = None
on_deleted: Callable[[T, "BotContext"], None] | None = None
on_updated: Callable[[dict[str, Any], T, "BotContext"], None] | None = None
@dataclass(kw_only=True)
class Entity(_BaseEntityDescriptor):
class Entity[T: "BotEntity"](_BaseEntityDescriptor[T]):
name: str | None = None
@@ -252,6 +249,7 @@ class BotContext[UT: UserBase]:
app_state: State
user: UT
message: Message | CallbackQuery | None = None
default_handler: Callable[["BotEntity", "BotContext"], None] | None = None
@dataclass(kw_only=True)