Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"@types/eslint": "^8.21.3",
"@types/node": "^18.15.11",
"@types/prettier": "^2.7.2",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/languages/python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,9 @@ describe('generatePythonStruct', () => {
'from typing import List\n\n\nGeneratedStruct: List[int]\n'
));

// NOTE: should this be switched to Array<unknown>?
it('null array', () =>
it('null arrays should return Any', () =>
expect(generatePythonStruct(tokenize([null]))).toEqual(
'from typing import List\n\n\nGeneratedStruct: List[None]\n'
'from typing import Any, List\n\n\nGeneratedStruct: List[Any]\n'
));

it('empty matrix', () =>
Expand All @@ -260,11 +259,12 @@ describe('generatePythonStruct', () => {
expect(generatePythonStruct(tokenize([1, 'mhouge.dk']))).toEqual(
'from typing import List, Union\n\n\nGeneratedStruct: List[Union[int, str]]\n'
);
});

it('arrays with multiple values and null should return optional', () =>
expect(generatePythonStruct(tokenize([1, 'mhouge.dk', null]))).toEqual(
'from typing import List, Union\n\n\nGeneratedStruct: List[Union[None, int, str]]\n'
);
});
'from typing import List, Optional, Union\n\n\nGeneratedStruct: List[Optional[Union[int, str]]]\n'
));
});

describe('objects', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ function convertArray(token: ArrayToken, imports: Set<string>, subStructs: Map<s
children.add(convertTokenToPython(token.children[i], imports, subStructs));
}

const optional = children.has('None');

if (optional) {
children.delete('None');

if (children.size) imports.add('Optional');
}

const childTypesArr = Array.from(children);

if (childTypesArr.length === 0) {
imports.add('Any');
return 'List[Any]';
}

if (childTypesArr.length === 1) return `List[${childTypesArr[0]}]`;

childTypesArr.sort();

imports.add('Union');

if (optional) return `List[Optional[Union[${childTypesArr.join(', ')}]]]`;

return `List[Union[${childTypesArr.join(', ')}]]`;
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"types": ["node"],
"declaration": true,
"declarationMap": true,
"lib": ["es6", "es2015", "dom"],
"lib": ["es6", "es2015"],
"resolveJsonModule": true
},
"include": ["src/"],
Expand Down