Skip to content

Commit a275a79

Browse files
committed
typex: python 3.9 support.
1 parent c6e197b commit a275a79

5 files changed

Lines changed: 53 additions & 8 deletions

File tree

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Important links
3636
Content
3737
-------
3838

39-
TypeX supplies a simple decorator to enforce Python types on function parameters when annotations are available.
39+
TypEx supplies a simple decorator to enforce Python types on function parameters when annotations are available.
4040

4141
Where to start
4242
--------------

typex/tests/test_typing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# for details.
77
##########################################################################
88

9-
9+
import os
1010
import torch
1111
import types
1212
import unittest
@@ -18,6 +18,7 @@
1818
from traits.api import TraitError
1919
from typex.decorator import typecheck
2020
from typex.validation import check_type
21+
from typex.typing_extensions import File, Directory
2122

2223

2324
class TestExperiment(unittest.TestCase):
@@ -51,12 +52,16 @@ def setUp(self):
5152
Union[int, float],
5253
Sequence[int],
5354
Optional[int],
55+
File,
56+
Directory,
5457
]
5558
self.mvalues = [
5659
(3, None),
5760
(2.3, None),
5861
([1, 2, 3], None),
5962
(None, 3.),
63+
(__file__, None),
64+
(os.path.dirname(__file__), None),
6065
]
6166

6267
def tearDown(self):

typex/traits_extension.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# for details.
77
##########################################################################
88

9+
import os
10+
911
import numpy as np
1012
import torch
1113
import traits.api as traits
@@ -46,6 +48,20 @@ def validate(self, objekt, name, value):
4648
return value
4749

4850

51+
class File(traits.TraitType):
52+
def validate(self, objekt, name, value):
53+
if not (isinstance(value, str) and os.path.isfile(value)):
54+
self.error(objekt, name, value)
55+
return value
56+
57+
58+
class Directory(traits.TraitType):
59+
def validate(self, objekt, name, value):
60+
if not (isinstance(value, str) and os.path.isdir(value)):
61+
self.error(objekt, name, value)
62+
return value
63+
64+
4965
class Sequence(traits.List):
5066
def validate(self, objekt, name, value):
5167
if not isinstance(value, (tuple, list)):
@@ -81,12 +97,15 @@ def validate(self, objekt, name, value):
8197
"int": "traits.Int",
8298
"float": "traits.Float",
8399
"bool": "traits.Bool",
84-
"Tensor": "traits.Tensor",
100+
"torch.Tensor": "traits.Tensor",
85101
"list": "traits.List",
86102
"tuple": "traits.Tuple",
87-
"Sequence": "traits.Sequence",
88-
"array": "traits.Array",
89-
"Union": "traits.Union",
90-
"Optional": "traits.Either",
103+
"collections.abc.Sequence": "traits.Sequence",
104+
"typing.Sequence": "traits.Sequence",
105+
"numpy.array": "traits.Array",
106+
"typing.Union": "traits.Union",
107+
"typing.Optional": "traits.Either",
91108
"NoneType": "traits.Undefined",
109+
"typex.typing_extensions.File": "traits.File",
110+
"typex.typing_extensions.Directory": "traits.Directory"
92111
}

typex/typing_extensions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##########################################################################
2+
# NSAp - Copyright (C) CEA, 2025
3+
# Distributed under the terms of the CeCILL-B license, as published by
4+
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
5+
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
6+
# for details.
7+
##########################################################################
8+
9+
import os
10+
11+
12+
class File(os.PathLike):
13+
pass
14+
15+
16+
class Directory(os.PathLike):
17+
pass

typex/validation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ def check_type(
5858

5959

6060
def typing_to_traits(annot, default_value=False):
61-
name = annot.__name__
61+
if hasattr(annot, "__name__"):
62+
name = f"{annot.__module__}.{annot.__name__}"
63+
name = name.replace("builtins.", "")
64+
else:
65+
name = repr(annot).split("[")[0]
6266
if name not in TRAIT_TYPES:
6367
raise NotImplementedError(f"The '{name}' type is not handled yet.")
6468
expr = f"{TRAIT_TYPES[name]}("

0 commit comments

Comments
 (0)