Skip to content

Commit 70f43cb

Browse files
authored
Add supported for Annotated to struct type. (#375)
Signed-off-by: Pascal Tomecek <[email protected]>
1 parent 60dbfd8 commit 70f43cb

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

conda/dev-environment-unix.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies:
5252
- threadpoolctl
5353
- tornado
5454
- twine
55+
- typing-extensions
5556
- unzip
5657
- wheel
5758
- zip

conda/dev-environment-win.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ dependencies:
5050
- threadpoolctl
5151
- tornado
5252
- twine
53+
- typing-extensions
5354
- wheel

csp/impl/types/container_type_normalizer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy
22
import typing
3+
import typing_extensions
34

45
import csp.typing
56
from csp.impl.types.typing_utils import CspTypingUtils, FastList
@@ -70,6 +71,8 @@ def _convert_containers_to_typing_generic_meta(cls, typ, is_within_container):
7071

7172
@classmethod
7273
def normalized_type_to_actual_python_type(cls, typ, level=0):
74+
if isinstance(typ, typing_extensions._AnnotatedAlias):
75+
typ = CspTypingUtils.get_origin(typ)
7376
if CspTypingUtils.is_generic_container(typ):
7477
if CspTypingUtils.get_origin(typ) is FastList and level == 0:
7578
return [cls.normalized_type_to_actual_python_type(typ.__args__[0], level + 1), True]

csp/tests/impl/test_struct.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
from datetime import date, datetime, time, timedelta
99
from typing import Dict, List, Set, Tuple
10+
from typing_extensions import Annotated
1011

1112
import csp
1213
from csp.impl.struct import define_nested_struct, define_struct, defineNestedStruct, defineStruct
@@ -2943,6 +2944,22 @@ def test_dir(self):
29432944
self.assertIn("__metadata__", dir_output)
29442945
self.assertEqual(dir_output, sorted(dir_output))
29452946

2947+
def test_annotations(self):
2948+
class StructWithAnnotations(csp.Struct):
2949+
b: Annotated[float, "test"]
2950+
d: Annotated[Dict[str, Annotated[int, "test_int"]], "test_dict"]
2951+
s: str
2952+
2953+
self.assertEqual(
2954+
StructWithAnnotations.metadata(typed=True),
2955+
{
2956+
"b": Annotated[float, "test"],
2957+
"d": Annotated[Dict[str, Annotated[int, "test_int"]], "test_dict"],
2958+
"s": str,
2959+
},
2960+
)
2961+
self.assertEqual(StructWithAnnotations.metadata(typed=False), {"b": float, "d": dict, "s": str})
2962+
29462963

29472964
if __name__ == "__main__":
29482965
unittest.main()

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires = [
77
"ruamel.yaml",
88
"scikit-build",
99
"setuptools>=69,<74",
10+
"typing-extensions",
1011
]
1112
build-backend="setuptools.build_meta"
1213

@@ -29,6 +30,7 @@ dependencies = [
2930
"pytz",
3031
"ruamel.yaml",
3132
"sqlalchemy",
33+
"typing-extensions",
3234
]
3335

3436
classifiers = [

0 commit comments

Comments
 (0)