Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions csp/impl/types/pydantic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def adjust_annotations(
annotation.typ, top_level=False, in_ts=False, make_optional=False, forced_tvars=forced_tvars
)
)
elif isinstance(annotation, list): # Handle list, i.e. in Callable[[Annotation], Any]
return [
adjust_annotations(a, top_level=False, in_ts=in_ts, make_optional=False, forced_tvars=forced_tvars)
for a in annotation
]

if type(annotation) is ForwardRef:
if in_ts:
Expand Down
29 changes: 27 additions & 2 deletions csp/tests/impl/types/test_pydantic_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from typing import Dict, Generic, List, Optional, Type, TypeVar, Union, get_args, get_origin
from inspect import isclass
from typing import Any, Callable, Dict, Generic, List, Optional, Type, TypeVar, Union, get_args, get_origin
from unittest import TestCase

import csp
Expand All @@ -24,7 +25,16 @@ def assertAnnotationsEqual(self, annotation1, annotation2):
if origin1 is None:
if isinstance(annotation1, TypeVar) and isinstance(annotation2, TypeVar):
self.assertEqual(annotation1.__name__, annotation2.__name__)
elif issubclass(annotation1, OutputBasket) and issubclass(annotation2, OutputBasket):
elif isinstance(annotation1, list) and isinstance(annotation2, list):
self.assertEqual(len(annotation1), len(annotation2))
for item1, item2 in zip(annotation1, annotation2):
self.assertAnnotationsEqual(item1, item2)
elif (
isclass(annotation1)
and issubclass(annotation1, OutputBasket)
and isclass(annotation2)
and issubclass(annotation2, OutputBasket)
):
self.assertAnnotationsEqual(annotation1.typ, annotation2.typ)
else:
self.assertEqual(annotation1, annotation2)
Expand Down Expand Up @@ -56,6 +66,12 @@ def test_tvar_container(self):
self.assertAnnotationsEqual(adjust_annotations(MyGeneric["T"]), MyGeneric[CspTypeVar[T]])
self.assertAnnotationsEqual(adjust_annotations(MyGeneric[T]), MyGeneric[CspTypeVar[T]])

def test_tvar_callable(self):
self.assertAnnotationsEqual(adjust_annotations(Callable[["T"], Any]), Callable[[CspTypeVar[T]], Any])
self.assertAnnotationsEqual(
adjust_annotations(Callable[["K", "K"], "T"]), Callable[[CspTypeVar[K], CspTypeVar[K]], CspTypeVar[T]]
)

def test_tvar_ts_of_container(self):
self.assertAnnotationsEqual(adjust_annotations(ts["T"]), ts[CspTypeVarType[T]])
self.assertAnnotationsEqual(adjust_annotations(ts["~T"]), ts[CspTypeVarType[TypeVar("~T")]])
Expand All @@ -77,6 +93,15 @@ def test_tvar_ts_of_container(self):
adjust_annotations(ts[Union[K, T]]), ts[Union[CspTypeVarType[K], CspTypeVarType[T]]]
)

def test_tvar_ts_of_callable(self):
self.assertAnnotationsEqual(
adjust_annotations(ts[Callable[["T"], Any]]), ts[Callable[[CspTypeVarType[T]], Any]]
)
self.assertAnnotationsEqual(
adjust_annotations(ts[Callable[["K", "K"], "T"]]),
ts[Callable[[CspTypeVarType[K], CspTypeVarType[K]], CspTypeVarType[T]]],
)

def test_tvar_container_of_ts(self):
self.assertAnnotationsEqual(adjust_annotations(List[ts["T"]]), List[ts[CspTypeVarType[T]]])
self.assertAnnotationsEqual(adjust_annotations(List[ts[T]]), List[ts[CspTypeVarType[T]]])
Expand Down
Loading