Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
247 changes: 244 additions & 3 deletions packages/pyright-internal/src/analyzer/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ import {
ParseNodeType,
TernaryNode,
UnaryOperationNode,
isExpressionNode,
} from '../parser/parseNodes';
import { OperatorType } from '../parser/tokenizerTypes';
import { getInfoReader, AnalyzerNodeInfoAccessor } from './analyzerNodeInfo';
import { getEnclosingLambda, isWithinLoop, operatorSupportsChaining, printOperator } from './parseTreeUtils';
import { DeclarationType } from './declaration';
import {
getEnclosingFunction,
getEnclosingLambda,
isWithinLoop,
operatorSupportsChaining,
printOperator,
} from './parseTreeUtils';
import { getScopeForNode } from './scopeUtils';
import { evaluateStaticBoolExpression } from './staticExpressions';
import { Symbol } from './symbol';
import { EvalFlags, MagicMethodDeprecationInfo, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import {
InferenceContext,
MemberAccessFlags,
ClassMember,
convertToInstantiable,
getLiteralTypeClassName,
getTypeCondition,
Expand All @@ -38,6 +49,7 @@ import {
isUnboundedTupleClass,
isUnionable,
lookUpClassMember,
lookUpObjectMember,
makeInferenceContext,
mapSubtypes,
preserveUnknown,
Expand All @@ -49,17 +61,21 @@ import {
} from './typeUtils';
import {
ClassType,
ClassTypeFlags,
NeverType,
OverloadedType,
Type,
TypeBase,
UnknownType,
combineTypes,
isAnyOrUnknown,
isClass,
isClassInstance,
isFunction,
isFunctionOrOverloaded,
isInstantiableClass,
isNever,
isOverloaded,
isUnion,
} from './types';

Expand Down Expand Up @@ -1245,6 +1261,157 @@ function validateContainmentOperation(
return { type, magicMethodDeprecationInfo: deprecatedInfo };
}

function getOperatorType(type: Type): Type {
// Class objects dispatch through their metaclasses.
const metaclass = isInstantiableClass(type) ? type.shared.effectiveMetaclass : undefined;
return metaclass && isInstantiableClass(metaclass) ? ClassType.cloneAsInstance(metaclass) : type;
}

// Python evaluates a binary operation by calling the left operand's magic
// method first, but there is an exception to this rule: if the right operand's
// type is a proper subclass of the left operand's type and it overrides the
// reflected magic method, the reflected method is called first.
function isReflectedOperatorPrioritized(
evaluator: TypeEvaluator,
leftType: Type,
rightType: Type,
operator: OperatorType
): boolean {
Comment thread
rchiodo marked this conversation as resolved.
Resolved
leftType = getOperatorType(leftType);
rightType = getOperatorType(rightType);

if (!isClassInstance(leftType) || !isClassInstance(rightType)) {
return false;
}

// The right operand's class must be a proper subclass of the left operand's class.
if (ClassType.isSameGenericClass(leftType, rightType)) {
return false;
}

const leftClass = ClassType.cloneAsInstantiable(leftType);
if (
!rightType.shared.mro.some((base) => isInstantiableClass(base) && ClassType.isSameGenericClass(base, leftClass))
) {
return false;
}

const [magicMethodName, altMagicMethodName] = binaryOperatorMap[operator];

// The right operand's class must override the reflected magic method.
const rightAltMember = lookUpObjectMember(rightType, altMagicMethodName, MemberAccessFlags.SkipInstanceMembers);
if (!rightAltMember) {
return false;
}

// An intersection's MRO is hypothetical. Require a known subclass to
// provide the reflected implementation rather than an unrelated mix-in.
if (
rightType.shared.flags & ClassTypeFlags.IntersectionClass &&
!rightType.shared.mro.some(
(base) =>
isInstantiableClass(base) &&
!(base.shared.flags & ClassTypeFlags.IntersectionClass) &&
!ClassType.isSameGenericClass(base, leftClass) &&
base.shared.mro.some(
(ancestor) => isInstantiableClass(ancestor) && ClassType.isSameGenericClass(ancestor, leftClass)
) &&
lookUpClassMember(base, altMagicMethodName, MemberAccessFlags.SkipInstanceMembers)?.symbol ===
rightAltMember.symbol
)
) {
return false;
}

const leftAltMember = lookUpObjectMember(leftType, altMagicMethodName, MemberAccessFlags.SkipInstanceMembers);
if (
leftAltMember &&
isClass(leftAltMember.classType) &&
isClass(rightAltMember.classType) &&
ClassType.isSameGenericClass(leftAltMember.classType, rightAltMember.classType)
) {
return false;
}

if (leftAltMember && !operatorSupportsChaining(operator)) {
const leftImplementation = getOperatorImplementation(evaluator, leftAltMember);
const rightImplementation = getOperatorImplementation(evaluator, rightAltMember);
const leftAltType = leftImplementation.type;
const rightAltType = rightImplementation.type;
const leftFunction = isOverloaded(leftAltType) ? OverloadedType.getOverloads(leftAltType)[0] : leftAltType;
const rightFunction = isOverloaded(rightAltType) ? OverloadedType.getOverloads(rightAltType)[0] : rightAltType;

// Rebinding the same function does not provide a different implementation.
// Nested declarations can produce distinct functions on separate calls.
if (
isFunction(leftFunction) &&
isFunction(rightFunction) &&
leftFunction.shared.declaration &&
leftFunction.shared.declaration === rightFunction.shared.declaration &&
(!getEnclosingFunction(leftFunction.shared.declaration.node) ||
leftImplementation.symbol === rightImplementation.symbol)
) {
return false;
}
}

// If the left operand's class doesn't implement the normal form of the
// operator, there's nothing to prioritize over.
if (!lookUpObjectMember(leftType, magicMethodName, MemberAccessFlags.SkipInstanceMembers)) {
return false;
}

return true;
}

function getOperatorImplementation(evaluator: TypeEvaluator, member: ClassMember): { type: Type; symbol: Symbol } {
let symbol = member.symbol;
const visited = new Set<Symbol>();

while (!visited.has(symbol)) {
visited.add(symbol);
const declarations = symbol.getDeclarations();
if (declarations.length !== 1 || declarations[0].type !== DeclarationType.Variable) {
return { type: evaluator.getTypeOfMember({ ...member, symbol }), symbol };
}

const source = declarations[0].inferredTypeSource;
if (!source || !isExpressionNode(source)) {
break;
}

// Inspect alias sources only for dispatch identity. The callable exposed
// to callers must retain its validated, narrowed signature.
const sourceType = evaluator.getTypeOfExpression(source).type;
let sourceSymbol: Symbol | undefined;
if (source.nodeType === ParseNodeType.Name) {
sourceSymbol = evaluator.lookUpSymbolRecursive(source, source.d.value, /* honorCodeFlow */ false)?.symbol;
} else if (source.nodeType === ParseNodeType.MemberAccess) {
const receiverType = evaluator.getTypeOfExpression(source.d.leftExpr).type;
if (isInstantiableClass(receiverType)) {
sourceSymbol = lookUpClassMember(
receiverType,
source.d.member.d.value,
MemberAccessFlags.SkipInstanceMembers
)?.symbol;
}
}

if (!sourceSymbol) {
return { type: sourceType, symbol };
}
symbol = sourceSymbol;
}

return { type: evaluator.getTypeOfMember(member), symbol: member.symbol };
}

function removeNotImplemented(type: Type): Type {
return mapSubtypes(type, (subtype) =>
isClassInstance(subtype) && ClassType.isBuiltIn(subtype, 'types.NotImplementedType') ? undefined : subtype
);
}

function validateArithmeticOperation(
evaluator: TypeEvaluator,
operator: OperatorType,
Expand All @@ -1257,6 +1424,7 @@ function validateArithmeticOperation(
): TypeResult {
let deprecatedInfo: MagicMethodDeprecationInfo | undefined;
const isIncomplete = !!leftTypeResult.isIncomplete || !!rightTypeResult.isIncomplete;
const isArithmetic = !operatorSupportsChaining(operator);

const type = evaluator.mapSubtypesExpandTypeVars(
leftTypeResult.type,
Expand Down Expand Up @@ -1304,7 +1472,35 @@ function validateArithmeticOperation(
}

const magicMethodName = binaryOperatorMap[operator][0];
let resultTypeResult = evaluator.getTypeOfMagicMethodCall(
let resultTypeResult: TypeResult | undefined;
let decliningResult: TypeResult | undefined;
let skipReflectedFallback = false;

// If the right operand's type is a proper subclass of the left
// operand's type and overrides the reflected magic method, Python
// gives the reflected form of the operator priority.
if (
isReflectedOperatorPrioritized(evaluator, leftSubtypeExpanded, rightSubtypeExpanded, operator)
) {
resultTypeResult = evaluator.getTypeOfMagicMethodCall(
convertFunctionToObject(evaluator, rightSubtypeUnexpanded),
binaryOperatorMap[operator][1],
[{ type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }],
errorNode,
inferenceContext
Comment thread
rchiodo marked this conversation as resolved.
Resolved
);

if (resultTypeResult && isArithmetic) {
const resultType = removeNotImplemented(resultTypeResult.type);
if (resultType !== resultTypeResult.type) {
decliningResult = { ...resultTypeResult, type: resultType };
resultTypeResult = undefined;
skipReflectedFallback = true;
}
}
}

resultTypeResult ??= evaluator.getTypeOfMagicMethodCall(
convertFunctionToObject(evaluator, leftSubtypeUnexpanded),
magicMethodName,
[{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }],
Expand Down Expand Up @@ -1334,7 +1530,30 @@ function validateArithmeticOperation(
);
}

if (!resultTypeResult) {
if (resultTypeResult && isArithmetic) {
const resultType = removeNotImplemented(resultTypeResult.type);
if (resultType !== resultTypeResult.type) {
decliningResult = {
...(decliningResult ?? resultTypeResult),
type: combineTypes([decliningResult?.type ?? NeverType.createNever(), resultType]),
isIncomplete: decliningResult?.isIncomplete || resultTypeResult.isIncomplete,
magicMethodDeprecationInfo:
decliningResult?.magicMethodDeprecationInfo ??
resultTypeResult.magicMethodDeprecationInfo,
};
resultTypeResult = undefined;

// Python does not retry the reflected method for operands of the same runtime class.
const leftOperatorType = getOperatorType(leftSubtypeExpanded);
const rightOperatorType = getOperatorType(rightSubtypeExpanded);
skipReflectedFallback ||=
isClassInstance(leftOperatorType) &&
isClassInstance(rightOperatorType) &&
ClassType.isSameGenericClass(leftOperatorType, rightOperatorType);
}
}

if (!resultTypeResult && !skipReflectedFallback) {
// Try the alternate form (swapping right and left).
const altMagicMethodName = binaryOperatorMap[operator][1];
resultTypeResult = evaluator.getTypeOfMagicMethodCall(
Expand Down Expand Up @@ -1371,6 +1590,28 @@ function validateArithmeticOperation(
inferenceContext
);
}
if (resultTypeResult && isArithmetic) {
const resultType = removeNotImplemented(resultTypeResult.type);
if (resultType !== resultTypeResult.type) {
resultTypeResult = isNever(resultType)
? undefined
: { ...resultTypeResult, type: resultType };
}
}
}

if (decliningResult && !isNever(decliningResult.type)) {
resultTypeResult = {
...decliningResult,
type: combineTypes([
decliningResult.type,
resultTypeResult?.type ?? NeverType.createNever(),
]),
isIncomplete: decliningResult.isIncomplete || resultTypeResult?.isIncomplete,
magicMethodDeprecationInfo:
decliningResult.magicMethodDeprecationInfo ??
resultTypeResult?.magicMethodDeprecationInfo,
};
}

if (!resultTypeResult) {
Expand Down
Loading
Loading