MCPcopy Index your code
hub / github.com/github/copilot-sdk / postProcessDiscriminatorDefaultsForPython

Function postProcessDiscriminatorDefaultsForPython

scripts/codegen/python.ts:498–648  ·  view source on GitHub ↗

* For each discriminated-union variant class, replace the dataclass-level * discriminator field (e.g. ``kind: PermissionDecisionApproveOnceKind``) with * a class-level constant (e.g. ``kind: ClassVar[str] = "approve-once"``). * This lets users construct variants without supplying the discriminato

(
    code: string,
    unions: ResolvedRefBasedUnion[]
)

Source from the content-addressed store, hash-verified

496 * and ``to_dict`` to emit the constant directly.
497 */
498function postProcessDiscriminatorDefaultsForPython(
499 code: string,
500 unions: ResolvedRefBasedUnion[]
501): string {
502 // Build variant lookup: variant class name → { prop, value }.
503 const variantInfo = new Map<string, { prop: string; value: string }>();
504 for (const union of unions) {
505 for (const d of union.dispatch) {
506 // First-wins; multiple unions referencing the same variant share a
507 // discriminator/value pair anyway.
508 if (!variantInfo.has(d.typeName)) {
509 variantInfo.set(d.typeName, { prop: union.discriminatorProp, value: d.value });
510 }
511 }
512 }
513 if (variantInfo.size === 0) return code;
514
515 const lines = code.split("\n");
516 const out: string[] = [];
517 let usedClassVar = false;
518
519 let i = 0;
520 while (i < lines.length) {
521 const line = lines[i];
522 const classMatch = line.match(/^class (\w+)[:\(]/);
523 if (!classMatch) {
524 out.push(line);
525 i++;
526 continue;
527 }
528 const className = classMatch[1];
529 const info = variantInfo.get(className);
530 if (!info) {
531 out.push(line);
532 i++;
533 continue;
534 }
535
536 // Find the bounds of this class block: everything indented under it.
537 const classStart = i;
538 let classEnd = i + 1;
539 while (classEnd < lines.length) {
540 const ln = lines[classEnd];
541 if (
542 /^class \w/.test(ln) ||
543 /^def \w/.test(ln) ||
544 ln === "@dataclass" ||
545 /^# (?:Experimental|Deprecated|Internal):/.test(ln) ||
546 ln.startsWith("@dataclass(")
547 ) {
548 break;
549 }
550 classEnd++;
551 }
552 const block = lines.slice(classStart, classEnd);
553
554 // Locate the discriminator field declaration. Quicktype emits
555 // ` kind: PermissionDecisionApproveOnceKind` while the

Callers 2

generateRpcFunction · 0.85

Calls 6

splitTopLevelCommasMultiFunction · 0.85
ensureClassVarImportFunction · 0.85
joinMethod · 0.80
setMethod · 0.45
pushMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…