Skip to content

Commit 8caba44

Browse files
committed
Improve handling of enums with same name
1 parent cd41afe commit 8caba44

File tree

15 files changed

+94
-4
lines changed

15 files changed

+94
-4
lines changed

pydantic2ts/cli/script.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,15 @@ def add_ts_enum_names(schema: Dict[str, Any], enum_class: Type[Enum]) -> None:
163163
schema["tsEnumNames"] = [name for name, member in enum_class.__members__.items()]
164164

165165

166-
def is_matching_enum(prop_type: Any, schema_title: str) -> bool:
166+
def is_matching_enum(prop_type: Any, schema_title: str, schema_enum: list[str]) -> bool:
167167
return (
168168
isclass(prop_type)
169169
and issubclass(prop_type, Enum)
170170
and prop_type.__name__ == schema_title
171+
and all(
172+
value in (member.value for member in prop_type.__members__.values())
173+
for value in schema_enum
174+
)
171175
)
172176

173177

@@ -183,17 +187,19 @@ def extend_enum_definitions(
183187
for model in models:
184188
for prop, prop_type in model.__annotations__.items():
185189
origin = get_origin(prop_type)
186-
if is_matching_enum(prop_type, schema["title"]):
190+
if is_matching_enum(prop_type, schema["title"], schema["enum"]):
187191
add_ts_enum_names(schema, prop_type)
188192
break
189193
elif origin is list:
190194
inner_type = get_args(prop_type)[0]
191-
if is_matching_enum(inner_type, schema["title"]):
195+
if is_matching_enum(inner_type, schema["title"], schema["enum"]):
192196
add_ts_enum_names(schema, inner_type)
193197
break
194198
elif (UnionType and origin is UnionType) or origin is Union:
195199
for inner_type in get_args(prop_type):
196-
if is_matching_enum(inner_type, schema["title"]):
200+
if is_matching_enum(
201+
inner_type, schema["title"], schema["enum"]
202+
):
197203
add_ts_enum_names(schema, inner_type)
198204
break
199205

tests/expected_results/__init__.py

Whitespace-only changes.

tests/expected_results/enums/__init__.py

Whitespace-only changes.

tests/expected_results/enums/v1/__init__.py

Whitespace-only changes.

tests/expected_results/enums/v1/input.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from pydantic import BaseModel
44

5+
from .schemas.schema_one import ModelOne # noqa: F401
6+
from .schemas.schema_two import ModelTwo # noqa: F401
7+
58

69
class Cat(BaseModel):
710
name: str

tests/expected_results/enums/v1/output.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ export interface Dog {
2121
name: string;
2222
age: number;
2323
}
24+
export interface ModelOne {
25+
foo: Foo;
26+
}
27+
export interface ModelTwo {
28+
foo: Foo1;
29+
}
30+
31+
export const enum Foo {
32+
ONE_A = "one_a",
33+
ONE_B = "one_b"
34+
}
35+
export const enum Foo1 {
36+
TWO_A = "two_a",
37+
TWO_B = "two_b"
38+
}

tests/expected_results/enums/v1/schemas/__init__.py

Whitespace-only changes.
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 Foo(enum.Enum):
7+
ONE_A = "one_a"
8+
ONE_B = "one_b"
9+
10+
11+
class ModelOne(BaseModel):
12+
foo: Foo
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 Foo(enum.Enum):
7+
TWO_A = "two_a"
8+
TWO_B = "two_b"
9+
10+
11+
class ModelTwo(BaseModel):
12+
foo: Foo

tests/expected_results/enums/v2/__init__.py

Whitespace-only changes.

tests/expected_results/enums/v2/input.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from pydantic import BaseModel
44

5+
from .schemas.schema_one import ModelOne # noqa: F401
6+
from .schemas.schema_two import ModelTwo # noqa: F401
7+
58

69
class Cat(BaseModel):
710
name: str

tests/expected_results/enums/v2/output.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ export interface Dog {
2121
name: string;
2222
age: number;
2323
}
24+
export interface ModelOne {
25+
foo: Foo;
26+
}
27+
export interface ModelTwo {
28+
foo: Foo1;
29+
}
30+
31+
export const enum Foo {
32+
ONE_A = "one_a",
33+
ONE_B = "one_b"
34+
}
35+
export const enum Foo1 {
36+
TWO_A = "two_a",
37+
TWO_B = "two_b"
38+
}

tests/expected_results/enums/v2/schemas/__init__.py

Whitespace-only changes.
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 Foo(enum.Enum):
7+
ONE_A = "one_a"
8+
ONE_B = "one_b"
9+
10+
11+
class ModelOne(BaseModel):
12+
foo: Foo
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 Foo(enum.Enum):
7+
TWO_A = "two_a"
8+
TWO_B = "two_b"
9+
10+
11+
class ModelTwo(BaseModel):
12+
foo: Foo

0 commit comments

Comments
 (0)