Skip to content

Commit c10b4e2

Browse files
committed
Fix enums from absolute imports
1 parent a29816f commit c10b4e2

File tree

7 files changed

+72
-14
lines changed

7 files changed

+72
-14
lines changed

pydantic2ts/cli/script.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,16 @@ def extract_pydantic_models(module: ModuleType) -> List[Type[BaseModel]]:
118118
return models
119119

120120

121-
def extract_enum_models(module: ModuleType) -> List[Type[Enum]]:
121+
def extract_enum_models(models: List[Type[BaseModel]]) -> List[Type[Enum]]:
122122
"""
123-
Given a module, return a list of the Enum classes contained within it.
123+
Given a list of pydantic models, return a list of the Enum classes used as fields within those models.
124124
"""
125125
enums = []
126-
module_name = module.__name__
127-
128-
for _, enum in inspect.getmembers(module, is_enum):
129-
if enum.__module__ != "enum":
130-
enums.append(enum)
131126

132-
for _, submodule in inspect.getmembers(
133-
module, lambda obj: is_submodule(obj, module_name)
134-
):
135-
enums.extend(extract_enum_models(submodule))
127+
for model in models:
128+
for field in model.__fields__.values():
129+
if isinstance(field.type_, type) and issubclass(field.type_, Enum):
130+
enums.append(field.type_)
136131

137132
return enums
138133

@@ -315,8 +310,7 @@ def generate_typescript_defs(
315310

316311
logger.info("Finding pydantic models...")
317312

318-
import_result = import_module(module)
319-
models = extract_pydantic_models(import_result)
313+
models = extract_pydantic_models(import_module(module))
320314

321315
if exclude:
322316
models = [m for m in models if m.__name__ not in exclude]
@@ -326,7 +320,7 @@ def generate_typescript_defs(
326320
if V2:
327321
schema = generate_json_schema_v2(models)
328322
else:
329-
enums = extract_enum_models(import_result)
323+
enums = extract_enum_models(models)
330324
schema = generate_json_schema_v1(models, enums)
331325

332326
schema_dir = mkdtemp()

tests/expected_results/enums/v1/input.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import os
2+
import sys
13
from enum import Enum
24
from typing import List, Optional
35

46
from pydantic import BaseModel
57

8+
# Make absolute imports work
9+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__))))
10+
611
from .schemas.schema_one import ModelOne # noqa: F401
712
from .schemas.schema_two import ModelTwo # noqa: F401
13+
from schemas.sub_model import SubModel # this tests absolute imports
814

915

1016
class Cat(BaseModel):
@@ -29,3 +35,7 @@ class AnimalShelter(BaseModel):
2935
class Standalone(Enum):
3036
something = "something"
3137
anything = "anything"
38+
39+
40+
class ImportedSubModule(BaseModel):
41+
sub: SubModel

tests/expected_results/enums/v1/output.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@ export interface Dog {
2121
name: string;
2222
age: number;
2323
}
24+
export interface ImportedSubModule {
25+
sub: SubModel;
26+
}
27+
export interface SubModel {
28+
bar: Bar;
29+
}
2430
export interface ModelOne {
2531
foo: Foo;
2632
}
2733
export interface ModelTwo {
2834
foo: Foo1;
2935
}
3036

37+
export const enum Bar {
38+
ONE = "one",
39+
TWO = "two"
40+
}
3141
export const enum Foo {
3242
ONE_A = "one_a",
3343
ONE_B = "one_b"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import enum
2+
3+
from pydantic import BaseModel
4+
5+
6+
class Bar(enum.Enum):
7+
ONE = "one"
8+
TWO = "two"
9+
10+
11+
class SubModel(BaseModel):
12+
bar: Bar

tests/expected_results/enums/v2/input.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import os
2+
import sys
13
from enum import Enum
24
from typing import List, Optional
35

46
from pydantic import BaseModel
57

8+
# Make absolute imports work
9+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__))))
10+
611
from .schemas.schema_one import ModelOne # noqa: F401
712
from .schemas.schema_two import ModelTwo # noqa: F401
13+
from schemas.sub_model import SubModel # this tests absolute imports
814

915

1016
class Cat(BaseModel):
@@ -29,3 +35,7 @@ class AnimalShelter(BaseModel):
2935
class Standalone(Enum):
3036
something = "something"
3137
anything = "anything"
38+
39+
40+
class ImportedSubModule(BaseModel):
41+
sub: SubModel

tests/expected_results/enums/v2/output.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@ export interface Dog {
2121
name: string;
2222
age: number;
2323
}
24+
export interface ImportedSubModule {
25+
sub: SubModel;
26+
}
27+
export interface SubModel {
28+
bar: Bar;
29+
}
2430
export interface ModelOne {
2531
foo: Foo;
2632
}
2733
export interface ModelTwo {
2834
foo: Foo1;
2935
}
3036

37+
export const enum Bar {
38+
ONE = "one",
39+
TWO = "two"
40+
}
3141
export const enum Foo {
3242
ONE_A = "one_a",
3343
ONE_B = "one_b"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import enum
2+
3+
from pydantic import BaseModel
4+
5+
6+
class Bar(enum.Enum):
7+
ONE = "one"
8+
TWO = "two"
9+
10+
11+
class SubModel(BaseModel):
12+
bar: Bar

0 commit comments

Comments
 (0)