Skip to content

Commit ece8d7d

Browse files
feat: update dict_to_schema to use dataclasses with inferred types
Co-Authored-By: vshenoy@codegen.com <vshenoy@codegen.com>
1 parent 5f635b6 commit ece8d7d

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

examples/dict_to_schema/README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Dict to Schema
22

3-
This example demonstrates how to automatically convert Python dictionary literals into Pydantic models. The codemod makes this process simple by handling all the tedious manual updates automatically.
3+
This example demonstrates how to automatically convert Python dictionary literals into dataclasses with proper type hints. The codemod makes this process simple by handling all the tedious manual updates automatically.
44

55
## How the Conversion Script Works
66

@@ -17,9 +17,9 @@ The script (`run.py`) automates the entire conversion process in a few key steps
1717
- Maintains proper Python indentation
1818

1919
3. **Code Updates**
20-
- Inserts new Pydantic models in appropriate locations
21-
- Updates dictionary assignments to use the new models
22-
- Automatically adds required Pydantic imports
20+
- Inserts new dataclass definitions in appropriate locations
21+
- Updates dictionary assignments to use the new dataclasses
22+
- Automatically adds required imports for dataclasses and typing
2323

2424
## Example Transformations
2525

@@ -29,26 +29,44 @@ The script (`run.py`) automates the entire conversion process in a few key steps
2929
app_config = {"host": "localhost", "port": 8080}
3030

3131
# After
32-
class AppConfigSchema(BaseModel):
33-
host: str = "localhost"
34-
port: int = 8080
32+
@dataclass
33+
class AppConfig:
34+
host: str | None = None
35+
port: int | None = None
3536

36-
app_config = AppConfigSchema(**{"host": "localhost", "port": 8080})
37+
app_config = AppConfig(host="localhost", port=8080)
38+
39+
# List Example
40+
books = [
41+
{"id": 1, "title": "Book One", "author": "Author A"},
42+
{"id": 2, "title": "Book Two", "author": "Author B"}
43+
]
44+
45+
# After
46+
@dataclass
47+
class Book:
48+
id: int | None = None
49+
title: str | None = None
50+
author: str | None = None
51+
52+
books = [Book(**item) for item in books]
3753
```
3854

3955
### Class Attributes
4056
```python
4157
# Before
4258
class Service:
43-
defaults = {"timeout": 30, "retries": 3}
59+
defaults = {"timeout": 30, "retries": 3, "backoff": 1.5}
4460

4561
# After
46-
class DefaultsSchema(BaseModel):
47-
timeout: int = 30
48-
retries: int = 3
62+
@dataclass
63+
class Defaults:
64+
timeout: int | None = None
65+
retries: int | None = None
66+
backoff: float | None = None
4967

5068
class Service:
51-
defaults = DefaultsSchema(**{"timeout": 30, "retries": 3})
69+
defaults = Defaults(timeout=30, retries=3, backoff=1.5)
5270
```
5371

5472
## Running the Conversion
@@ -63,5 +81,5 @@ codegen run dict_to_schema
6381

6482
## Learn More
6583

66-
- [Pydantic Documentation](https://docs.pydantic.dev/)
84+
- [Python Dataclasses Documentation](https://docs.python.org/3/library/dataclasses.html)
6785
- [Codegen Documentation](https://docs.codegen.com)

0 commit comments

Comments
 (0)