Skip to content

Commit

Permalink
♻️ Fixing typing for versions older than 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
perdy committed Sep 19, 2023
1 parent 73d6c65 commit 1110d01
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 52 deletions.
4 changes: 2 additions & 2 deletions flama/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import ParamSpec

t.ParamSpec = ParamSpec
t.ParamSpec = ParamSpec # type: ignore

__all__ = ["BackgroundTask", "BackgroundTasks", "Concurrency", "BackgroundThreadTask", "BackgroundProcessTask"]

P = t.ParamSpec("P")
P = t.ParamSpec("P") # type: ignore # PORT: Remove this comment when stop supporting 3.9


class task_wrapper:
Expand Down
22 changes: 18 additions & 4 deletions flama/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@
import sys
import typing as t

if sys.version_info < (3, 9): # PORT: Remove when stop supporting 3.8 # pragma: no cover
import contextvars

async def to_thread(func, /, *args, **kwargs):
return await asyncio.get_running_loop().run_in_executor(
None, functools.partial(contextvars.copy_context().run, func, *args, **kwargs)
)

asyncio.to_thread = to_thread # pyright: ignore

if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import ParamSpec, TypeGuard

t.TypeGuard = TypeGuard
t.ParamSpec = ParamSpec
t.TypeGuard = TypeGuard # type: ignore
t.ParamSpec = ParamSpec # type: ignore

__all__ = ["is_async", "run", "run"]

R = t.TypeVar("R", covariant=True)
P = t.ParamSpec("P")
P = t.ParamSpec("P") # type: ignore # PORT: Remove this comment when stop supporting 3.9


def is_async(obj: t.Any) -> t.TypeGuard[t.Callable[..., t.Awaitable[t.Any]]]:
def is_async(
obj: t.Any,
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Callable[..., t.Awaitable[t.Any]]
]:
"""Check if given object is an async function, callable or partialised function.
:param obj: Object to check.
Expand Down
10 changes: 7 additions & 3 deletions flama/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore

if t.TYPE_CHECKING:
from flama.applications import Flama
Expand Down Expand Up @@ -336,7 +336,9 @@ def __repr__(self) -> str:
@staticmethod
def is_endpoint(
x: t.Union[t.Callable, t.Type[endpoints.HTTPEndpoint]]
) -> t.TypeGuard[t.Type[endpoints.HTTPEndpoint]]:
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Type[endpoints.HTTPEndpoint]
]:
return inspect.isclass(x) and issubclass(x, endpoints.HTTPEndpoint)

def endpoint_handlers(self) -> t.Dict[str, t.Callable]:
Expand Down Expand Up @@ -412,7 +414,9 @@ def __eq__(self, other: t.Any) -> bool:
@staticmethod
def is_endpoint(
x: t.Union[t.Callable, t.Type[endpoints.WebSocketEndpoint]]
) -> t.TypeGuard[t.Type[endpoints.WebSocketEndpoint]]:
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Type[endpoints.WebSocketEndpoint]
]:
return inspect.isclass(x) and issubclass(x, endpoints.WebSocketEndpoint)

def endpoint_handlers(self) -> t.Dict[str, t.Callable]:
Expand Down
14 changes: 11 additions & 3 deletions flama/schemas/_libs/marshmallow/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore

if t.TYPE_CHECKING:
from apispec.ext.marshmallow import OpenAPIConverter
Expand Down Expand Up @@ -115,10 +115,18 @@ def unique_schema(self, schema: t.Union[Schema, t.Type[Schema]]) -> t.Type[Schem

return schema

def is_schema(self, obj: t.Any) -> t.TypeGuard[t.Union[Schema, t.Type[Schema]]]:
def is_schema(
self, obj: t.Any
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Union[Schema, t.Type[Schema]]
]:
return isinstance(obj, Schema) or (inspect.isclass(obj) and issubclass(obj, Schema))

def is_field(self, obj: t.Any) -> t.TypeGuard[t.Union[Field, t.Type[Field]]]:
def is_field(
self, obj: t.Any
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Union[Field, t.Type[Field]]
]:
return isinstance(obj, Field) or (inspect.isclass(obj) and issubclass(obj, Field))

def _schema_instance(self, schema: t.Union[t.Type[Schema], Schema]) -> Schema:
Expand Down
10 changes: 7 additions & 3 deletions flama/schemas/_libs/pydantic/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore

__all__ = ["PydanticAdapter"]

Expand Down Expand Up @@ -116,8 +116,12 @@ def to_json_schema(self, schema: t.Union[Schema, t.Type[Schema], Field]) -> JSON
def unique_schema(self, schema: t.Union[Schema, t.Type[Schema]]) -> t.Type[Schema]:
return schema.__class__ if isinstance(schema, Schema) else schema

def is_schema(self, obj: t.Any) -> t.TypeGuard[t.Type[Schema]]:
def is_schema(
self, obj: t.Any
) -> t.TypeGuard[t.Type[Schema]]: # type: ignore # PORT: Remove this comment when stop supporting 3.9
return inspect.isclass(obj) and issubclass(obj, Schema)

def is_field(self, obj: t.Any) -> t.TypeGuard[Field]:
def is_field(
self, obj: t.Any
) -> t.TypeGuard[Field]: # type: ignore # PORT: Remove this comment when stop supporting 3.9
return isinstance(obj, Field)
10 changes: 7 additions & 3 deletions flama/schemas/_libs/typesystem/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore

__all__ = ["TypesystemAdapter"]

Expand Down Expand Up @@ -101,9 +101,13 @@ def unique_schema(self, schema: Schema) -> Schema:
return schema

@t.no_type_check
def is_schema(self, obj: t.Any) -> t.TypeGuard[Schema]:
def is_schema(
self, obj: t.Any
) -> t.TypeGuard[Schema]: # type: ignore # PORT: Remove this comment when stop supporting 3.9
return isinstance(obj, Schema) or (inspect.isclass(obj) and issubclass(obj, Schema))

@t.no_type_check
def is_field(self, obj: t.Any) -> t.TypeGuard[Field]:
def is_field(
self, obj: t.Any
) -> t.TypeGuard[Field]: # type: ignore # PORT: Remove this comment when stop supporting 3.9
return isinstance(obj, Field) or (inspect.isclass(obj) and issubclass(obj, Field))
14 changes: 11 additions & 3 deletions flama/schemas/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore


class Adapter(t.Generic[_T_Schema, _T_Field], metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -83,9 +83,17 @@ def unique_schema(self, schema: t.Union[_T_Schema, t.Type[_T_Schema]]) -> t.Unio
...

@abc.abstractmethod
def is_schema(self, obj: t.Any) -> t.TypeGuard[t.Union[_T_Schema, t.Type[_T_Schema]]]:
def is_schema(
self, obj: t.Any
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Union[_T_Schema, t.Type[_T_Schema]]
]:
...

@abc.abstractmethod
def is_field(self, obj: t.Any) -> t.TypeGuard[t.Union[_T_Field, t.Type[_T_Field]]]:
def is_field(
self, obj: t.Any
) -> t.TypeGuard[ # type: ignore # PORT: Remove this comment when stop supporting 3.9
t.Union[_T_Field, t.Type[_T_Field]]
]:
...
2 changes: 1 addition & 1 deletion flama/schemas/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore

__all__ = ["Field", "Schema", "Parameter", "Parameters"]

Expand Down
14 changes: 9 additions & 5 deletions flama/types/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import Concatenate, ParamSpec

t.Concatenate = Concatenate
t.ParamSpec = ParamSpec
t.Concatenate = Concatenate # type: ignore
t.ParamSpec = ParamSpec # type: ignore

if t.TYPE_CHECKING:
from flama import endpoints # noqa
Expand All @@ -29,7 +29,7 @@
"WebSocketHandler",
]

P = t.ParamSpec("P")
P = t.ParamSpec("P") # type: ignore # PORT: Remove this comment when stop supporting 3.9
R = t.TypeVar("R", covariant=True)

Scope = t.NewType("Scope", t.MutableMapping[str, t.Any])
Expand Down Expand Up @@ -73,8 +73,12 @@ def __init__(self, app: App, *args: P.args, **kwargs: P.kwargs):
...


MiddlewareFunction = t.Callable[t.Concatenate[App, P], App]
MiddlewareAsyncFunction = t.Callable[t.Concatenate[App, P], t.Awaitable[App]]
MiddlewareFunction = t.Callable[
t.Concatenate[App, P], App # type: ignore # PORT: Remove this comment when stop supporting 3.9
]
MiddlewareAsyncFunction = t.Callable[
t.Concatenate[App, P], t.Awaitable[App] # type: ignore # PORT: Remove this comment when stop supporting 3.9
]
Middleware = t.Union[t.Type[MiddlewareClass], t.Type[MiddlewareAsyncClass], MiddlewareFunction, MiddlewareAsyncFunction]

HTTPHandler = t.Union[AppFunction, t.Type["endpoints.HTTPEndpoint"]]
Expand Down
6 changes: 4 additions & 2 deletions flama/types/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

t.TypeGuard = TypeGuard
t.TypeGuard = TypeGuard # type: ignore

__all__ = ["Schema", "is_schema"]

_T_Field = t.TypeVar("_T_Field")
_T_Schema = t.TypeVar("_T_Schema")


def is_schema(obj: t.Any) -> t.TypeGuard[t.Type["Schema"]]:
def is_schema(
obj: t.Any,
) -> t.TypeGuard[t.Type["Schema"]]: # type: ignore # PORT: Remove this comment when stop supporting 3.9
return inspect.isclass(obj) and issubclass(obj, Schema)


Expand Down
Loading

0 comments on commit 1110d01

Please sign in to comment.