typico 0.0.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.
- typico/__init__.py +4 -0
- typico/py.typed +0 -0
- typico/pyfield.py +198 -0
- typico/utils.py +55 -0
- typico-0.0.1.dist-info/METADATA +81 -0
- typico-0.0.1.dist-info/RECORD +8 -0
- typico-0.0.1.dist-info/WHEEL +4 -0
- typico-0.0.1.dist-info/licenses/LICENSE +22 -0
typico/__init__.py
ADDED
typico/py.typed
ADDED
|
File without changes
|
typico/pyfield.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import TYPE_CHECKING, Annotated, Any, TypeVar, get_args, get_origin
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from pydantic import BaseModel
|
|
9
|
+
from pydantic.fields import FieldInfo
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T", bound="BaseModel")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class ValidationConstraints:
|
|
17
|
+
"""Encapsulation of field validation constraints."""
|
|
18
|
+
|
|
19
|
+
min_value: float | None = None
|
|
20
|
+
"""Minimum allowed value for numeric fields."""
|
|
21
|
+
|
|
22
|
+
max_value: float | None = None
|
|
23
|
+
"""Maximum allowed value for numeric fields."""
|
|
24
|
+
|
|
25
|
+
exclusive_min: bool = False
|
|
26
|
+
"""If True, the minimum value is exclusive (value must be greater than min_value)."""
|
|
27
|
+
|
|
28
|
+
exclusive_max: bool = False
|
|
29
|
+
"""If True, the maximum value is exclusive (value must be less than max_value)."""
|
|
30
|
+
|
|
31
|
+
multiple_of: float | None = None
|
|
32
|
+
"""If set, the value must be a multiple of this number."""
|
|
33
|
+
|
|
34
|
+
min_length: int | None = None
|
|
35
|
+
"""Minimum length for strings or collections."""
|
|
36
|
+
|
|
37
|
+
max_length: int | None = None
|
|
38
|
+
"""Maximum length for strings or collections."""
|
|
39
|
+
|
|
40
|
+
pattern: str | None = None
|
|
41
|
+
"""Regular expression pattern that strings must match."""
|
|
42
|
+
|
|
43
|
+
min_items: int | None = None
|
|
44
|
+
"""Minimum number of items for array/list types."""
|
|
45
|
+
|
|
46
|
+
max_items: int | None = None
|
|
47
|
+
"""Maximum number of items for array/list types."""
|
|
48
|
+
|
|
49
|
+
allowed_values: list[Any] | None = None
|
|
50
|
+
"""List of allowed values (for enums, literals, or constrained types)."""
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def from_pydantic_field(cls, field_info: FieldInfo) -> ValidationConstraints:
|
|
54
|
+
"""Extract constraints directly from Pydantic field info."""
|
|
55
|
+
constraints: dict[str, Any] = {}
|
|
56
|
+
|
|
57
|
+
from annotated_types import Ge, Gt, Le, Lt, MaxLen, MinLen, MultipleOf
|
|
58
|
+
|
|
59
|
+
for meta in field_info.metadata:
|
|
60
|
+
match meta:
|
|
61
|
+
case Gt():
|
|
62
|
+
constraints["min_value"] = meta.gt
|
|
63
|
+
constraints["exclusive_min"] = True
|
|
64
|
+
case Ge():
|
|
65
|
+
constraints["min_value"] = meta.ge
|
|
66
|
+
case Lt():
|
|
67
|
+
constraints["max_value"] = meta.lt
|
|
68
|
+
constraints["exclusive_max"] = True
|
|
69
|
+
case Le():
|
|
70
|
+
constraints["max_value"] = meta.le
|
|
71
|
+
case MultipleOf():
|
|
72
|
+
constraints["multiple_of"] = meta.multiple_of
|
|
73
|
+
case MinLen():
|
|
74
|
+
constraints["min_length"] = meta.min_length
|
|
75
|
+
case MaxLen():
|
|
76
|
+
constraints["max_length"] = meta.max_length
|
|
77
|
+
# Easy to extend with more cases as needed
|
|
78
|
+
|
|
79
|
+
return cls(**constraints)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass
|
|
83
|
+
class PyField[T]:
|
|
84
|
+
"""Generic representation of a field focused on type semantics."""
|
|
85
|
+
|
|
86
|
+
name: str
|
|
87
|
+
"""The name of the field in the model."""
|
|
88
|
+
|
|
89
|
+
raw_type: Any
|
|
90
|
+
"""The Python type annotation of the field."""
|
|
91
|
+
|
|
92
|
+
parent_model: type[T] | None = None
|
|
93
|
+
"""The parent model class containing this field."""
|
|
94
|
+
|
|
95
|
+
field_type: str | None = None
|
|
96
|
+
"""The field type, based on Annoated convention."""
|
|
97
|
+
|
|
98
|
+
title: str | None = None
|
|
99
|
+
"""Display title for the field."""
|
|
100
|
+
|
|
101
|
+
description: str | None = None
|
|
102
|
+
"""Detailed description of the field."""
|
|
103
|
+
|
|
104
|
+
placeholder: str | None = None
|
|
105
|
+
"""Placeholder text to show when the field is empty."""
|
|
106
|
+
|
|
107
|
+
examples: list[Any] | None = None
|
|
108
|
+
"""Example values for the field."""
|
|
109
|
+
|
|
110
|
+
hidden: bool = False
|
|
111
|
+
"""Whether the field should be hidden from serialization or presentation."""
|
|
112
|
+
|
|
113
|
+
readonly: bool = False
|
|
114
|
+
"""Whether the field is immutable after initialization."""
|
|
115
|
+
|
|
116
|
+
deprecated: bool = False
|
|
117
|
+
"""Whether the field is marked as deprecated."""
|
|
118
|
+
|
|
119
|
+
is_required: bool = True
|
|
120
|
+
"""Whether the field is required for validation."""
|
|
121
|
+
|
|
122
|
+
default_value: Any = None
|
|
123
|
+
"""The default value for the field."""
|
|
124
|
+
|
|
125
|
+
has_default: bool = False
|
|
126
|
+
"""Whether the field has a default value defined."""
|
|
127
|
+
|
|
128
|
+
constraints: ValidationConstraints = field(default_factory=ValidationConstraints)
|
|
129
|
+
"""Validation constraints for the field."""
|
|
130
|
+
|
|
131
|
+
metadata: dict[str, Any] = field(default_factory=dict)
|
|
132
|
+
"""Additional metadata associated with the field."""
|
|
133
|
+
|
|
134
|
+
@classmethod
|
|
135
|
+
def from_pydantic(
|
|
136
|
+
cls,
|
|
137
|
+
name: str,
|
|
138
|
+
field_info: FieldInfo,
|
|
139
|
+
parent_model: type[BaseModel] | None = None,
|
|
140
|
+
) -> PyField:
|
|
141
|
+
"""Create a PyField from Pydantic field information."""
|
|
142
|
+
raw_type = field_info.annotation
|
|
143
|
+
field_type = None
|
|
144
|
+
if get_origin(raw_type) == Annotated: # Direct comparison with the actual type
|
|
145
|
+
args = get_args(raw_type)
|
|
146
|
+
raw_type = args[0] # Actual type is first argument
|
|
147
|
+
|
|
148
|
+
# Extract field_type from any dict arguments
|
|
149
|
+
for arg in args[1:]:
|
|
150
|
+
if isinstance(arg, dict) and "field_type" in arg:
|
|
151
|
+
field_type = arg["field_type"]
|
|
152
|
+
break
|
|
153
|
+
|
|
154
|
+
# Check json_schema_extra as an alternative source for field_type
|
|
155
|
+
if (
|
|
156
|
+
field_type is None
|
|
157
|
+
and field_info.json_schema_extra
|
|
158
|
+
and (
|
|
159
|
+
isinstance(field_info.json_schema_extra, dict)
|
|
160
|
+
and "field_type" in field_info.json_schema_extra
|
|
161
|
+
)
|
|
162
|
+
):
|
|
163
|
+
field_type = field_info.json_schema_extra["field_type"]
|
|
164
|
+
|
|
165
|
+
# Get required status (we still need model_json_schema for this)
|
|
166
|
+
is_required = True # Default to True
|
|
167
|
+
if parent_model:
|
|
168
|
+
schema = parent_model.model_json_schema()
|
|
169
|
+
is_required = name in schema.get("required", [])
|
|
170
|
+
|
|
171
|
+
from pydantic.fields import PydanticUndefined
|
|
172
|
+
|
|
173
|
+
has_default = field_info.default is not PydanticUndefined
|
|
174
|
+
default_value = field_info.default if has_default else None
|
|
175
|
+
constraints = ValidationConstraints.from_pydantic_field(field_info)
|
|
176
|
+
examples = field_info.examples
|
|
177
|
+
|
|
178
|
+
# Use first example as placeholder if available
|
|
179
|
+
placeholder = str(examples[0]) if examples and examples[0] is not None else None
|
|
180
|
+
|
|
181
|
+
return cls(
|
|
182
|
+
name=name,
|
|
183
|
+
raw_type=raw_type,
|
|
184
|
+
parent_model=parent_model,
|
|
185
|
+
field_type=field_type,
|
|
186
|
+
title=field_info.title or name.replace("_", " ").capitalize(),
|
|
187
|
+
description=field_info.description,
|
|
188
|
+
placeholder=placeholder,
|
|
189
|
+
examples=examples,
|
|
190
|
+
hidden=field_info.exclude is True,
|
|
191
|
+
readonly=field_info.frozen is True,
|
|
192
|
+
deprecated=field_info.deprecated is not None,
|
|
193
|
+
is_required=is_required,
|
|
194
|
+
default_value=default_value,
|
|
195
|
+
has_default=has_default,
|
|
196
|
+
constraints=constraints,
|
|
197
|
+
metadata={},
|
|
198
|
+
)
|
typico/utils.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import pydantic
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_json_schema_extra(field_info: pydantic.fields.FieldInfo) -> dict[str, Any]:
|
|
9
|
+
"""Extract JSON schema extra information from a field.
|
|
10
|
+
|
|
11
|
+
Handles both when json_schema_extra is a dict and when it's a callable.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
field_info: The Pydantic field info object
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
A dictionary of schema extra information
|
|
18
|
+
"""
|
|
19
|
+
if not field_info.json_schema_extra:
|
|
20
|
+
return {}
|
|
21
|
+
|
|
22
|
+
if isinstance(field_info.json_schema_extra, dict):
|
|
23
|
+
return field_info.json_schema_extra
|
|
24
|
+
|
|
25
|
+
# Handle callable json_schema_extra
|
|
26
|
+
if callable(field_info.json_schema_extra):
|
|
27
|
+
schema_dict: dict[str, Any] = {}
|
|
28
|
+
field_info.json_schema_extra(schema_dict)
|
|
29
|
+
return schema_dict
|
|
30
|
+
return {}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_default_value[T](field_info: pydantic.fields.FieldInfo, fallback: T) -> T:
|
|
34
|
+
"""Get default value for an integer field.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
field_info: Pydantic field info
|
|
38
|
+
fallback: Default value to use if no default is defined
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
Default value for the field
|
|
42
|
+
"""
|
|
43
|
+
# Check for default value
|
|
44
|
+
if (
|
|
45
|
+
field_info.default is not None
|
|
46
|
+
and field_info.default is not pydantic.fields.PydanticUndefined
|
|
47
|
+
):
|
|
48
|
+
return field_info.default
|
|
49
|
+
|
|
50
|
+
# Check for default factory
|
|
51
|
+
if field_info.default_factory is not None:
|
|
52
|
+
if field_info.default_factory_takes_validated_data:
|
|
53
|
+
return field_info.default_factory({}) # type: ignore
|
|
54
|
+
return field_info.default_factory() # type: ignore
|
|
55
|
+
return fallback
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: typico
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Type helpers and abstractions
|
|
5
|
+
Project-URL: Documentation, https://phil65.github.io/typico/
|
|
6
|
+
Project-URL: Source, https://github.com/phil65/typico
|
|
7
|
+
Project-URL: Issues, https://github.com/phil65/typico/issues
|
|
8
|
+
Project-URL: Discussions, https://github.com/phil65/typico/discussions
|
|
9
|
+
Project-URL: Code coverage, https://app.codecov.io/gh/phil65/typico
|
|
10
|
+
Author-email: Philipp Temminghoff <philipptemminghoff@googlemail.com>
|
|
11
|
+
License: MIT License
|
|
12
|
+
|
|
13
|
+
Copyright (c) 2024, Philipp Temminghoff
|
|
14
|
+
|
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
in the Software without restriction, including without limitation the rights
|
|
18
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all
|
|
23
|
+
copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Classifier: Development Status :: 4 - Beta
|
|
35
|
+
Classifier: Intended Audience :: Developers
|
|
36
|
+
Classifier: Operating System :: OS Independent
|
|
37
|
+
Classifier: Programming Language :: Python :: 3
|
|
38
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
42
|
+
Classifier: Topic :: Documentation
|
|
43
|
+
Classifier: Topic :: Software Development
|
|
44
|
+
Classifier: Topic :: Utilities
|
|
45
|
+
Classifier: Typing :: Typed
|
|
46
|
+
Requires-Python: >=3.12
|
|
47
|
+
Requires-Dist: typing-inspection>=0.4.0
|
|
48
|
+
Description-Content-Type: text/markdown
|
|
49
|
+
|
|
50
|
+
# Typico
|
|
51
|
+
|
|
52
|
+
[](https://pypi.org/project/typico/)
|
|
53
|
+
[](https://pypi.org/project/typico/)
|
|
54
|
+
[](https://pypi.org/project/typico/)
|
|
55
|
+
[](https://pypi.org/project/typico/)
|
|
56
|
+
[](https://pypi.org/project/typico/)
|
|
57
|
+
[](https://pypi.org/project/typico/)
|
|
58
|
+
[](https://pypi.org/project/typico/)
|
|
59
|
+
[](https://pypi.org/project/typico/)
|
|
60
|
+
[](https://pypi.org/project/typico/)
|
|
61
|
+
[](https://github.com/phil65/typico/releases)
|
|
62
|
+
[](https://github.com/phil65/typico/graphs/contributors)
|
|
63
|
+
[](https://github.com/phil65/typico/discussions)
|
|
64
|
+
[](https://github.com/phil65/typico/forks)
|
|
65
|
+
[](https://github.com/phil65/typico/issues)
|
|
66
|
+
[](https://github.com/phil65/typico/pulls)
|
|
67
|
+
[](https://github.com/phil65/typico/watchers)
|
|
68
|
+
[](https://github.com/phil65/typico/stars)
|
|
69
|
+
[](https://github.com/phil65/typico)
|
|
70
|
+
[](https://github.com/phil65/typico/commits)
|
|
71
|
+
[](https://github.com/phil65/typico/releases)
|
|
72
|
+
[](https://github.com/phil65/typico)
|
|
73
|
+
[](https://github.com/phil65/typico)
|
|
74
|
+
[](https://github.com/phil65/typico)
|
|
75
|
+
[](https://github.com/phil65/typico)
|
|
76
|
+
[](https://codecov.io/gh/phil65/typico/)
|
|
77
|
+
[](https://github.com/psf/black)
|
|
78
|
+
[](https://pyup.io/repos/github/phil65/typico/)
|
|
79
|
+
|
|
80
|
+
[Read the documentation!](https://phil65.github.io/typico/)
|
|
81
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
typico/__init__.py,sha256=r4gcjZ0Eo5Fa3ctw0N1h-b5QGozQq9SsbxT9Q-uMhJw,37
|
|
2
|
+
typico/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
typico/pyfield.py,sha256=WkeuEiRycDdYG9eLhCvoVhBe4DGAU8loXIDj7XUz2EM,6914
|
|
4
|
+
typico/utils.py,sha256=fdsikBb6z-V0tRSU40S4M-CO36-gGk8LOpaEyKvJp7k,1647
|
|
5
|
+
typico-0.0.1.dist-info/METADATA,sha256=skFf6V3Xbh_GzJQyOTVZt5eWmVsAY9vFjBj60RiTW9Q,5535
|
|
6
|
+
typico-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
typico-0.0.1.dist-info/licenses/LICENSE,sha256=AteGCH9r177TxxrOFEiOARrastASsf7yW6MQxlAHdwA,1078
|
|
8
|
+
typico-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Philipp Temminghoff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|