Skip to content
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

fix: correct typehints in project #395

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
33 changes: 24 additions & 9 deletions src/aiogram_dialog/api/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
__all__ = [
"Context", "Data",
"Context",
"Data",
"ChatEvent",
"LaunchMode",
"MediaAttachment", "MediaId",
"ShowMode", "StartMode",
"MarkupVariant", "NewMessage", "OldMessage", "UnknownText",
"DEFAULT_STACK_ID", "Stack",
"DIALOG_EVENT_NAME", "DialogAction", "DialogUpdateEvent",
"DialogStartEvent", "DialogSwitchEvent", "DialogUpdate",
"MediaAttachment",
"MediaId",
"ShowMode",
"StartMode",
"MarkupVariant",
"NewMessage",
"OldMessage",
"UnknownText",
"DEFAULT_STACK_ID",
"Stack",
"DIALOG_EVENT_NAME",
"DialogAction",
"DialogUpdateEvent",
"DialogStartEvent",
"DialogSwitchEvent",
"DialogUpdate",
]

from .context import Context, Data
Expand All @@ -18,6 +29,10 @@
from .new_message import MarkupVariant, NewMessage, OldMessage, UnknownText
from .stack import DEFAULT_STACK_ID, Stack
from .update_event import (
DIALOG_EVENT_NAME, DialogAction, DialogStartEvent, DialogSwitchEvent,
DialogUpdate, DialogUpdateEvent,
DIALOG_EVENT_NAME,
DialogAction,
DialogStartEvent,
DialogSwitchEvent,
DialogUpdate,
DialogUpdateEvent,
)
3 changes: 2 additions & 1 deletion src/aiogram_dialog/api/entities/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from aiogram.fsm.state import State

Data = Union[Dict, List, int, str, float, None]
SerializationData = Union[int, str, float, None]
Data = Union[Dict[SerializationData, "Data"], List["Data"], SerializationData]
DataDict = Dict[str, Data]


Expand Down
11 changes: 6 additions & 5 deletions src/aiogram_dialog/api/entities/events.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Union

from aiogram.types import (
CallbackQuery, ChatJoinRequest, ChatMemberUpdated, Message,
)
from aiogram.types import CallbackQuery, ChatJoinRequest, ChatMemberUpdated, Message

from .update_event import DialogUpdateEvent

ChatEvent = Union[
CallbackQuery, Message, DialogUpdateEvent,
ChatMemberUpdated, ChatJoinRequest,
CallbackQuery,
Message,
DialogUpdateEvent,
ChatMemberUpdated,
ChatJoinRequest,
]
36 changes: 18 additions & 18 deletions src/aiogram_dialog/api/entities/media.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Union
from typing import Any, Optional, Union

from aiogram.types import ContentType

Expand All @@ -10,8 +10,8 @@ class MediaId:
file_id: str
file_unique_id: Optional[str] = None

def __eq__(self, other):
if type(other) is not MediaId:
def __eq__(self, other: object) -> bool:
if not isinstance(other, MediaId):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

original code was correct

return False
if self.file_unique_id is None or other.file_unique_id is None:
return self.file_id == other.file_id
Expand All @@ -20,13 +20,13 @@ def __eq__(self, other):

class MediaAttachment:
def __init__(
self,
type: ContentType,
url: Optional[str] = None,
path: Union[str, Path, None] = None,
file_id: Optional[MediaId] = None,
use_pipe: bool = False,
**kwargs,
self,
type: ContentType,
url: Optional[str] = None,
path: Union[str, Path, None] = None,
file_id: Optional[MediaId] = None,
use_pipe: bool = False,
**kwargs: Any,
):
if not (url or path or file_id):
raise ValueError("Neither url nor path not file_id are provided")
Expand All @@ -37,14 +37,14 @@ def __init__(
self.use_pipe = use_pipe
self.kwargs = kwargs

def __eq__(self, other):
if type(other) is not type(self):
def __eq__(self, other: object) -> bool:
if not isinstance(other, MediaAttachment):
return False
return (
self.type == other.type and
self.url == other.url and
self.path == other.path and
self.file_id == other.file_id and
self.use_pipe == other.use_pipe and
self.kwargs == other.kwargs
self.type == other.type
and self.url == other.url
and self.path == other.path
and self.file_id == other.file_id
and self.use_pipe == other.use_pipe
and self.kwargs == other.kwargs
)
10 changes: 8 additions & 2 deletions src/aiogram_dialog/api/entities/new_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
from typing import Optional, Union

from aiogram.types import (
Chat, ForceReply, InlineKeyboardMarkup, ReplyKeyboardMarkup,
Chat,
ForceReply,
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
)

from aiogram_dialog.api.entities import MediaAttachment, ShowMode

MarkupVariant = Union[
ForceReply, InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove,
ForceReply,
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
]


Expand Down
16 changes: 9 additions & 7 deletions src/aiogram_dialog/api/entities/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from aiogram.fsm.state import State

from aiogram_dialog.api.exceptions import DialogStackOverflow

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we had a discussion that this is probably can be configured

from .context import Context, Data

DEFAULT_STACK_ID = ""
Expand All @@ -29,7 +30,7 @@ def id_to_str(int_id: int) -> str:
return res


def new_id():
def new_id() -> str:
return id_to_str(new_int_id())


Expand All @@ -42,11 +43,12 @@ class Stack:
last_media_id: Optional[str] = field(compare=False, default=None)
last_media_unique_id: Optional[str] = field(compare=False, default=None)
last_income_media_group_id: Optional[str] = field(
compare=False, default=None,
compare=False,
default=None,
)

@property
def id(self):
def id(self) -> str:
return self._id

def push(self, state: State, data: Data) -> Context:
Expand All @@ -64,14 +66,14 @@ def push(self, state: State, data: Data) -> Context:
self.intents.append(context.id)
return context

def pop(self):
def pop(self) -> str:
return self.intents.pop()

def last_intent_id(self):
def last_intent_id(self) -> str:
return self.intents[-1]

def empty(self):
def empty(self) -> bool:
return not self.intents

def default(self):
def default(self) -> bool:
return self.id == DEFAULT_STACK_ID
12 changes: 2 additions & 10 deletions src/aiogram_dialog/api/entities/update_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@
from typing import Any, Optional

from aiogram.fsm.state import State
from aiogram.types import (
Chat,
TelegramObject,
Update,
User,
)
from aiogram.types import Chat, TelegramObject, Update, User
from pydantic import ConfigDict

from .modes import (
ShowMode,
StartMode,
)
from .modes import ShowMode, StartMode

DIALOG_EVENT_NAME = "aiogd_update"

Expand Down
2 changes: 1 addition & 1 deletion src/aiogram_dialog/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnknownIntent(DialogsError):


class OutdatedIntent(DialogsError):
def __init__(self, stack_id, text):
def __init__(self, stack_id: str, text: str) -> None:
super().__init__(text)
self.stack_id = stack_id

Expand Down
41 changes: 30 additions & 11 deletions src/aiogram_dialog/api/internal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
__all__ = [
"FakeChat", "FakeUser", "ReplyCallbackQuery",
"FakeChat",
"FakeUser",
"ReplyCallbackQuery",
"DialogManagerFactory",
"CALLBACK_DATA_KEY", "CONTEXT_KEY", "EVENT_SIMULATED",
"STACK_KEY", "STORAGE_KEY",
"ButtonVariant", "DataGetter", "InputWidget", "KeyboardWidget",
"MediaWidget", "RawKeyboard", "TextWidget", "Widget",
"CALLBACK_DATA_KEY",
"CONTEXT_KEY",
"EVENT_SIMULATED",
"STACK_KEY",
"STORAGE_KEY",
"ButtonVariant",
"DataGetter",
"InputWidget",
"KeyboardWidget",
"MediaWidget",
"RawKeyboard",
"TextWidget",
"Widget",
"WindowProtocol",
]

from .fake_data import FakeChat, FakeUser, ReplyCallbackQuery
from .manager import (
DialogManagerFactory,
)
from .manager import DialogManagerFactory
from .middleware import (
CALLBACK_DATA_KEY, CONTEXT_KEY, EVENT_SIMULATED, STACK_KEY, STORAGE_KEY,
CALLBACK_DATA_KEY,
CONTEXT_KEY,
EVENT_SIMULATED,
STACK_KEY,
STORAGE_KEY,
)
from .widgets import (
ButtonVariant, DataGetter, InputWidget, KeyboardWidget,
MediaWidget, RawKeyboard, TextWidget, Widget,
ButtonVariant,
DataGetter,
InputWidget,
KeyboardWidget,
MediaWidget,
RawKeyboard,
TextWidget,
Widget,
)
from .window import WindowProtocol
7 changes: 1 addition & 6 deletions src/aiogram_dialog/api/internal/fake_data.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from typing import Any, Literal

from aiogram.methods import AnswerCallbackQuery
from aiogram.types import (
CallbackQuery,
Chat,
Message,
User,
)
from aiogram.types import CallbackQuery, Chat, Message, User


class ReplyCallbackQuery(CallbackQuery):
Expand Down
14 changes: 7 additions & 7 deletions src/aiogram_dialog/api/internal/manager.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from abc import abstractmethod
from typing import Dict, Protocol
from typing import Any, Dict, Protocol

from aiogram import Router

from aiogram_dialog.api.entities import ChatEvent
from aiogram_dialog.api.protocols import (
DialogManager, DialogRegistryProtocol,
)
from aiogram_dialog.api.protocols import DialogManager, DialogRegistryProtocol


class DialogManagerFactory(Protocol):
@abstractmethod
def __call__(
self, event: ChatEvent, data: Dict,
registry: DialogRegistryProtocol,
router: Router,
self,
event: ChatEvent,
data: Dict[str, Any],
registry: DialogRegistryProtocol,
router: Router,
) -> DialogManager:
raise NotImplementedError
Loading
Loading