Skip to content

Commit 8f4df80

Browse files
committed
added first version of utils/type_safe/Type_Safe__To__BaseModel (with code, tests and documentation)
1 parent 497dda0 commit 8f4df80

5 files changed

Lines changed: 1057 additions & 0 deletions

File tree

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
# Type_Safe Converters Architecture
2+
3+
## 🎯 Overview
4+
5+
The Type_Safe converters provide bidirectional transformation between Type_Safe classes and standard Python model frameworks (Pydantic BaseModel and dataclasses). This enables Type_Safe to integrate seamlessly with popular frameworks like FastAPI while maintaining its strong type safety guarantees.
6+
7+
**Status**: Production Ready
8+
**Version**: 1.0.0
9+
**Compatibility**: Python 3.8+, Pydantic v2+, FastAPI 0.100+
10+
11+
## 🏗️ Architecture Overview
12+
13+
```mermaid
14+
graph TB
15+
subgraph "Type_Safe Ecosystem"
16+
TS[Type_Safe Class]
17+
TSI[Type_Safe Instance]
18+
end
19+
20+
subgraph "Converters Hub"
21+
TSB[Type_Safe__To__BaseModel]
22+
BTS[BaseModel__To__Type_Safe]
23+
TSD[Type_Safe__To__Dataclass]
24+
DTS[Dataclass__To__Type_Safe]
25+
BTD[BaseModel__To__Dataclass]
26+
DTB[Dataclass__To__BaseModel]
27+
end
28+
29+
subgraph "External Frameworks"
30+
BM[Pydantic BaseModel]
31+
DC[Python Dataclass]
32+
FA[FastAPI]
33+
SQL[SQLAlchemy]
34+
end
35+
36+
TS --> TSB --> BM
37+
BM --> BTS --> TS
38+
TS --> TSD --> DC
39+
DC --> DTS --> TS
40+
BM --> BTD --> DC
41+
DC --> DTB --> BM
42+
43+
BM -.-> FA
44+
DC -.-> SQL
45+
46+
style TS fill:#e1f5fe
47+
style TSI fill:#e1f5fe
48+
style TSB fill:#fff3e0
49+
style BTS fill:#fff3e0
50+
style TSD fill:#f3e5f5
51+
style DTS fill:#f3e5f5
52+
style BTD fill:#e8f5e9
53+
style DTB fill:#e8f5e9
54+
```
55+
56+
## 🔄 Conversion Matrix
57+
58+
| From → To | Type_Safe | BaseModel | Dataclass |
59+
|-----------|-----------|-----------|-----------|
60+
| **Type_Safe** | - | `Type_Safe__To__BaseModel` | `Type_Safe__To__Dataclass` |
61+
| **BaseModel** | `BaseModel__To__Type_Safe` | - | `BaseModel__To__Dataclass` |
62+
| **Dataclass** | `Dataclass__To__Type_Safe` | `Dataclass__To__BaseModel` | - |
63+
64+
## 🎭 Design Principles
65+
66+
### 1. **Type_Safe as Source of Truth**
67+
Type_Safe remains the canonical representation. Conversions are views/projections, not replacements.
68+
69+
### 2. **Lazy Conversion**
70+
Only convert what's needed when it's needed for performance optimization.
71+
72+
### 3. **Type Information Preservation**
73+
Maintain type hints, validators, and metadata through conversion cycles.
74+
75+
### 4. **Minimal Surface Area**
76+
Only implement the subset of BaseModel/dataclass features needed for compatibility.
77+
78+
### 5. **Bidirectional Fidelity**
79+
Round-trip conversions should maintain data integrity: `Type_Safe → BaseModel → Type_Safe` should preserve information.
80+
81+
## 🔧 Core Components
82+
83+
### Converter Base Pattern
84+
85+
Each converter follows this structure:
86+
87+
```python
88+
class Type_Safe__To__X(Type_Safe):
89+
model_cache: Dict[Type, Type[X]] # Cache for generated models
90+
91+
@type_safe
92+
def convert_class(self, source_class: Type[Source]) -> Type[Target]:
93+
# Convert class definition
94+
pass
95+
96+
@type_safe
97+
def convert_instance(self, source_instance: Source) -> Target:
98+
# Convert instance with data
99+
pass
100+
```
101+
102+
### Type Mapping Registry
103+
104+
```mermaid
105+
graph LR
106+
subgraph "Type_Safe Types"
107+
TSL[Type_Safe__List]
108+
TSD[Type_Safe__Dict]
109+
TSS[Type_Safe__Set]
110+
TSP[Type_Safe Primitives]
111+
end
112+
113+
subgraph "Pydantic Types"
114+
PL[List]
115+
PD[Dict]
116+
PS[List - no Set]
117+
PP[Standard Types]
118+
end
119+
120+
subgraph "Dataclass Types"
121+
DL[list]
122+
DD[dict]
123+
DS[set]
124+
DP[Standard Types]
125+
end
126+
127+
TSL -.-> PL -.-> DL
128+
TSD -.-> PD -.-> DD
129+
TSS -.-> PS -.-> DS
130+
TSP -.-> PP -.-> DP
131+
```
132+
133+
## 🚀 Usage Patterns
134+
135+
### FastAPI Integration
136+
137+
```python
138+
# Define in Type_Safe
139+
class UserRequest(Type_Safe):
140+
username: str
141+
email: str
142+
age: int
143+
144+
# Convert for FastAPI
145+
UserRequestModel = type_safe__to__basemodel.convert_class(UserRequest)
146+
147+
@app.post("/users")
148+
async def create_user(user: UserRequestModel):
149+
# Convert back to Type_Safe for business logic
150+
type_safe_user = basemodel__to__type_safe.convert_instance(user)
151+
# Process with Type_Safe guarantees
152+
result = process_user(type_safe_user)
153+
# Convert back for response
154+
return type_safe__to__basemodel.convert_instance(result)
155+
```
156+
157+
### Dataclass Integration
158+
159+
```python
160+
# For ORM compatibility
161+
@dataclass
162+
class UserORM:
163+
id: int
164+
username: str
165+
created_at: datetime
166+
167+
# Convert from Type_Safe for database
168+
user_orm = type_safe__to__dataclass.convert_instance(type_safe_user)
169+
db.session.add(user_orm)
170+
171+
# Convert back after retrieval
172+
type_safe_user = dataclass__to__type_safe.convert_instance(user_orm)
173+
```
174+
175+
## 🎯 Converter Responsibilities
176+
177+
### Type_Safe → BaseModel
178+
- Dynamic BaseModel class generation
179+
- Type annotation mapping
180+
- Default value preservation
181+
- Nested Type_Safe handling
182+
- Collection type conversion
183+
- Validator migration
184+
185+
### BaseModel → Type_Safe
186+
- Field extraction via `.dict()`
187+
- Type reconstruction
188+
- Collection wrapping (Type_Safe__List, etc.)
189+
- Validation error mapping
190+
- Custom validator preservation
191+
192+
### Type_Safe → Dataclass
193+
- `@dataclass` class generation
194+
- Field definition with defaults
195+
- Type hint mapping
196+
- `field()` metadata for constraints
197+
- Post-init validation hooks
198+
199+
### Dataclass → Type_Safe
200+
- Field introspection via `fields()`
201+
- Type mapping reconstruction
202+
- Default factory handling
203+
- Metadata extraction
204+
- Type_Safe validation application
205+
206+
### BaseModel ↔ Dataclass
207+
- Leverage Pydantic's existing capabilities
208+
- Use `model_validate()` for dataclass → BaseModel
209+
- Use `asdict()` for BaseModel → dataclass
210+
- Maintain type safety throughout
211+
212+
## 🔐 Security Considerations
213+
214+
1. **Type Validation**: All conversions maintain strict type checking
215+
2. **Injection Prevention**: No dynamic code execution during conversion
216+
3. **Memory Safety**: Cached models use WeakKeyDictionary where appropriate
217+
4. **Data Sanitization**: Input validation at conversion boundaries
218+
219+
## ⚡ Performance Characteristics
220+
221+
| Operation | Time Complexity | Space Complexity | Cached |
222+
|-----------|----------------|------------------|---------|
223+
| Class Conversion | O(n) fields | O(1) ||
224+
| Instance Conversion | O(n) fields | O(n) data ||
225+
| Nested Conversion | O(n*m) depth | O(n*m) | Partial |
226+
| Collection Conversion | O(n) items | O(n) ||
227+
228+
## 🧪 Testing Strategy
229+
230+
```mermaid
231+
graph TD
232+
A[Unit Tests] --> B[Simple Conversions]
233+
A --> C[Complex Nested Structures]
234+
A --> D[Collection Types]
235+
A --> E[Edge Cases]
236+
237+
F[Integration Tests] --> G[FastAPI Routes]
238+
F --> H[SQLAlchemy Models]
239+
F --> I[Round-trip Fidelity]
240+
241+
J[Performance Tests] --> K[Caching Efficiency]
242+
J --> L[Large Dataset Conversion]
243+
J --> M[Memory Usage]
244+
```
245+
246+
## 📊 Metrics & Monitoring
247+
248+
Key metrics to track:
249+
- Conversion success rate
250+
- Cache hit ratio
251+
- Average conversion time
252+
- Memory usage per model
253+
- Type mismatch frequency
254+
255+
## 🚦 Error Handling
256+
257+
```python
258+
try:
259+
converted = converter.convert_instance(source)
260+
except TypeError as e:
261+
# Type mismatch during conversion
262+
log.error(f"Type conversion failed: {e}")
263+
except ValueError as e:
264+
# Validation error
265+
log.error(f"Validation failed: {e}")
266+
except AttributeError as e:
267+
# Missing required field
268+
log.error(f"Field missing: {e}")
269+
```

0 commit comments

Comments
 (0)