Skip to content

Commit

Permalink
fix!: add unique union types for Python (#1982)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored May 7, 2024
1 parent b249824 commit 0d4f8ef
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/migrations/version-3-to-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Console.WriteLine(dateTime2);

Models are aiming to be >= v3.7 compliant.

### Unique types in unions

In v4, unions types are rendered unique, meaning you will never see `str | str` but just `str`.

### Pydantic now follows v2 instead of v1

Reference: https://docs.pydantic.dev/2.6/migration/
Expand Down
3 changes: 2 additions & 1 deletion src/generators/python/PythonConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const PythonDefaultTypeMapping: PythonTypeMapping = {
const unionTypes = constrainedModel.union.map((unionModel) => {
return unionModel.type;
});
return unionTypes.join(' | ');
const uniqueSet = new Set(unionTypes);
return [...uniqueSet].join(' | ');
},
Dictionary({ constrainedModel }): string {
return `dict[${constrainedModel.key.type}, ${constrainedModel.value.type}]`;
Expand Down
25 changes: 24 additions & 1 deletion test/generators/python/PythonConstrainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ describe('PythonConstrainer', () => {
expect(type).toEqual('str');
});
test('should render multiple types', () => {
const unionModel1 = new ConstrainedStringModel(
'test',
undefined,
{},
'str'
);
const unionModel2 = new ConstrainedStringModel(
'test',
undefined,
{},
'str2'
);
const model = new ConstrainedUnionModel('test', undefined, {}, '', [
unionModel1,
unionModel2
]);
const type = PythonDefaultTypeMapping.Union({
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('str | str2');
});
test('should render unique types', () => {
const unionModel1 = new ConstrainedStringModel(
'test',
undefined,
Expand All @@ -219,7 +242,7 @@ describe('PythonConstrainer', () => {
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('str | str');
expect(type).toEqual('str');
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/runtime/runtime-python.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ test("Python runtime testing", async () => {

const compileCommand = `cd ${path.resolve(
__dirname,
"./runtime-python/test/"
)} && python3 main.py`;
"./runtime-python"
)} && python3 -m unittest discover ./tests`;
await execCommand(compileCommand,true);

});
7 changes: 7 additions & 0 deletions test/runtime/runtime-python/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
import sys
PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH, "src"
)
sys.path.append(SOURCE_PATH)
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest
import sys
sys.path.insert(1, '../src/main/')
from Address import Address
from NestedObject import NestedObject
from src.main.Address import Address
from src.main.NestedObject import NestedObject
import json

class TestAddress(unittest.TestCase):
Expand Down

0 comments on commit 0d4f8ef

Please sign in to comment.