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

Better type annotations for contextmanager #219

Open
andreymal opened this issue Jan 28, 2023 · 0 comments
Open

Better type annotations for contextmanager #219

andreymal opened this issue Jan 28, 2023 · 0 comments
Milestone

Comments

@andreymal
Copy link

I noticed that contextmanager doesn't preserve signatures of decorated methods:

reveal_type(db.executemany)
# mypy: Revealed type is "def (*Any, **Any) -> aiosqlite.context.Result[aiosqlite.cursor.Cursor]"

Consider using ParamSpec to preserve them:

from typing_extensions import ParamSpec

_P = ParamSpec("_P")

def contextmanager(
    method: Callable[_P, Coroutine[Any, Any, _T]]
) -> Callable[_P, Result[_T]]:
    @wraps(method)
    def wrapper(self, *args: _P.args, **kwargs: _P.kwargs) -> Result[_T]:
        return Result(method(self, *args, **kwargs))

    return wrapper
reveal_type(db.executemany)
# mypy: Revealed type is "def (sql: builtins.str, parameters: typing.Iterable[typing.Iterable[Any]])
#  -> aiosqlite.context.Result[aiosqlite.cursor.Cursor]"
@amyreese amyreese added this to the 0.20 milestone Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants