Skip to content

Commit

Permalink
Use a ForwardRef object as a result of inspect.signature annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Nov 14, 2023
1 parent b7d6435 commit 1f5ab42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mashumaro/core/meta/code/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ def evaluate_forward_ref(
typ: typing.ForwardRef,
owner: typing.Optional[typing.Type],
) -> typing.Optional[typing.Type]:
if not getattr(typ, "__forward_module__", None) and owner:
forward_module = getattr(typ, "__forward_module__", None)
if not forward_module and owner:
# We can't get the module in which ForwardRef's value is defined on
# Python < 3.10, ForwardRef evaluation might not work properly
# without this information, so we will consider the namespace of
Expand All @@ -304,7 +305,7 @@ def evaluate_forward_ref(
self.globals,
)
else:
globalns = self.globals
globalns = getattr(forward_module, "__dict__", self.globals)
return evaluate_forward_ref(typ, globalns, self.__dict__)

def get_declared_hook(self, method_name: str) -> typing.Any:
Expand Down
4 changes: 4 additions & 0 deletions mashumaro/core/meta/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ def get_function_arg_annotation(
annotation = parameter.annotation
if annotation is inspect.Signature.empty:
raise ValueError(f"Argument {arg_name} doesn't have annotation")
if isinstance(annotation, str):
annotation = ForwardRef(annotation, module=inspect.getmodule(function))
return annotation


Expand All @@ -689,6 +691,8 @@ def get_function_return_annotation(
annotation = inspect.signature(function).return_annotation
if annotation is inspect.Signature.empty:
raise ValueError("Function doesn't have return annotation")
if isinstance(annotation, str):
annotation = ForwardRef(annotation, module=inspect.getmodule(function))
return annotation


Expand Down

0 comments on commit 1f5ab42

Please sign in to comment.