Skip to content

Commit 7a2a960

Browse files
vortex14typhoon
andauthored
added bytes type for Pydantic (#31)
* added bytes type for Pydantic * Testing a byte type field for a pydantic model * removed autogenerated models.py * W292 no newline at end of file * W292 no newline at end of file Co-authored-by: typhoon <[email protected]>
1 parent a523933 commit 7a2a960

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

omymodels/models/pydantic/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
numeric_types,
88
boolean_types,
99
datetime_types,
10+
binary_types,
1011
json_types,
1112
text_types,
1213
)
@@ -21,6 +22,7 @@
2122
datetime_types: "datetime.datetime",
2223
json_types: "Json",
2324
text_types: "str",
25+
binary_types: "bytes"
2426
}
2527

2628
types_mapping = populate_types_mapping(mapper)

omymodels/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
"date",
2525
)
2626

27+
binary_types = (
28+
"BINARY",
29+
"VARBINARY",
30+
"binary",
31+
"varbinary",
32+
)
33+
2734
json_types = ("union[dict, list]", "json", "union")
2835

2936
integer_types = ("integer", "int", "serial")

tests/functional/generator/test_pydantic_models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,23 @@ class Material(BaseModel):
177177
updated_at: Optional[datetime.datetime]
178178
"""
179179
assert expected == result
180+
181+
182+
def test_pydantic_with_bytes():
183+
expected = """from pydantic import BaseModel
184+
185+
186+
class User(BaseModel):
187+
188+
avatar: bytes
189+
"""
190+
191+
ddl = """
192+
CREATE TABLE "User" (
193+
"avatar" BINARY NOT NULL
194+
);
195+
"""
196+
result = create_models(ddl, models_type="pydantic")
197+
198+
assert expected == result["code"]
199+

0 commit comments

Comments
 (0)