Skip to content

Commit

Permalink
Did some code cleanup with unpacked tuples and TypeVarTuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
erictraut committed Aug 11, 2024
1 parent 2c9c424 commit aff4bfb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/analyzer/constraintSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function assignTypeVar(
tupleClassType,
[{ type: srcType, isUnbounded: false }],
/* isTypeArgExplicit */ true,
/* isUnpackedTuple */ true
/* isUnpacked */ true
)
);
}
Expand Down Expand Up @@ -1351,7 +1351,7 @@ function stripLiteralValueForUnpackedTuple(evaluator: TypeEvaluator, type: Type)
return type;
}

return specializeTupleClass(type, tupleTypeArgs, /* isTypeArgExplicit */ true, /* isUnpackedTuple */ true);
return specializeTupleClass(type, tupleTypeArgs, /* isTypeArgExplicit */ true, /* isUnpacked */ true);
}

// This function is used for debugging only. It dumps the current contents of
Expand Down
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/analyzer/tuples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function adjustTupleTypeArgs(
};
}),
/* isTypeArgExplicit */ true,
/* isUnpackedTuple */ true
/* isUnpacked */ true
)
);

Expand Down Expand Up @@ -245,7 +245,7 @@ export function adjustTupleTypeArgs(
};
}),
/* isTypeArgExplicit */ true,
/* isUnpackedTuple */ true
/* isUnpacked */ true
)
);
}
Expand All @@ -271,7 +271,7 @@ export function adjustTupleTypeArgs(
srcUnboundedIndex < destUnboundedOrVariadicIndex + srcArgsToCapture)
) {
const removedArgTypes = srcTypeArgs.splice(destUnboundedOrVariadicIndex, srcArgsToCapture).map((t) => {
if (isTypeVar(t.type) && isUnpackedTypeVarTuple(t.type) && !t.type.priv.isInUnion) {
if (isTypeVar(t.type) && isUnpackedTypeVarTuple(t.type)) {
return TypeVarType.cloneForUnpacked(t.type, /* isInUnion */ true);
}
return t.type;
Expand Down
24 changes: 12 additions & 12 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5011,7 +5011,7 @@ export function createTypeEvaluator(
} else if (isTypeVarTuple(param) && tupleClass && isInstantiableClass(tupleClass)) {
defaultType = makeTupleObject(
[{ type: UnknownType.create(), isUnbounded: true }],
/* isUnpackedTuple */ true
/* isUnpacked */ true
);
} else {
defaultType = UnknownType.create();
Expand Down Expand Up @@ -6751,7 +6751,7 @@ export function createTypeEvaluator(
});
}

const tupleObject = makeTupleObject(variadicTypes, /* isUnpackedTuple */ true);
const tupleObject = makeTupleObject(variadicTypes, /* isUnpacked */ true);

typeArgs = [
...typeArgs.slice(0, variadicIndex),
Expand All @@ -6763,7 +6763,7 @@ export function createTypeEvaluator(
// Add an empty tuple that maps to the TypeVarTuple type parameter.
typeArgs.push({
node: errorNode,
type: makeTupleObject([], /* isUnpackedTuple */ true),
type: makeTupleObject([], /* isUnpacked */ true),
});
}
}
Expand Down Expand Up @@ -7419,10 +7419,10 @@ export function createTypeEvaluator(
});
}

function makeTupleObject(typeArgs: TupleTypeArg[], isUnpackedTuple = false) {
function makeTupleObject(typeArgs: TupleTypeArg[], isUnpacked = false) {
if (tupleClass && isInstantiableClass(tupleClass)) {
return convertToInstance(
specializeTupleClass(tupleClass, typeArgs, /* isTypeArgExplicit */ true, isUnpackedTuple)
specializeTupleClass(tupleClass, typeArgs, /* isTypeArgExplicit */ true, isUnpacked)
);
}

Expand Down Expand Up @@ -11182,7 +11182,7 @@ export function createTypeEvaluator(
// simplify the type.
specializedTuple = tupleTypeArgs[0].type;
} else {
specializedTuple = makeTupleObject(tupleTypeArgs, /* isUnpackedTuple */ true);
specializedTuple = makeTupleObject(tupleTypeArgs, /* isUnpacked */ true);
}

const combinedArg: ValidateArgTypeParams = {
Expand Down Expand Up @@ -11653,7 +11653,7 @@ export function createTypeEvaluator(

// If the final return type is an unpacked tuple, turn it into a normal (unpacked) tuple.
if (isUnpackedClass(specializedReturnType)) {
specializedReturnType = ClassType.cloneForUnpacked(specializedReturnType, /* isUnpackedTuple */ false);
specializedReturnType = ClassType.cloneForUnpacked(specializedReturnType, /* isUnpacked */ false);
}

const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(errorNode);
Expand Down Expand Up @@ -18434,7 +18434,7 @@ export function createTypeEvaluator(
}

if (isUnpackedClass(type)) {
return ClassType.cloneForUnpacked(type, /* isUnpackedTuple */ false);
return ClassType.cloneForUnpacked(type, /* isUnpacked */ false);
}

return makeTupleObject([{ type, isUnbounded: !isTypeVarTuple(type) }]);
Expand Down Expand Up @@ -23055,7 +23055,7 @@ export function createTypeEvaluator(

if (curSrcType.priv.tupleTypeArgs) {
typeArgType = convertToInstance(
makeTupleObject(curSrcType.priv.tupleTypeArgs, /* isUnpackedTuple */ true)
makeTupleObject(curSrcType.priv.tupleTypeArgs, /* isUnpacked */ true)
);
} else {
typeArgType = i < srcTypeArgs.length ? srcTypeArgs[i] : UnknownType.create();
Expand Down Expand Up @@ -24812,7 +24812,7 @@ export function createTypeEvaluator(
});

if (srcTupleTypes.length !== 1 || !isTypeVarTuple(srcTupleTypes[0].type)) {
const srcPositionalsType = makeTupleObject(srcTupleTypes, /* isUnpackedTuple */ true);
const srcPositionalsType = makeTupleObject(srcTupleTypes, /* isUnpacked */ true);

// Snip out the portion of the source positionals that map to the variadic
// dest parameter and replace it with a single parameter that is typed as a
Expand Down Expand Up @@ -25179,11 +25179,11 @@ export function createTypeEvaluator(
let srcArgsType = srcParamDetails.params[srcParamDetails.argsIndex].type;

if (!isUnpacked(destArgsType)) {
destArgsType = makeTupleObject([{ type: destArgsType, isUnbounded: true }], /* isUnpackedTuple */ true);
destArgsType = makeTupleObject([{ type: destArgsType, isUnbounded: true }], /* isUnpacked */ true);
}

if (!isUnpacked(srcArgsType)) {
srcArgsType = makeTupleObject([{ type: srcArgsType, isUnbounded: true }], /* isUnpackedTuple */ true);
srcArgsType = makeTupleObject([{ type: srcArgsType, isUnbounded: true }], /* isUnpacked */ true);
}

if (
Expand Down
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ export function getUnknownForTypeVarTuple(tupleClassType: ClassType): Type {
tupleClassType,
[{ type: UnknownType.create(), isUnbounded: true }],
/* isTypeArgExplicit */ true,
/* isUnpackedTuple */ true
/* isUnpacked */ true
)
);
}
Expand Down Expand Up @@ -2035,7 +2035,7 @@ export function buildSolutionFromSpecializedClass(classType: ClassType): Constra
classType,
classType.priv.tupleTypeArgs,
classType.priv.isTypeArgExplicit,
/* isUnpackedTuple */ true
/* isUnpacked */ true
)
),
];
Expand Down Expand Up @@ -2661,7 +2661,7 @@ export function specializeTupleClass(
classType: ClassType,
typeArgs: TupleTypeArg[],
isTypeArgExplicit = true,
isUnpackedTuple = false
isUnpacked = false
): ClassType {
const clonedClassType = ClassType.specialize(
classType,
Expand All @@ -2671,7 +2671,7 @@ export function specializeTupleClass(
typeArgs
);

if (isUnpackedTuple) {
if (isUnpacked) {
clonedClassType.priv.isUnpacked = true;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3250,6 +3250,10 @@ export function isTypeSame(type1: Type, type2: Type, options: TypeSameOptions =
return false;
}

if (!type1.priv.isUnpacked !== !classType2.priv.isUnpacked) {
return false;
}

if (!type1.priv.isTypedDictPartial !== !classType2.priv.isTypedDictPartial) {
return false;
}
Expand Down

0 comments on commit aff4bfb

Please sign in to comment.