Skip to content

Commit

Permalink
Implement wraps_handler function
Browse files Browse the repository at this point in the history
  • Loading branch information
Themanwhosmellslikesugar committed Dec 19, 2024
1 parent 11935a1 commit 5055357
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
24 changes: 22 additions & 2 deletions hammett/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""The module contains the types used throughout the framework."""

import sys
from collections.abc import Awaitable, Callable, Coroutine, Iterable, Sequence
from enum import Enum, auto
from typing import TYPE_CHECKING, Any, NewType, Protocol, TypedDict, TypeVar
from typing import TYPE_CHECKING, Any, Literal, NewType, Protocol, TypedDict, TypeVar
from uuid import UUID

import telegram
Expand Down Expand Up @@ -96,3 +96,23 @@ class JobConfig(TypedDict):
Routes = tuple[tuple[set[State], State]]

Source = str | type[Screen] | Handler | HandlerAlias

if sys.version_info >= (3, 12):
HANDLER_ASSIGNMENTS_TYPE = tuple[
Literal['__module__'],
Literal['__name__'],
Literal['__qualname__'],
Literal['__doc__'],
Literal['__annotations__'],
Literal['__type_params__'],
str,
]
else:
HANDLER_ASSIGNMENTS_TYPE = tuple[
Literal['__module__'],
Literal['__name__'],
Literal['__qualname__'],
Literal['__doc__'],
Literal['__annotations__'],
str,
]
26 changes: 26 additions & 0 deletions hammett/utils/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""The module contains tools for working with handlers."""

import functools
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from hammett.types import HANDLER_ASSIGNMENTS_TYPE, Func

HANDLER_ASSIGNMENTS = (*functools.WRAPPER_ASSIGNMENTS, '__self__')


def wraps_handler(
wrapped: 'Func',
assigned: 'HANDLER_ASSIGNMENTS_TYPE' = HANDLER_ASSIGNMENTS,
updated: tuple[str] = functools.WRAPPER_UPDATES,
) -> 'functools.partial[Func]':
"""Identical to `functools.wraps`, the only thing different is the `assigned` argument.
This is required for `calc_checksum` function to return a valid result even
for a decorated handler. Should be used if a decorator for the handler is implemented.
"""
return functools.partial(
functools.update_wrapper,
wrapped=wrapped,
assigned=assigned,
updated=updated,
)

0 comments on commit 5055357

Please sign in to comment.