Skip to content

Preserving typing on async_to_sync/sync_to_async wrapped functions #270

Open
@shughes-uk

Description

@shughes-uk

Currently wrapping functions with these class based decorators will obliterate type information. With the introduction of Paramspec in python 3.10 and the backports library typing_extensions it should be possible (using Generic) to preserve this information.

The code for this looks like this

from typing_extensions import ParamSpec
from typing import Callable, Generic, TypeVar

return_value  = TypeVar("return_value")
parameters = ParamSpec("parameters")

class FooDecorator(Generic[parameters, return_value]):
    def __init__(self,func: Callable[parameters, return_value]):
        self.func = func

    def __call__(self,*args:parameters.args, **kwargs:parameters.kwargs)-> return_value:
        return self.func(*args,**kwargs)

This would currently be blocked by a bug in the typing backport (python/typing#817), but is soon to resolved. I would be happy to create a PR for this once the bug is resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions