Skip to content

Commit

Permalink
Improve testing for TypedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
aaltat committed Nov 16, 2023
1 parent 18f24de commit 2c91687
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
22 changes: 22 additions & 0 deletions atest/lib_future_annotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

from typing_extensions import TypedDict

from robotlibcore import DynamicCore, keyword


class Location(TypedDict):
longitude: float
latitude: float


class lib_future_annotation(DynamicCore):

def __init__(self) -> None:
DynamicCore.__init__(self, [])

@keyword
def future_annotations(self, arg: Location):
longitude = arg["longitude"]
latitude = arg["latitude"]
return f'{longitude} type({type(longitude)}), {latitude} {type(latitude)}'
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ wheel
rellu >= 0.7
twine
wheel
typing-extensions >= 4.5.0
18 changes: 13 additions & 5 deletions src/robotlibcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import inspect
import os
from dataclasses import dataclass
from typing import Any, Callable, List, Optional, Union, get_type_hints
from typing import Any, Callable, List, Optional, Union, get_type_hints, ForwardRef

from robot.api.deco import keyword # noqa: F401
from robot.errors import DataError
Expand Down Expand Up @@ -223,6 +223,17 @@ def _get_arguments(cls, function):
def _get_arg_spec(cls, function: Callable) -> inspect.FullArgSpec:
return inspect.getfullargspec(function)

@classmethod
def _get_type_hint(cls, function: Callable):
try:
hints = get_type_hints(function)
except Exception: # noqa: BLE001
hints = function.__annotations__
for arg, hint in hints.items():
if isinstance(hint, ForwardRef):
hint = hint.__forward_arg__
return hints

@classmethod
def _get_args(cls, arg_spec: inspect.FullArgSpec, function: Callable) -> list:
args = cls._drop_self_from_args(function, arg_spec)
Expand Down Expand Up @@ -279,10 +290,7 @@ def _get_types(cls, function):
@classmethod
def _get_typing_hints(cls, function):
function = cls.unwrap(function)
try:
hints = get_type_hints(function)
except Exception: # noqa: BLE001
hints = function.__annotations__
hints = cls._get_type_hint(function)
arg_spec = cls._get_arg_spec(function)
all_args = cls._args_as_list(function, arg_spec)
for arg_with_hint in list(hints):
Expand Down
12 changes: 12 additions & 0 deletions utest/test_get_keyword_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from DynamicTypesAnnotationsLibrary import CustomObject, DynamicTypesAnnotationsLibrary
from DynamicTypesLibrary import DynamicTypesLibrary
from lib_future_annotation import lib_future_annotation, Location


@pytest.fixture(scope="module")
Expand All @@ -16,6 +17,11 @@ def lib_types():
return DynamicTypesAnnotationsLibrary("aaa")


@pytest.fixture(scope="module")
def lib_annotation():
return lib_future_annotation()


def test_using_keyword_types(lib):
types = lib.get_keyword_types("keyword_with_types")
assert types == {"arg1": str}
Expand Down Expand Up @@ -204,3 +210,9 @@ def test_kw_with_many_named_arguments_with_default(lib_types: DynamicTypesAnnota
assert types == {"arg1": int, "arg2": str}
types = lib_types.get_keyword_types("kw_with_positional_and_named_arguments")
assert types == {"arg2": int}


def test_lib_annotations(lib_annotation: lib_future_annotation):
types = lib_annotation.get_keyword_types("future_annotations")
expected = {"arg1": Location}
assert types == expected

0 comments on commit 2c91687

Please sign in to comment.