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
19 changes: 15 additions & 4 deletions packages/quicktype-core/src/language/Python/PythonRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import { matchType, removeNullFromUnion } from "../../Type/TypeUtils.js";

import { forbiddenPropertyNames, forbiddenTypeNames } from "./constants.js";
import type { pythonOptions } from "./language.js";
import { classNameStyle, snakeNameStyle } from "./utils.js";
import {
classNameStyle,
isLegalPythonIdentifier,
snakeNameStyle,
} from "./utils.js";

export class PythonRenderer extends ConvenienceRenderer {
private readonly imports: Map<string, Set<string>> = new Map();
Expand Down Expand Up @@ -68,9 +72,16 @@ export class PythonRenderer extends ConvenienceRenderer {
}

protected namerForObjectProperty(): Namer {
return funPrefixNamer("property", (s) =>
snakeNameStyle(s, false, this.pyOptions.nicePropertyNames),
);
return funPrefixNamer("property", (s) => {
if (
this.pyOptions.keepPropertyNames &&
isLegalPythonIdentifier(s)
) {
return s;
}

return snakeNameStyle(s, false, this.pyOptions.nicePropertyNames);
});
}

protected makeUnionMemberNamer(): null {
Expand Down
5 changes: 5 additions & 0 deletions packages/quicktype-core/src/language/Python/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export const pythonOptions = {
"Transform property names to be Pythonic",
true,
),
keepPropertyNames: new BooleanOption(
"keep-property-names",
"Keep original property names when they are valid Python identifiers",
false,
),
pydanticBaseModel: new BooleanOption(
"pydantic-base-model",
"Uses pydantic BaseModel",
Expand Down
26 changes: 26 additions & 0 deletions packages/quicktype-core/src/language/Python/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ function isPartCharacter3(utf16Unit: number): boolean {
return true;
}

function isPythonIdentifierStart(utf16Unit: number): boolean {
return utf16Unit === 0x5f || isNormalizedStartCharacter3(utf16Unit);
}

function isPythonIdentifierPart(utf16Unit: number): boolean {
return (
isPythonIdentifierStart(utf16Unit) ||
isNormalizedPartCharacter3(utf16Unit)
);
}

export function isLegalPythonIdentifier(original: string): boolean {
if (
original.length === 0 ||
!isPythonIdentifierStart(original.charCodeAt(0))
) {
return false;
}

for (let i = 1; i < original.length; i++) {
if (!isPythonIdentifierPart(original.charCodeAt(i))) return false;
}

return true;
}

const legalizeName3 = utf16LegalizeCharacters(isPartCharacter3);

export function classNameStyle(original: string): string {
Expand Down
48 changes: 48 additions & 0 deletions test/unit/python-property-names.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, test } from "vitest";

import {
InputData,
JSONSchemaInput,
quicktype,
} from "../../packages/quicktype-core/src/index.js";

const schema = JSON.stringify({
type: "object",
properties: {
source_m3u8: { type: "string" },
"has-dash": { type: "string" },
},
});

async function pythonFor(keepPropertyNames: boolean): Promise<string> {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({ name: "TopLevel", schema });

const inputData = new InputData();
inputData.addInput(schemaInput);

const result = await quicktype({
inputData,
lang: "python",
rendererOptions: {
"keep-property-names": keepPropertyNames,
},
});

return result.lines.join("\n");
}

test("Python keeps the default naming behavior", async () => {
const output = await pythonFor(false);

expect(output).toContain("source_m3_u8: str");
expect(output).toContain("has_dash: str");
});

test("Python can keep valid original property names", async () => {
const output = await pythonFor(true);

expect(output).toContain("source_m3u8: str");
expect(output).toContain("has_dash: str");
expect(output).toContain('obj.get("source_m3u8")');
});
Loading