| 1 |
from functools import wraps |
| 2 |
from typing import TYPE_CHECKING, TypeVar, cast |
| 3 |
|
| 4 |
from typing_extensions import ParamSpec |
| 5 |
|
| 6 |
if TYPE_CHECKING: |
| 7 |
from beanie import Document |
| 8 |
from beanie.odm.documents import AsyncDocMethod |
| 9 |
|
| 10 |
P = ParamSpec("P") |
| 11 |
R = TypeVar("R") |
| 12 |
|
| 13 |
|
| 14 |
def validate_self_before( |
| 15 |
f: "AsyncDocMethod[P, R]", |
| 16 |
) -> "AsyncDocMethod[P, R]": |
| 17 |
@wraps(f) |
| 18 |
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: |
| 19 |
self = cast("Document", args[0]) |
| 20 |
await self.validate_self(*args, **kwargs) |
| 21 |
return await f(*args, **kwargs) |
| 22 |
|
| 23 |
return wrapper |