str-to-obj 2023.10__py3-none-any.whl → 2025.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- str_to_obj/__init__.py +64 -32
- str_to_obj/api/catalog.py +58 -0
- str_to_obj/api/type.py +58 -0
- str_to_obj/catalog/boolean.py +99 -0
- str_to_obj/catalog/callable.py +99 -0
- str_to_obj/catalog/choices.py +101 -0
- str_to_obj/catalog/collection.py +175 -0
- str_to_obj/catalog/number.py +142 -0
- str_to_obj/catalog/path.py +117 -0
- str_to_obj/interface/console.py +59 -37
- str_to_obj/main.py +76 -36
- str_to_obj/runtime/type.py +58 -0
- str_to_obj/runtime/value.py +56 -0
- str_to_obj/task/casting.py +88 -59
- str_to_obj/task/comparison.py +64 -38
- str_to_obj/task/inspection.py +59 -35
- str_to_obj/type/annotation.py +61 -43
- str_to_obj/type/hint.py +59 -38
- str_to_obj/type/hint_tree.py +122 -63
- str_to_obj/type/type.py +121 -0
- str_to_obj/type/value.py +69 -0
- str_to_obj/version.py +54 -34
- {str_to_obj-2023.10.dist-info → str_to_obj-2025.1.dist-info}/METADATA +90 -48
- str_to_obj-2025.1.dist-info/RECORD +26 -0
- {str_to_obj-2023.10.dist-info → str_to_obj-2025.1.dist-info}/WHEEL +1 -1
- str_to_obj-2023.10.dist-info/RECORD +0 -14
- {str_to_obj-2023.10.dist-info → str_to_obj-2025.1.dist-info}/top_level.txt +0 -0
str_to_obj/type/annotation.py
CHANGED
@@ -1,49 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# This software is governed by the CeCILL license under French law and
|
7
|
-
# abiding by the rules of distribution of free software. You can use,
|
8
|
-
# modify and/ or redistribute the software under the terms of the CeCILL
|
9
|
-
# license as circulated by CEA, CNRS and INRIA at the following URL
|
10
|
-
# "http://www.cecill.info".
|
11
|
-
#
|
12
|
-
# As a counterpart to the access to the source code and rights to copy,
|
13
|
-
# modify and redistribute granted by the license, users are provided only
|
14
|
-
# with a limited warranty and the software's author, the holder of the
|
15
|
-
# economic rights, and the successive licensors have only limited
|
16
|
-
# liability.
|
17
|
-
#
|
18
|
-
# In this respect, the user's attention is drawn to the risks associated
|
19
|
-
# with loading, using, modifying and/or developing or reproducing the
|
20
|
-
# software by the user in light of its specific status of free software,
|
21
|
-
# that may mean that it is complicated to manipulate, and that also
|
22
|
-
# therefore means that it is reserved for developers and experienced
|
23
|
-
# professionals having in-depth computer knowledge. Users are therefore
|
24
|
-
# encouraged to load and test the software's suitability as regards their
|
25
|
-
# requirements in conditions enabling the security of their systems and/or
|
26
|
-
# data to be ensured and, more generally, to use and operate it in the
|
27
|
-
# same conditions as regards security.
|
28
|
-
#
|
29
|
-
# The fact that you are presently reading this means that you have had
|
30
|
-
# knowledge of the CeCILL license and that you accept its terms.
|
31
|
-
|
32
|
-
import dataclasses as dtcl
|
33
|
-
from typing import Any, ClassVar
|
1
|
+
"""
|
2
|
+
Copyright CNRS/Inria/UniCA
|
3
|
+
Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023
|
4
|
+
SEE COPYRIGHT NOTICE BELOW
|
5
|
+
"""
|
34
6
|
|
35
|
-
|
7
|
+
import dataclasses as d
|
8
|
+
import typing as h
|
36
9
|
|
37
|
-
from
|
38
|
-
|
39
|
-
TypeAsRichStr,
|
40
|
-
)
|
10
|
+
from rich.text import Text as text_t
|
11
|
+
from str_to_obj.interface.console import NameValueTypeAsRichStr, TypeAsRichStr
|
41
12
|
from str_to_obj.type.hint import annotated_hint_t, non_complex_hint_h
|
42
13
|
|
43
14
|
|
44
|
-
@
|
15
|
+
@d.dataclass(slots=True, repr=False, eq=False)
|
45
16
|
class annotation_t:
|
46
|
-
ACCEPTED_TYPES: ClassVar[tuple[non_complex_hint_h, ...]] = (Any,)
|
17
|
+
ACCEPTED_TYPES: h.ClassVar[tuple[non_complex_hint_h, ...]] = (h.Any,)
|
47
18
|
|
48
19
|
@classmethod
|
49
20
|
def NewAnnotatedType(cls, *args, **kwargs) -> annotated_hint_t:
|
@@ -57,11 +28,11 @@ class annotation_t:
|
|
57
28
|
"""
|
58
29
|
raise NotImplementedError
|
59
30
|
|
60
|
-
def
|
31
|
+
def ValueIssues(self, value: h.Any, /) -> list[str]:
|
61
32
|
""""""
|
62
33
|
types = self.__class__.ACCEPTED_TYPES
|
63
34
|
stripe = type(value)
|
64
|
-
if (Any in types) or (stripe in types) or issubclass(stripe, types):
|
35
|
+
if (h.Any in types) or (stripe in types) or issubclass(stripe, types):
|
65
36
|
return []
|
66
37
|
|
67
38
|
expected = " or ".join(map(str, types))
|
@@ -77,9 +48,56 @@ class annotation_t:
|
|
77
48
|
""""""
|
78
49
|
output = [TypeAsRichStr(self)]
|
79
50
|
|
80
|
-
names = (_fld.name for _fld in
|
51
|
+
names = (_fld.name for _fld in d.fields(self))
|
81
52
|
for name in names:
|
82
53
|
value = getattr(self, name)
|
83
54
|
output.append(f" {NameValueTypeAsRichStr(name, value, separator=' = ')}")
|
84
55
|
|
85
56
|
return "\n".join(output)
|
57
|
+
|
58
|
+
|
59
|
+
"""
|
60
|
+
COPYRIGHT NOTICE
|
61
|
+
|
62
|
+
This software is governed by the CeCILL license under French law and
|
63
|
+
abiding by the rules of distribution of free software. You can use,
|
64
|
+
modify and/ or redistribute the software under the terms of the CeCILL
|
65
|
+
license as circulated by CEA, CNRS and INRIA at the following URL
|
66
|
+
"http://www.cecill.info".
|
67
|
+
|
68
|
+
As a counterpart to the access to the source code and rights to copy,
|
69
|
+
modify and redistribute granted by the license, users are provided only
|
70
|
+
with a limited warranty and the software's author, the holder of the
|
71
|
+
economic rights, and the successive licensors have only limited
|
72
|
+
liability.
|
73
|
+
|
74
|
+
In this respect, the user's attention is drawn to the risks associated
|
75
|
+
with loading, using, modifying and/or developing or reproducing the
|
76
|
+
software by the user in light of its specific status of free software,
|
77
|
+
that may mean that it is complicated to manipulate, and that also
|
78
|
+
therefore means that it is reserved for developers and experienced
|
79
|
+
professionals having in-depth computer knowledge. Users are therefore
|
80
|
+
encouraged to load and test the software's suitability as regards their
|
81
|
+
requirements in conditions enabling the security of their systems and/or
|
82
|
+
data to be ensured and, more generally, to use and operate it in the
|
83
|
+
same conditions as regards security.
|
84
|
+
|
85
|
+
The fact that you are presently reading this means that you have had
|
86
|
+
knowledge of the CeCILL license and that you accept its terms.
|
87
|
+
|
88
|
+
SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root.
|
89
|
+
|
90
|
+
This software is being developed by Eric Debreuve, a CNRS employee and
|
91
|
+
member of team Morpheme.
|
92
|
+
Team Morpheme is a joint team between Inria, CNRS, and UniCA.
|
93
|
+
It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory
|
94
|
+
I3S, and Laboratory iBV.
|
95
|
+
|
96
|
+
CNRS: https://www.cnrs.fr/index.php/en
|
97
|
+
Inria: https://www.inria.fr/en/
|
98
|
+
UniCA: https://univ-cotedazur.eu/
|
99
|
+
Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/
|
100
|
+
I3S: https://www.i3s.unice.fr/en/
|
101
|
+
iBV: http://ibv.unice.fr/
|
102
|
+
Team Morpheme: https://team.inria.fr/morpheme/
|
103
|
+
"""
|
str_to_obj/type/hint.py
CHANGED
@@ -1,42 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
# license as circulated by CEA, CNRS and INRIA at the following URL
|
10
|
-
# "http://www.cecill.info".
|
11
|
-
#
|
12
|
-
# As a counterpart to the access to the source code and rights to copy,
|
13
|
-
# modify and redistribute granted by the license, users are provided only
|
14
|
-
# with a limited warranty and the software's author, the holder of the
|
15
|
-
# economic rights, and the successive licensors have only limited
|
16
|
-
# liability.
|
17
|
-
#
|
18
|
-
# In this respect, the user's attention is drawn to the risks associated
|
19
|
-
# with loading, using, modifying and/or developing or reproducing the
|
20
|
-
# software by the user in light of its specific status of free software,
|
21
|
-
# that may mean that it is complicated to manipulate, and that also
|
22
|
-
# therefore means that it is reserved for developers and experienced
|
23
|
-
# professionals having in-depth computer knowledge. Users are therefore
|
24
|
-
# encouraged to load and test the software's suitability as regards their
|
25
|
-
# requirements in conditions enabling the security of their systems and/or
|
26
|
-
# data to be ensured and, more generally, to use and operate it in the
|
27
|
-
# same conditions as regards security.
|
28
|
-
#
|
29
|
-
# The fact that you are presently reading this means that you have had
|
30
|
-
# knowledge of the CeCILL license and that you accept its terms.
|
31
|
-
|
32
|
-
from types import EllipsisType, GenericAlias, UnionType
|
33
|
-
from typing import Annotated, Any
|
1
|
+
"""
|
2
|
+
Copyright CNRS/Inria/UniCA
|
3
|
+
Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023
|
4
|
+
SEE COPYRIGHT NOTICE BELOW
|
5
|
+
"""
|
6
|
+
|
7
|
+
import types as t
|
8
|
+
import typing as h
|
34
9
|
|
35
10
|
# --- Unannotated hints
|
36
11
|
# Atoms
|
37
|
-
generic_hint_h = Any
|
38
|
-
simple_hint_h = type[Any]
|
39
|
-
complex_hint_h = GenericAlias | UnionType
|
12
|
+
generic_hint_h = h.Any
|
13
|
+
simple_hint_h = type[h.Any]
|
14
|
+
complex_hint_h = t.GenericAlias | t.UnionType
|
40
15
|
|
41
16
|
# Grouping
|
42
17
|
non_complex_hint_h = generic_hint_h | simple_hint_h
|
@@ -44,7 +19,7 @@ raw_hint_h = non_complex_hint_h | complex_hint_h
|
|
44
19
|
|
45
20
|
|
46
21
|
# --- Annotated hints
|
47
|
-
annotated_hint_t = type(Annotated[object, None])
|
22
|
+
annotated_hint_t = type(h.Annotated[object, None])
|
48
23
|
|
49
24
|
|
50
25
|
# --- [Un]Annotated hints
|
@@ -54,4 +29,50 @@ any_hint_h = raw_hint_h | annotated_hint_t
|
|
54
29
|
# --- Complex hints additional components:
|
55
30
|
# - EllipsisType for GenericAlias, as in: tuple[int, ...]
|
56
31
|
# - None for UnionType, as in: int | None
|
57
|
-
complex_hint_additions_h = EllipsisType | None
|
32
|
+
complex_hint_additions_h = t.EllipsisType | None
|
33
|
+
|
34
|
+
"""
|
35
|
+
COPYRIGHT NOTICE
|
36
|
+
|
37
|
+
This software is governed by the CeCILL license under French law and
|
38
|
+
abiding by the rules of distribution of free software. You can use,
|
39
|
+
modify and/ or redistribute the software under the terms of the CeCILL
|
40
|
+
license as circulated by CEA, CNRS and INRIA at the following URL
|
41
|
+
"http://www.cecill.info".
|
42
|
+
|
43
|
+
As a counterpart to the access to the source code and rights to copy,
|
44
|
+
modify and redistribute granted by the license, users are provided only
|
45
|
+
with a limited warranty and the software's author, the holder of the
|
46
|
+
economic rights, and the successive licensors have only limited
|
47
|
+
liability.
|
48
|
+
|
49
|
+
In this respect, the user's attention is drawn to the risks associated
|
50
|
+
with loading, using, modifying and/or developing or reproducing the
|
51
|
+
software by the user in light of its specific status of free software,
|
52
|
+
that may mean that it is complicated to manipulate, and that also
|
53
|
+
therefore means that it is reserved for developers and experienced
|
54
|
+
professionals having in-depth computer knowledge. Users are therefore
|
55
|
+
encouraged to load and test the software's suitability as regards their
|
56
|
+
requirements in conditions enabling the security of their systems and/or
|
57
|
+
data to be ensured and, more generally, to use and operate it in the
|
58
|
+
same conditions as regards security.
|
59
|
+
|
60
|
+
The fact that you are presently reading this means that you have had
|
61
|
+
knowledge of the CeCILL license and that you accept its terms.
|
62
|
+
|
63
|
+
SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root.
|
64
|
+
|
65
|
+
This software is being developed by Eric Debreuve, a CNRS employee and
|
66
|
+
member of team Morpheme.
|
67
|
+
Team Morpheme is a joint team between Inria, CNRS, and UniCA.
|
68
|
+
It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory
|
69
|
+
I3S, and Laboratory iBV.
|
70
|
+
|
71
|
+
CNRS: https://www.cnrs.fr/index.php/en
|
72
|
+
Inria: https://www.inria.fr/en/
|
73
|
+
UniCA: https://univ-cotedazur.eu/
|
74
|
+
Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/
|
75
|
+
I3S: https://www.i3s.unice.fr/en/
|
76
|
+
iBV: http://ibv.unice.fr/
|
77
|
+
Team Morpheme: https://team.inria.fr/morpheme/
|
78
|
+
"""
|
str_to_obj/type/hint_tree.py
CHANGED
@@ -1,41 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# This software is governed by the CeCILL license under French law and
|
7
|
-
# abiding by the rules of distribution of free software. You can use,
|
8
|
-
# modify and/ or redistribute the software under the terms of the CeCILL
|
9
|
-
# license as circulated by CEA, CNRS and INRIA at the following URL
|
10
|
-
# "http://www.cecill.info".
|
11
|
-
#
|
12
|
-
# As a counterpart to the access to the source code and rights to copy,
|
13
|
-
# modify and redistribute granted by the license, users are provided only
|
14
|
-
# with a limited warranty and the software's author, the holder of the
|
15
|
-
# economic rights, and the successive licensors have only limited
|
16
|
-
# liability.
|
17
|
-
#
|
18
|
-
# In this respect, the user's attention is drawn to the risks associated
|
19
|
-
# with loading, using, modifying and/or developing or reproducing the
|
20
|
-
# software by the user in light of its specific status of free software,
|
21
|
-
# that may mean that it is complicated to manipulate, and that also
|
22
|
-
# therefore means that it is reserved for developers and experienced
|
23
|
-
# professionals having in-depth computer knowledge. Users are therefore
|
24
|
-
# encouraged to load and test the software's suitability as regards their
|
25
|
-
# requirements in conditions enabling the security of their systems and/or
|
26
|
-
# data to be ensured and, more generally, to use and operate it in the
|
27
|
-
# same conditions as regards security.
|
28
|
-
#
|
29
|
-
# The fact that you are presently reading this means that you have had
|
30
|
-
# knowledge of the CeCILL license and that you accept its terms.
|
31
|
-
|
32
|
-
from __future__ import annotations
|
1
|
+
"""
|
2
|
+
Copyright CNRS/Inria/UniCA
|
3
|
+
Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023
|
4
|
+
SEE COPYRIGHT NOTICE BELOW
|
5
|
+
"""
|
33
6
|
|
34
|
-
import dataclasses as
|
7
|
+
import dataclasses as d
|
35
8
|
import re as regx
|
36
|
-
|
37
|
-
|
9
|
+
import types as t
|
10
|
+
import typing as h
|
38
11
|
|
12
|
+
from logger_36.content import MessageWithActualExpected
|
13
|
+
from str_to_obj.catalog.choices import choices_t
|
39
14
|
from str_to_obj.task.inspection import HintComponents
|
40
15
|
from str_to_obj.type.annotation import annotation_t
|
41
16
|
from str_to_obj.type.hint import (
|
@@ -46,24 +21,29 @@ from str_to_obj.type.hint import (
|
|
46
21
|
)
|
47
22
|
|
48
23
|
|
49
|
-
@
|
24
|
+
@d.dataclass(slots=True, repr=False, eq=False)
|
50
25
|
class _hint_node_t:
|
51
26
|
"""
|
52
27
|
Leave elements to the tree.
|
28
|
+
literal_s: the accepted (literal) values if the hint is a typing.Literal, None
|
29
|
+
otherwise.
|
53
30
|
"""
|
54
31
|
|
55
|
-
type: non_complex_hint_h | UnionType | EllipsisType | type[NoneType]
|
56
|
-
annotations: tuple[annotation_t, ...] =
|
32
|
+
type: non_complex_hint_h | t.UnionType | t.EllipsisType | type[t.NoneType]
|
33
|
+
annotations: tuple[annotation_t, ...] = d.field(default_factory=tuple)
|
34
|
+
literal_s: tuple[h.Any, ...] | None = None
|
57
35
|
|
58
36
|
|
59
|
-
@
|
60
|
-
class
|
61
|
-
|
37
|
+
@d.dataclass(slots=True, repr=False, eq=False)
|
38
|
+
class hint_t(_hint_node_t):
|
39
|
+
"""
|
40
|
+
elements: tuple if the hint is a container, None otherwise. In terms of tree
|
41
|
+
structure, it plays the role of the (hint) node children.
|
42
|
+
"""
|
43
|
+
elements: tuple[h.Self, ...] | None = None
|
62
44
|
|
63
45
|
@classmethod
|
64
|
-
def
|
65
|
-
cls, hint: any_hint_h | complex_hint_additions_h, /
|
66
|
-
) -> hint_tree_t:
|
46
|
+
def NewForHint(cls, hint: any_hint_h | complex_hint_additions_h, /) -> h.Self:
|
67
47
|
"""
|
68
48
|
Note that type hints cannot translate into hint trees with an OR-node having a child
|
69
49
|
OR-node. For example: str | (int | float) is interpreted as str | int | float. This
|
@@ -74,30 +54,50 @@ class hint_tree_t(_hint_node_t):
|
|
74
54
|
if isinstance(hint, annotation_t):
|
75
55
|
# This is a common mistake in specifications.
|
76
56
|
raise ValueError(
|
77
|
-
|
78
|
-
|
57
|
+
MessageWithActualExpected(
|
58
|
+
"Invalid hint type", actual=type(hint).__name__, expected=any_hint_h
|
59
|
+
)
|
79
60
|
)
|
80
61
|
|
81
|
-
# Dealing with complex_hint_additions_h first
|
62
|
+
# Dealing with complex_hint_additions_h first.
|
82
63
|
if hint is Ellipsis:
|
83
|
-
return cls(type=EllipsisType)
|
84
|
-
if (hint is None) or (hint is NoneType):
|
85
|
-
return cls(type=NoneType)
|
64
|
+
return cls(type=t.EllipsisType)
|
65
|
+
if (hint is None) or (hint is t.NoneType):
|
66
|
+
return cls(type=t.NoneType)
|
86
67
|
|
87
68
|
# nnts: Do not use "annotations" since it shadows __future__.annotations.
|
88
69
|
raw_hint, nnts = HintComponents(hint)
|
89
70
|
|
90
|
-
if (origin := get_origin(raw_hint)) is None:
|
71
|
+
if (origin := h.get_origin(raw_hint)) is None:
|
91
72
|
return cls(type=raw_hint, annotations=nnts)
|
92
73
|
|
93
74
|
# Handled types: list, set, tuple, with sets using the dict delimiters { and }.
|
94
75
|
if origin is dict:
|
95
|
-
raise TypeError(f"{origin.__name__}
|
76
|
+
raise TypeError(f"Unhandled type: {origin.__name__}.")
|
77
|
+
|
78
|
+
if origin is h.Literal:
|
79
|
+
literal_s = h.get_args(raw_hint)
|
80
|
+
if any(isinstance(_elm, choices_t) for _elm in nnts):
|
81
|
+
raise ValueError(
|
82
|
+
f"Literal hint with values {literal_s} "
|
83
|
+
f"must not be annotated with {choices_t.__name__} as it leads to "
|
84
|
+
f"either redundancy or mismatch."
|
85
|
+
)
|
86
|
+
stripes = tuple(set(type(_elm) for _elm in literal_s))
|
87
|
+
if stripes.__len__() > 1:
|
88
|
+
origin = t.UnionType
|
89
|
+
elements = tuple(cls.NewForHint(_elm) for _elm in stripes)
|
90
|
+
else:
|
91
|
+
origin = stripes[0]
|
92
|
+
elements = None
|
93
|
+
return cls(
|
94
|
+
type=origin, annotations=nnts, elements=elements, literal_s=literal_s
|
95
|
+
)
|
96
96
|
|
97
|
-
if origin is Union:
|
98
|
-
origin = UnionType
|
97
|
+
if origin is h.Union:
|
98
|
+
origin = t.UnionType
|
99
99
|
# get_args returns NoneType for None. This must be taken into account above.
|
100
|
-
elements = tuple(cls.
|
100
|
+
elements = tuple(cls.NewForHint(_elm) for _elm in h.get_args(raw_hint))
|
101
101
|
return cls(type=origin, annotations=nnts, elements=elements)
|
102
102
|
|
103
103
|
@property
|
@@ -112,30 +112,43 @@ class hint_tree_t(_hint_node_t):
|
|
112
112
|
return output
|
113
113
|
|
114
114
|
@property
|
115
|
-
def template(self) -> simple_hint_h | dict[int, Any] | None:
|
115
|
+
def template(self) -> simple_hint_h | dict[int, h.Any] | None:
|
116
116
|
""""""
|
117
|
-
if self.type is NoneType:
|
117
|
+
if self.type is t.NoneType:
|
118
118
|
return None
|
119
119
|
|
120
|
-
if self.type is UnionType:
|
120
|
+
if self.type is t.UnionType:
|
121
121
|
return {_key: _elm.template for _key, _elm in enumerate(self.elements)}
|
122
122
|
|
123
123
|
if self.elements is None:
|
124
124
|
return self.type
|
125
125
|
|
126
|
-
|
126
|
+
try:
|
127
|
+
output = self.type(_elm.template for _elm in self.elements)
|
128
|
+
except TypeError:
|
129
|
+
output = None
|
130
|
+
if output is not None:
|
131
|
+
return output
|
132
|
+
|
133
|
+
# TODO: Does it work all the time?
|
134
|
+
if self.elements.__len__() == 1:
|
135
|
+
# For example, self.type is typing.Sequence.
|
136
|
+
return self.type[self.elements[0].template]
|
137
|
+
|
138
|
+
# TODO: Can something better be done?
|
139
|
+
return self.type
|
127
140
|
|
128
141
|
@property
|
129
142
|
def template_as_str(self) -> str:
|
130
143
|
""""""
|
131
144
|
output = (
|
132
145
|
str(self.template)
|
133
|
-
.replace(str(EllipsisType), "...")
|
146
|
+
.replace(str(t.EllipsisType), "...")
|
134
147
|
.replace("<class '", "")
|
135
148
|
.replace("'>", "")
|
136
149
|
)
|
137
|
-
output = regx.sub("{\d: ", "{", output, flags=regx.ASCII)
|
138
|
-
output = regx.sub(", \d:", " |", output, flags=regx.ASCII)
|
150
|
+
output = regx.sub(r"{\d: ", "{", output, flags=regx.ASCII)
|
151
|
+
output = regx.sub(r", \d:", " |", output, flags=regx.ASCII)
|
139
152
|
|
140
153
|
return output
|
141
154
|
|
@@ -149,7 +162,7 @@ class hint_tree_t(_hint_node_t):
|
|
149
162
|
# """"""
|
150
163
|
# output = [TypeAsRichStr(self)]
|
151
164
|
#
|
152
|
-
# names = (_fld.name for _fld in
|
165
|
+
# names = (_fld.name for _fld in d.fields(self))
|
153
166
|
# for name in names:
|
154
167
|
# value = getattr(self, name)
|
155
168
|
# output.append(f" {NameValueTypeAsRichStr(name, value, separator='@=@')}")
|
@@ -157,3 +170,49 @@ class hint_tree_t(_hint_node_t):
|
|
157
170
|
# output = AlignedOnSeparator(output, "@=@", " = ")
|
158
171
|
#
|
159
172
|
# return "\n".join(output)
|
173
|
+
|
174
|
+
"""
|
175
|
+
COPYRIGHT NOTICE
|
176
|
+
|
177
|
+
This software is governed by the CeCILL license under French law and
|
178
|
+
abiding by the rules of distribution of free software. You can use,
|
179
|
+
modify and/ or redistribute the software under the terms of the CeCILL
|
180
|
+
license as circulated by CEA, CNRS and INRIA at the following URL
|
181
|
+
"http://www.cecill.info".
|
182
|
+
|
183
|
+
As a counterpart to the access to the source code and rights to copy,
|
184
|
+
modify and redistribute granted by the license, users are provided only
|
185
|
+
with a limited warranty and the software's author, the holder of the
|
186
|
+
economic rights, and the successive licensors have only limited
|
187
|
+
liability.
|
188
|
+
|
189
|
+
In this respect, the user's attention is drawn to the risks associated
|
190
|
+
with loading, using, modifying and/or developing or reproducing the
|
191
|
+
software by the user in light of its specific status of free software,
|
192
|
+
that may mean that it is complicated to manipulate, and that also
|
193
|
+
therefore means that it is reserved for developers and experienced
|
194
|
+
professionals having in-depth computer knowledge. Users are therefore
|
195
|
+
encouraged to load and test the software's suitability as regards their
|
196
|
+
requirements in conditions enabling the security of their systems and/or
|
197
|
+
data to be ensured and, more generally, to use and operate it in the
|
198
|
+
same conditions as regards security.
|
199
|
+
|
200
|
+
The fact that you are presently reading this means that you have had
|
201
|
+
knowledge of the CeCILL license and that you accept its terms.
|
202
|
+
|
203
|
+
SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root.
|
204
|
+
|
205
|
+
This software is being developed by Eric Debreuve, a CNRS employee and
|
206
|
+
member of team Morpheme.
|
207
|
+
Team Morpheme is a joint team between Inria, CNRS, and UniCA.
|
208
|
+
It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory
|
209
|
+
I3S, and Laboratory iBV.
|
210
|
+
|
211
|
+
CNRS: https://www.cnrs.fr/index.php/en
|
212
|
+
Inria: https://www.inria.fr/en/
|
213
|
+
UniCA: https://univ-cotedazur.eu/
|
214
|
+
Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/
|
215
|
+
I3S: https://www.i3s.unice.fr/en/
|
216
|
+
iBV: http://ibv.unice.fr/
|
217
|
+
Team Morpheme: https://team.inria.fr/morpheme/
|
218
|
+
"""
|
str_to_obj/type/type.py
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
"""
|
2
|
+
Copyright CNRS/Inria/UniCA
|
3
|
+
Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023
|
4
|
+
SEE COPYRIGHT NOTICE BELOW
|
5
|
+
"""
|
6
|
+
|
7
|
+
import dataclasses as d
|
8
|
+
import typing as h
|
9
|
+
|
10
|
+
from rich.text import Text as text_t
|
11
|
+
from str_to_obj.main import ObjectFromStr
|
12
|
+
from str_to_obj.runtime.value import INVALID_VALUE
|
13
|
+
from str_to_obj.task.casting import CastValue
|
14
|
+
from str_to_obj.type.annotation import annotation_t
|
15
|
+
from str_to_obj.type.hint_tree import hint_t
|
16
|
+
|
17
|
+
|
18
|
+
@d.dataclass(slots=True, repr=False, eq=False)
|
19
|
+
class type_t(hint_t):
|
20
|
+
def FirstAnnotationWithType(
|
21
|
+
self, stripe: type[annotation_t], /
|
22
|
+
) -> annotation_t | None:
|
23
|
+
""""""
|
24
|
+
for annotation in self.all_annotations:
|
25
|
+
if isinstance(annotation, stripe):
|
26
|
+
return annotation
|
27
|
+
|
28
|
+
return None
|
29
|
+
|
30
|
+
def ValueIssues(self, value: h.Any, /) -> list[str]:
|
31
|
+
""""""
|
32
|
+
return CastValue(value, self, only_check_validity=True)
|
33
|
+
|
34
|
+
def InterpretedValueOf(self, value: h.Any, /) -> tuple[h.Any, list[str]]:
|
35
|
+
""""""
|
36
|
+
if isinstance(value, str):
|
37
|
+
typed_value, issues = ObjectFromStr(value, expected_type=self)
|
38
|
+
else:
|
39
|
+
typed_value, issues = CastValue(value, self)
|
40
|
+
|
41
|
+
if issues.__len__() > 0:
|
42
|
+
return INVALID_VALUE, issues
|
43
|
+
|
44
|
+
return typed_value, []
|
45
|
+
|
46
|
+
def __str__(self) -> str:
|
47
|
+
""""""
|
48
|
+
return text_t.from_markup(self.__rich__()).plain
|
49
|
+
|
50
|
+
def __rich__(self) -> str:
|
51
|
+
""""""
|
52
|
+
output = [f"[white]{self.template_as_str}[/]"]
|
53
|
+
|
54
|
+
for annotation in self.all_annotations:
|
55
|
+
output.append(type(annotation).__name__)
|
56
|
+
|
57
|
+
return "[yellow]" + "::".join(output) + "[/]"
|
58
|
+
|
59
|
+
|
60
|
+
@d.dataclass(slots=True, repr=False, eq=False)
|
61
|
+
class any_type_t(type_t):
|
62
|
+
def FirstAnnotationWithType(
|
63
|
+
self, stripe: type[annotation_t], /
|
64
|
+
) -> annotation_t | None:
|
65
|
+
""""""
|
66
|
+
return None
|
67
|
+
|
68
|
+
def ValueIssues(self, _: h.Any, /) -> list[str]:
|
69
|
+
""""""
|
70
|
+
return []
|
71
|
+
|
72
|
+
def InterpretedValueOf(self, value: h.Any, /) -> tuple[h.Any, list[str]]:
|
73
|
+
""""""
|
74
|
+
return value, []
|
75
|
+
|
76
|
+
|
77
|
+
"""
|
78
|
+
COPYRIGHT NOTICE
|
79
|
+
|
80
|
+
This software is governed by the CeCILL license under French law and
|
81
|
+
abiding by the rules of distribution of free software. You can use,
|
82
|
+
modify and/ or redistribute the software under the terms of the CeCILL
|
83
|
+
license as circulated by CEA, CNRS and INRIA at the following URL
|
84
|
+
"http://www.cecill.info".
|
85
|
+
|
86
|
+
As a counterpart to the access to the source code and rights to copy,
|
87
|
+
modify and redistribute granted by the license, users are provided only
|
88
|
+
with a limited warranty and the software's author, the holder of the
|
89
|
+
economic rights, and the successive licensors have only limited
|
90
|
+
liability.
|
91
|
+
|
92
|
+
In this respect, the user's attention is drawn to the risks associated
|
93
|
+
with loading, using, modifying and/or developing or reproducing the
|
94
|
+
software by the user in light of its specific status of free software,
|
95
|
+
that may mean that it is complicated to manipulate, and that also
|
96
|
+
therefore means that it is reserved for developers and experienced
|
97
|
+
professionals having in-depth computer knowledge. Users are therefore
|
98
|
+
encouraged to load and test the software's suitability as regards their
|
99
|
+
requirements in conditions enabling the security of their systems and/or
|
100
|
+
data to be ensured and, more generally, to use and operate it in the
|
101
|
+
same conditions as regards security.
|
102
|
+
|
103
|
+
The fact that you are presently reading this means that you have had
|
104
|
+
knowledge of the CeCILL license and that you accept its terms.
|
105
|
+
|
106
|
+
SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root.
|
107
|
+
|
108
|
+
This software is being developed by Eric Debreuve, a CNRS employee and
|
109
|
+
member of team Morpheme.
|
110
|
+
Team Morpheme is a joint team between Inria, CNRS, and UniCA.
|
111
|
+
It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory
|
112
|
+
I3S, and Laboratory iBV.
|
113
|
+
|
114
|
+
CNRS: https://www.cnrs.fr/index.php/en
|
115
|
+
Inria: https://www.inria.fr/en/
|
116
|
+
UniCA: https://univ-cotedazur.eu/
|
117
|
+
Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/
|
118
|
+
I3S: https://www.i3s.unice.fr/en/
|
119
|
+
iBV: http://ibv.unice.fr/
|
120
|
+
Team Morpheme: https://team.inria.fr/morpheme/
|
121
|
+
"""
|