Skip to content
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/test-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ jobs:
if ! command -v stack > /dev/null 2>&1; then
curl -sL "https://get.haskellstack.org/" | sh
fi
- name: Install Python 3.7
- name: Install Python 3.9
if: ${{ contains(matrix.fixture, 'python') }}
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.9

- name: Install Python Dependencies
if: ${{ contains(matrix.fixture, 'python') }}
run: |
pip3.7 install mypy python-dateutil types-python-dateutil
pip3.9 install mypy python-dateutil types-python-dateutil
- name: Install flow
if: ${{ contains(matrix.fixture, 'flow') }}
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class JSONPythonRenderer extends PythonRenderer {
", ",
this.typingDecl("x", "Any"),
")",
this.typeHint(" -> ", this.withTyping("List"), "[", tvar, "]"),
this.typeHint(" -> ", this.pyOptions.features.builtinGenerics ? "list" : this.withTyping("List"), "[", tvar, "]"),
":",
],
() => {
Expand Down Expand Up @@ -429,7 +429,7 @@ export class JSONPythonRenderer extends PythonRenderer {
")",
this.typeHint(
" -> ",
this.withTyping("Dict"),
this.pyOptions.features.builtinGenerics ? "dict" : "Dict",
"[str, ",
tvar,
"]",
Expand Down Expand Up @@ -618,9 +618,9 @@ export class JSONPythonRenderer extends PythonRenderer {
(_integerType) => "int",
(_doubleType) => "float",
(_stringType) => "str",
(_arrayType) => "List",
(_arrayType) => this.pyOptions.features.builtinGenerics ? "list" : "List",
(classType) => this.nameForNamedType(classType),
(_mapType) => "dict",
(_mapType) => this.pyOptions.features.builtinGenerics ? "dict" : "Dict",
(enumType) => this.nameForNamedType(enumType),
(_unionType) => undefined,
(transformedStringType) => {
Expand Down
50 changes: 42 additions & 8 deletions packages/quicktype-core/src/language/Python/PythonRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ export class PythonRenderer extends ConvenienceRenderer {
return this.withImport("typing", name);
}

protected namedType(t: Type): Sourcelike {
protected namedType(t: Type, _suppressQuotes: boolean = false): Sourcelike {
const name = this.nameForNamedType(t);
if (this.declaredTypes.has(t)) return name;
if (_suppressQuotes || this.declaredTypes.has(t)) return name;
return ["'", name, "'"];
}

protected pythonType(t: Type, _isRootTypeDef = false): Sourcelike {
protected pythonType(t: Type, _isRootTypeDef = false, _suppressQuotes = false): Sourcelike {
const actualType = followTargetType(t);

return matchType<Sourcelike>(
Expand All @@ -155,25 +155,32 @@ export class PythonRenderer extends ConvenienceRenderer {
(_doubletype) => "float",
(_stringType) => "str",
(arrayType) => [
this.withTyping("List"),
this.pyOptions.features.builtinGenerics ? "list" : this.withTyping("List"),
"[",
this.pythonType(arrayType.items),
"]",
],
(classType) => this.namedType(classType),
(classType) => this.namedType(classType, _suppressQuotes),
(mapType) => [
this.withTyping("Dict"),
this.pyOptions.features.builtinGenerics ? "dict" : this.withTyping("Dict"),
"[str, ",
this.pythonType(mapType.values),
"]",
],
(enumType) => this.namedType(enumType),
(enumType) => this.namedType(enumType, _suppressQuotes),
(unionType) => {
const [hasNull, nonNulls] = removeNullFromUnion(unionType);
const hasClassOrEnumType = Array.from(nonNulls).some(
t => t instanceof ClassType || t instanceof EnumType
);
const encapsulator = hasClassOrEnumType ? "'" : ""

const memberTypes = Array.from(nonNulls).map((m) =>
this.pythonType(m),
// Suppress quotes for namedType
this.pythonType(m, false, true),
);


if (hasNull !== null) {
const rest: string[] = [];
if (
Expand All @@ -189,6 +196,16 @@ export class PythonRenderer extends ConvenienceRenderer {
}

if (nonNulls.size > 1) {
if (this.pyOptions.features.builtinGenerics) {
return [
encapsulator,
arrayIntercalate(" | ", memberTypes),
" | None",
...rest,
encapsulator
];
}

this.withImport("typing", "Union");
return [
this.withTyping("Optional"),
Expand All @@ -199,6 +216,15 @@ export class PythonRenderer extends ConvenienceRenderer {
];
}

if (this.pyOptions.features.builtinGenerics) {
return [
encapsulator,
defined(iterableFirst(memberTypes)),
" | None",
...rest,
encapsulator
];
}
return [
this.withTyping("Optional"),
"[",
Expand All @@ -208,6 +234,14 @@ export class PythonRenderer extends ConvenienceRenderer {
];
}

if (this.pyOptions.features.builtinGenerics) {
return [
encapsulator,
arrayIntercalate(" | ", memberTypes),
encapsulator
];
}

return [
this.withTyping("Union"),
"[",
Expand Down
2 changes: 2 additions & 0 deletions packages/quicktype-core/src/language/Python/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export const forbiddenTypeNames = [
"None",
"Enum",
"List",
"list",
"Dict",
"dict",
"Optional",
"Union",
"Iterable",
Expand Down
9 changes: 5 additions & 4 deletions packages/quicktype-core/src/language/Python/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ export const pythonOptions = {
"python-version",
"Python version",
{
"3.5": { typeHints: false, dataClasses: false },
"3.6": { typeHints: true, dataClasses: false },
"3.7": { typeHints: true, dataClasses: true },
"3.5": { typeHints: false, dataClasses: false, builtinGenerics: false },
"3.6": { typeHints: true, dataClasses: false, builtinGenerics: false },
"3.7": { typeHints: true, dataClasses: true, builtinGenerics: false },
"3.9": { typeHints: true, dataClasses: true, builtinGenerics: true },
},
"3.6",
"3.9",
),
justTypes: new BooleanOption("just-types", "Classes only", false),
nicePropertyNames: new BooleanOption(
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/python/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if [ "x$QUICKTYPE_PYTHON_VERSION" = "x2.7" ] ; then
PYTHON="python2.7"
else
PYTHON="python3.7"
PYTHON="python3.9"
fi

"$PYTHON" "$@"
Expand Down
Loading