From 0a154847cec8e9a36040fbaf5b6902f6a945dfcd Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 25 Oct 2023 02:30:21 -0400 Subject: [PATCH 1/6] do not add type of default if it is already in type --- dargs/dargs.py | 15 ++++++++++++++- dargs/sphinx.py | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dargs/dargs.py b/dargs/dargs.py index dc70b0d..1506fd4 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -224,7 +224,7 @@ def _reorg_dtype(self): # check conner cases if self.sub_fields or self.sub_variants: self.dtype.add(list if self.repeat else dict) - if self.optional and self.default is not _Flags.NONE: + if self.optional and self.default is not _Flags.NONE and all([not isinstance_annotation(self.default, tt) for tt in self.dtype]): self.dtype.add(type(self.default)) # and make it compatible with `isinstance` self.dtype = tuple(self.dtype) @@ -968,6 +968,19 @@ def trim_by_pattern( argdict.pop(key) +def isinstance_annotation(value, dtype) -> bool: + """Same as isinstance(), but supports arbitrary type annotations.""" + try: + typeguard.check_type( + value, + dtype, + collection_check_strategy=typeguard.CollectionCheckStrategy.ALL_ITEMS, + ) + except typeguard.TypeCheckError as e: + return False + return True + + class ArgumentEncoder(json.JSONEncoder): """Extended JSON Encoder to encode Argument object: diff --git a/dargs/sphinx.py b/dargs/sphinx.py index 80a80e3..33f3371 100644 --- a/dargs/sphinx.py +++ b/dargs/sphinx.py @@ -192,5 +192,5 @@ def _test_arguments() -> List[Argument]: return [ Argument(name="test1", dtype=int, doc="Argument 1"), Argument(name="test2", dtype=[float, None], doc="Argument 2"), - Argument(name="test3", dtype=List[str], doc="Argument 3"), + Argument(name="test3", dtype=List[str], default=["test"], doc="Argument 3"), ] From 3b659817a814d7302d29479bf145abcf8357743c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 06:30:34 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dargs/dargs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dargs/dargs.py b/dargs/dargs.py index 1506fd4..6a7c87c 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -224,7 +224,11 @@ def _reorg_dtype(self): # check conner cases if self.sub_fields or self.sub_variants: self.dtype.add(list if self.repeat else dict) - if self.optional and self.default is not _Flags.NONE and all([not isinstance_annotation(self.default, tt) for tt in self.dtype]): + if ( + self.optional + and self.default is not _Flags.NONE + and all([not isinstance_annotation(self.default, tt) for tt in self.dtype]) + ): self.dtype.add(type(self.default)) # and make it compatible with `isinstance` self.dtype = tuple(self.dtype) From cafee97e0dfc73ee496be4c14cb53bbcf404bb16 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 25 Oct 2023 02:31:57 -0400 Subject: [PATCH 3/6] add tests --- tests/test_checker.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_checker.py b/tests/test_checker.py index 088a8b0..7585a98 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -31,6 +31,9 @@ def test_name_type(self): # list[int] ca = Argument("key1", List[float]) ca.check({"key1": [1, 2.0, 3]}) + with self.assertRaises(ArgumentTypeError): + ca.check({"key1": [1, 2.0, "3"]}) + ca = Argument("key1", List[float], default=[]) with self.assertRaises(ArgumentTypeError): ca.check({"key1": [1, 2.0, "3"]}) # optional case From 64662f72469e5946a2255bb5f6957c60a0689d88 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 25 Oct 2023 03:10:29 -0400 Subject: [PATCH 4/6] set optional to true --- dargs/sphinx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dargs/sphinx.py b/dargs/sphinx.py index 33f3371..57c4781 100644 --- a/dargs/sphinx.py +++ b/dargs/sphinx.py @@ -192,5 +192,5 @@ def _test_arguments() -> List[Argument]: return [ Argument(name="test1", dtype=int, doc="Argument 1"), Argument(name="test2", dtype=[float, None], doc="Argument 2"), - Argument(name="test3", dtype=List[str], default=["test"], doc="Argument 3"), + Argument(name="test3", dtype=List[str], default=["test"], optional=True, doc="Argument 3"), ] From 947692d0ad8f527f774386be6666d7e249afd350 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 07:10:39 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dargs/sphinx.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dargs/sphinx.py b/dargs/sphinx.py index 57c4781..5cc9e7f 100644 --- a/dargs/sphinx.py +++ b/dargs/sphinx.py @@ -192,5 +192,11 @@ def _test_arguments() -> List[Argument]: return [ Argument(name="test1", dtype=int, doc="Argument 1"), Argument(name="test2", dtype=[float, None], doc="Argument 2"), - Argument(name="test3", dtype=List[str], default=["test"], optional=True, doc="Argument 3"), + Argument( + name="test3", + dtype=List[str], + default=["test"], + optional=True, + doc="Argument 3", + ), ] From 625c5da401d5546ca70c414dbaa17c7f7b8a58db Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 25 Oct 2023 03:12:54 -0400 Subject: [PATCH 6/6] set optional to true --- tests/test_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_checker.py b/tests/test_checker.py index 7585a98..62be7ec 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -33,7 +33,7 @@ def test_name_type(self): ca.check({"key1": [1, 2.0, 3]}) with self.assertRaises(ArgumentTypeError): ca.check({"key1": [1, 2.0, "3"]}) - ca = Argument("key1", List[float], default=[]) + ca = Argument("key1", List[float], default=[], optional=True) with self.assertRaises(ArgumentTypeError): ca.check({"key1": [1, 2.0, "3"]}) # optional case