refactor(python): reuse marshal container classification (#17311)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-06 08:11:20 +08:00
1 parent 4cb19d6ede
commit 3e89e187fa
1 file changed
+12 -18
+12 -18
View File
@@ -92,7 +92,9 @@ type KeyFrame = {
failed: boolean;
};
const isContainer = (value: MarshalValue): boolean =>
type MarshalContainer = Extract<MarshalValue, { readonly items: unknown }>;
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<MarshalValue> {
* 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<MarshalValue>();
@@ -866,7 +860,7 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
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;