Skip to content

Commit

Permalink
✨ Use types.get_type_hints to get annotations for pep563 types.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalkrupinski committed Jan 3, 2025
1 parent 2a44d99 commit 7528cba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/lapidary/runtime/model/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

def process_operation_method(fn: Callable, op: 'Operation') -> tuple[RequestAdapter, ResponseMessageExtractor]:
sig = inspect.signature(fn)
type_hints = typing.get_type_hints(fn, include_extras=True)
params = {name: param.replace(annotation=type_hints[name]) for name, param in sig.parameters.items()}
try:
response_extractor, media_types = mk_response_extractor(sig.return_annotation)
request_adapter = prepare_request_adapter(fn.__name__, sig, op, media_types)
response_extractor, media_types = mk_response_extractor(type_hints['return'])
request_adapter = prepare_request_adapter(fn.__name__, params, op, media_types)
return request_adapter, response_extractor
except TypeError as error:
raise TypeError(fn.__name__) from error
Expand Down
10 changes: 5 additions & 5 deletions src/lapidary/runtime/model/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..annotations import Body, Cookie, Header, Metadata, Param, Path, Query, WebArg
from ..http_consts import ACCEPT, CONTENT_TYPE, MIME_JSON
from ..metattype import is_array_like, make_not_optional
from ..types_ import Dumper, MimeType, RequestFactory, SecurityRequirements
from ..types_ import Dumper, MimeType, RequestFactory, SecurityRequirements, Signature
from .annotations import (
find_annotation,
find_field_annotation,
Expand Down Expand Up @@ -236,20 +236,20 @@ def update_builder(self, builder: RequestBuilder, kwargs: dict[str, typing.Any])
try:
contributor = self.contributors[name]
except KeyError:
raise TypeError('Unexpected argument', name)
raise TypeError('Unexpected argument', name) from None
contributor.update_builder(builder, value)
if self.free_param_contributor:
self.free_param_contributor.update_builder(builder, free_params)

@classmethod
def for_signature(cls, sig: inspect.Signature) -> typing.Self:
def for_signature(cls, sig: Signature) -> typing.Self:
contributors: dict[str, RequestContributor] = {}
body_param: typing.Optional[str] = None
body_contributor: typing.Optional[BodyContributor] = None

free_params: dict[str, typing.Any] = {} # python name => annotation

for param in sig.parameters.values():
for param in sig.values():
if param.annotation is typing.Self:
continue

Expand Down Expand Up @@ -322,7 +322,7 @@ def build_request(
return builder(), auth


def prepare_request_adapter(name: str, sig: inspect.Signature, operation: 'Operation', accept: Iterable[str]) -> RequestAdapter:
def prepare_request_adapter(name: str, sig: Signature, operation: 'Operation', accept: Iterable[str]) -> RequestAdapter:
return RequestAdapter(
name,
operation.method,
Expand Down
3 changes: 2 additions & 1 deletion src/lapidary/runtime/types_.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import abc
from collections.abc import Callable, MutableMapping
from collections.abc import Callable, Mapping, MutableMapping

import httpx
import httpx._transports.base
Expand All @@ -16,6 +16,7 @@

MimeType: typing.TypeAlias = str
MimeMap: typing.TypeAlias = MutableMapping[MimeType, type]
Signature: typing.TypeAlias = Mapping[str, typing.Any]
StatusCodeRange: typing.TypeAlias = str
StatusCodeType: typing.TypeAlias = int
SessionFactory: typing.TypeAlias = Callable[..., httpx.AsyncClient]
Expand Down

0 comments on commit 7528cba

Please sign in to comment.