Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
118 changes: 0 additions & 118 deletions python/copilot/generated/rpc.py

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

11 changes: 11 additions & 0 deletions python/test_codegen_type_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from copilot.generated import rpc


def test_python_codegen_does_not_export_quicktype_synthetic_permission_approval_name():
assert not hasattr(rpc, "PermissionDecisionApproveForIonApproval")
assert "PermissionDecisionApproveForIonApproval" not in rpc.__all__

assert hasattr(rpc, "PermissionDecisionApproveForSessionApproval")
assert hasattr(rpc, "PermissionDecisionApproveForLocationApproval")
assert "PermissionDecisionApproveForSessionApproval" in rpc.__all__
assert "PermissionDecisionApproveForLocationApproval" in rpc.__all__
32 changes: 32 additions & 0 deletions scripts/codegen/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,34 @@ function collapsePlaceholderPythonDataclasses(code: string, knownDefinitionNames
return code.replace(/\n{3,}/g, "\n\n");
}

function removeUnusedSyntheticPythonDataclasses(code: string, knownDefinitionNames: Set<string>): string {
let changed = true;

while (changed) {
changed = false;
const classBlockRe =
/((?:^# (?:Experimental|Deprecated|Internal):[^\n]*\r?\n)*@dataclass\r?\nclass\s+(\w+):[\s\S]*?)(?=^(?:# (?:Experimental|Deprecated|Internal):[^\n]*\r?\n)*@dataclass|^class\s+\w|^def\s+\w|^[A-Z]\w+\s*=|\Z)/gm;
Comment thread
abhinavgautam01 marked this conversation as resolved.
Outdated
const matches = [...code.matchAll(classBlockRe)];
Comment thread
abhinavgautam01 marked this conversation as resolved.
Outdated

for (const match of matches) {
const fullBlock = match[1];
const className = match[2];
if (knownDefinitionNames.has(className.toLowerCase())) continue;

const before = code.slice(0, match.index);
const after = code.slice((match.index ?? 0) + fullBlock.length);
const referenceRe = new RegExp(`\\b${escapeRegExp(className)}\\b`);
if (referenceRe.test(before) || referenceRe.test(after)) continue;

code = `${before}${after}`;
changed = true;
break;
}
}

return code;
Comment thread
abhinavgautam01 marked this conversation as resolved.
Outdated
}

/**
* Reorder Python class/enum definitions so forward references are resolved.
* Quicktype may emit classes in an order where a class references another
Expand Down Expand Up @@ -3208,6 +3236,10 @@ def _patch_model_capabilities(data: dict) -> dict:
finalCode = applyUnionRewritesToPython(finalCode, refBasedUnions);
finalCode = postProcessDiscriminatorDefaultsForPython(finalCode, refBasedUnions);
finalCode = unwrapRedundantPythonLambdas(finalCode);
finalCode = removeUnusedSyntheticPythonDataclasses(
finalCode,
new Set(Object.keys(allDefinitions).map((name) => name.toLowerCase()))
);

// Apply `_`-prefix to type names of internal RPC types so the leading-underscore
// Python convention signals "internal, no stability guarantees" to consumers.
Expand Down