Skip to content

Proper typing for run_sync method is now possible #197

@michaeloliverx

Description

@michaeloliverx

Is your feature request related to a problem? Please describe.

I authored sqlalchemy/sqlalchemy#5777 which added a typevar to run_sync to infer the return value. At that time is was not possible to infer *args or **kwargs of the callable passed and nor was it possible to infer that the first argument of type Session passed automatically for you. Since Python 3.10 has been released we have additional typing constructs typing.ParamSpec and typing.Concatenate, both of which are available on older python versions using the typing_extensions backport.

Describe the solution you'd like

Properly type all of the run_sync methods, something along the lines of the following should do the trick (I haven't set up an environment to test):

if sys.version_info >= (3, 10):
    from typing import Concatenate
    from typing import ParamSpec
else:
    from typing_extensions import Concatenate
    from typing_extensions import ParamSpec

_T = TypeVar("_T")
_P = ParamSpec("_P")

class AsyncSession(
    _AsyncSessionTypingCommon,
    _SessionInTransactionTypingCommon,
):
    ...
    async def run_sync(
        self,
        fn: Callable[Concatenate[Session, _P], _T],
        *arg: _P.args,
        **kw: _P.kwargs,
    ) -> _T: ...

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions