From 9d00d65aa7c65feda01f7130b51f380f71122930 Mon Sep 17 00:00:00 2001 From: altescy Date: Tue, 3 Dec 2024 23:41:39 +0900 Subject: [PATCH 1/2] add test for annotated --- tests/test_colt.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_colt.py b/tests/test_colt.py index bb12b20..4e01658 100644 --- a/tests/test_colt.py +++ b/tests/test_colt.py @@ -1,4 +1,6 @@ import dataclasses +import sys +import pytest from enum import Enum from typing import ( Any, @@ -511,3 +513,22 @@ def __init__(self, model: BaseModel[T, U], params: U, data: T) -> None: assert isinstance(executor.model, MyModel) assert isinstance(executor.params, MyModel.Params) assert isinstance(executor.data, Item) + + +@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher") +def test_build_with_annotated() -> None: + from typing import Annotated + + class Foo: + def __init__(self, x: str) -> None: + self.x = x + + class Bar: + def __init__(self, foo: Annotated[Foo, "test"]) -> None: + self.foo = foo + + config = {"foo": {"x": "hello"}} + obj = colt.build(config, Bar) + + assert isinstance(obj.foo, Foo) + assert obj.foo.x == "hello" From a5f8d18a8a55a7bdd813a98fd57897369ba9aaf2 Mon Sep 17 00:00:00 2001 From: altescy Date: Tue, 3 Dec 2024 23:54:56 +0900 Subject: [PATCH 2/2] use typing extentions --- tests/test_colt.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_colt.py b/tests/test_colt.py index 4e01658..112e6e2 100644 --- a/tests/test_colt.py +++ b/tests/test_colt.py @@ -1,6 +1,5 @@ import dataclasses import sys -import pytest from enum import Enum from typing import ( Any, @@ -26,6 +25,11 @@ import colt +if sys.version_info >= (3, 9): + from typing import Annotated +else: + from typing_extensions import Annotated + @colt.register("foo") class Foo: @@ -515,10 +519,7 @@ def __init__(self, model: BaseModel[T, U], params: U, data: T) -> None: assert isinstance(executor.data, Item) -@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher") def test_build_with_annotated() -> None: - from typing import Annotated - class Foo: def __init__(self, x: str) -> None: self.x = x