From 3e89e187fa7ebe44ed4ac94f35dc5ec33461a112 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 6 Dec 2026 08:11:20 +0800 Subject: [PATCH] refactor(python): reuse marshal container classification (#17311) --- src/python/PythonPycParser.ts | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/python/PythonPycParser.ts b/src/python/PythonPycParser.ts index 577302b11b5..f866f08641c 100644 --- a/src/python/PythonPycParser.ts +++ b/src/python/PythonPycParser.ts @@ -92,7 +92,9 @@ type KeyFrame = { failed: boolean; }; -const isContainer = (value: MarshalValue): boolean => +type MarshalContainer = Extract; + +const isContainer = (value: MarshalValue): value is MarshalContainer => value.kind === "tuple" || value.kind === "list" || value.kind === "set" || @@ -100,21 +102,13 @@ const isContainer = (value: MarshalValue): boolean => value.kind === "dict" || value.kind === "slice"; -const containerChildren = (value: MarshalValue): readonly MarshalValue[] => { - if (value.kind === "dict") { - const children: MarshalValue[] = []; - for (const [key, mapped] of value.items) children.push(key, mapped); - return children; - } - if ( - value.kind === "tuple" || - value.kind === "list" || - value.kind === "set" || - value.kind === "frozenset" || - value.kind === "slice" - ) - return value.items; - return []; +const containerChildren = ( + value: MarshalContainer, +): readonly MarshalValue[] => { + if (value.kind !== "dict") return value.items; + const children: MarshalValue[] = []; + for (const [key, mapped] of value.items) children.push(key, mapped); + return children; }; /** @@ -745,7 +739,7 @@ class PycMarshalReader extends MarshalReader { * any cyclic container is cached; the caller then scans that component once. */ #memoizeNested( - root: MarshalValue, + root: MarshalContainer, added: { value: MarshalValue; retained: number }[], ): boolean { const gray = new Set(); @@ -866,7 +860,7 @@ class PycMarshalReader extends MarshalReader { this.work.nestedSteps += 1; } - #children(value: MarshalValue): readonly MarshalValue[] { + #children(value: MarshalContainer): readonly MarshalValue[] { const children = containerChildren(value); this.work.listedChildren += children.length; return children;