Skip to content

Commit

Permalink
Merge pull request #29 from argyle-engineering/fix/parsing-with-model…
Browse files Browse the repository at this point in the history
…-config

Fix parsing with model config
  • Loading branch information
povilasb committed Jun 8, 2023
2 parents 83c7c2a + 2e0f588 commit e600bdd
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 205 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ Transfer your type safe Python data models to TypeScript:
* Customize the output, etc.

![example](example.gif)

## Similar projects

* https://github.com/cs-cordero/py-ts-interfaces
380 changes: 176 additions & 204 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions pydantic2zod/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,17 @@ def __init__(self) -> None:
super().__init__()
self.class_decl = ClassDecl(name="to_be_parsed", base_classes=[])
self._last_field_nr = 0
self._depth = 0

def visit_ClassDef(self, node: cst.ClassDef):
# Guard against nested classes, e.g.
#
# class Model(BaseModel):
# class Config:
self._depth += 1
if self._depth > 1:
return

base_classes = [
b.value.value for b in node.bases if isinstance(b.value, cst.Name)
]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.10"
typing-extensions = "^4.5.0"
typing-extensions = "^4"
libcst = "^0.4.9"
networkx = "^2 || ^3"
typer = ">=0.7.0"
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures/with_model_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseModel


class Foo(BaseModel):
pass


class Bar(BaseModel):
pass


class Model(BaseModel):
x: str | int
y: Foo | Bar

class Config:
smart_union = True
28 changes: 28 additions & 0 deletions tests/snapshots/snap_test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,31 @@
});
export type ModuleType = z.infer<typeof Module>;
'''

snapshots['test_with_pydantic_model_config 1'] = '''
/**
* NOTE: automatically generated by the pydantic2zod compiler.
*/
import { z } from "zod";
export const Foo = z.object({
});
export type FooType = z.infer<typeof Foo>;
export const Bar = z.object({
});
export type BarType = z.infer<typeof Bar>;
export const Model = z.object({
x: z.union([
z.string(),
z.number().int(),
]),
y: z.union([
Foo,
Bar,
]),
});
export type ModelType = z.infer<typeof Model>;
'''
5 changes: 5 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ def test_initializes_empty_lists(snapshot: SnapshotTest):
def test_generic_field_type_is_any_with_no_typevar_bounds(snapshot: SnapshotTest):
out_src = Compiler().parse("tests.fixtures.generic_models").to_zod()
snapshot.assert_match(out_src)


def test_with_pydantic_model_config(snapshot: SnapshotTest):
out_src = Compiler().parse("tests.fixtures.with_model_config").to_zod()
snapshot.assert_match(out_src)

0 comments on commit e600bdd

Please sign in to comment.