diff --git a/docs/type-evaluator-architecture.md b/docs/type-evaluator-architecture.md index f9fb70101d86..80f69d3c4588 100644 --- a/docs/type-evaluator-architecture.md +++ b/docs/type-evaluator-architecture.md @@ -126,6 +126,11 @@ and type guard tests using those primitives. Scalar string evaluation and forward-annotation parsing belong to `expressionEvaluation.ts`, while callable parameter packing is private to `typeCompatibility.ts`. +IntFlag callable return refinement runs in `typeCompatibility.ts` after parameter +compatibility succeeds. The main evaluator retains validated call-argument +refinement and correlated packed-argument expansion; its original argument list +is carried in the private `MatchArgsToParamsResult` contract in `internals.ts`. + ## Where to start This is a navigation map, not an acyclic dependency graph. Start in diff --git a/packages/pyright-internal/src/analyzer/enums.ts b/packages/pyright-internal/src/analyzer/enums.ts index 8f8e58adfed1..7b9ec0180c53 100644 --- a/packages/pyright-internal/src/analyzer/enums.ts +++ b/packages/pyright-internal/src/analyzer/enums.ts @@ -10,30 +10,44 @@ import { assert } from '../common/debug'; import { PythonVersion, pythonVersion3_13 } from '../common/pythonVersion'; import { ArgCategory, ExpressionNode, NameNode, ParseNode, ParseNodeType } from '../parser/parseNodes'; -import { AnalyzerNodeInfoAccessor, AnalyzerNodeInfoReader, getFileInfo } from './analyzerNodeInfo'; +import { OperatorType } from '../parser/tokenizerTypes'; +import { AnalyzerNodeInfoAccessor, AnalyzerNodeInfoReader, getFileInfo, getInfoReader } from './analyzerNodeInfo'; import { VariableDeclaration } from './declaration'; +import { ParamKind, ParamListDetails } from './parameterUtils'; import { getClassFullName, getEnclosingClass, getTypeSourceId } from './parseTreeUtils'; import { Symbol, SymbolFlags } from './symbol'; import { isPrivateName, isSingleDunderName } from './symbolNameUtils'; -import { Arg, EvalFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; +import { Arg, EvalFlags, FunctionResult, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { enumerateLiteralsForType } from './typeGuards'; -import { MemberAccessFlags, computeMroLinearization, lookUpClassMember, makeInferenceContext } from './typeUtils'; +import { + MemberAccessFlags, + computeMroLinearization, + isNoneInstance, + lookUpClassMember, + makeInferenceContext, + mapSubtypes, +} from './typeUtils'; import { AnyType, ClassType, ClassTypeFlags, EnumLiteral, + FunctionType, + OverloadedType, Type, TypeBase, UnknownType, combineTypes, findSubtype, isAny, + isAnyOrUnknown, isClass, isClassInstance, isFunction, isFunctionOrOverloaded, isInstantiableClass, + isNever, + isOverloaded, maxTypeRecursionCount, } from './types'; @@ -45,6 +59,11 @@ interface EnumEvalStackEntry { // This stack is used to prevent infinite recursion when evaluating // enum members that refer to other enum members. const enumEvalStack: EnumEvalStackEntry[] = []; +const intFlagBitwiseMethods = new Set(['enum.IntFlag.__or__', 'enum.IntFlag.__xor__', 'enum.IntFlag.__and__']); + +export function isIntFlagBitwiseFunction(method: FunctionType): boolean { + return intFlagBitwiseMethods.has(method.shared.fullName); +} // Determines whether the class is an Enum metaclass or a subclass thereof. export function isEnumMetaclass(classType: ClassType) { @@ -58,7 +77,7 @@ export function isEnumMetaclass(classType: ClassType) { export function isEnumClassWithMembers( evaluator: TypeEvaluator, classType: ClassType, - nodeInfo: AnalyzerNodeInfoAccessor + nodeInfo: AnalyzerNodeInfoReader ) { if (!isClass(classType) || !ClassType.isEnumClass(classType)) { return false; @@ -125,6 +144,7 @@ export function createEnumType( '__class__', Symbol.createWithType(SymbolFlags.ClassMember | SymbolFlags.IgnoredForProtocolMatch, classType) ); + synthesizeEnumBoundary(evaluator, classType, argList); if (argList.length < 2) { return undefined; @@ -140,6 +160,42 @@ export function createEnumType( return undefined; } const classInstanceType = ClassType.cloneAsInstance(classType); + const isFlag = enumClass.shared.mro.some((base) => isClass(base) && ClassType.isBuiltIn(base, 'Flag')); + const startArg = isFlag ? argList.find((arg) => arg.name?.d.value === 'start') : undefined; + const startType = startArg ? evaluator.getTypeOfArg(startArg, /* inferenceContext */ undefined).type : undefined; + let nextFlagValue: bigint | undefined = BigInt(1); + if (startArg) { + nextFlagValue = + startType && + isClassInstance(startType) && + (typeof startType.priv.literalValue === 'number' || + typeof startType.priv.literalValue === 'bigint' || + typeof startType.priv.literalValue === 'boolean') + ? BigInt(startType.priv.literalValue) + : undefined; + } + + const getAutomaticValueType = (index: number): Type => { + if (!isFlag) { + return ClassType.cloneWithLiteral(ClassType.cloneAsInstance(intClassType), index + 1); + } + const generator = lookUpClassMember(enumClass, '_generate_next_value_'); + if ( + nextFlagValue === undefined || + !generator || + !isInstantiableClass(generator.classType) || + !ClassType.isBuiltIn(generator.classType, ['Enum', 'Flag']) + ) { + return ClassType.cloneAsInstance(intClassType); + } + const value = nextFlagValue; + const magnitude = value < BigInt(0) ? -value : value; + nextFlagValue = BigInt(1) << BigInt(magnitude === BigInt(0) ? 0 : magnitude.toString(2).length); + return ClassType.cloneWithLiteral( + ClassType.cloneAsInstance(intClassType), + value <= BigInt(Number.MAX_SAFE_INTEGER) && value >= BigInt(Number.MIN_SAFE_INTEGER) ? Number(value) : value + ); + }; // The Enum functional form supports various forms of arguments: // Enum('name', 'a b c') @@ -168,7 +224,7 @@ export function createEnumType( return undefined; } - const valueType = ClassType.cloneWithLiteral(ClassType.cloneAsInstance(intClassType), index + 1); + const valueType = getAutomaticValueType(index); const enumLiteral = new EnumLiteral( classType.shared.fullName, @@ -219,7 +275,7 @@ export function createEnumType( } nameNode = entry; - valueType = ClassType.cloneWithLiteral(ClassType.cloneAsInstance(intClassType), index + 1); + valueType = getAutomaticValueType(index); } else if (entry.nodeType === ParseNodeType.Tuple) { if (isSimpleString) { return undefined; @@ -305,6 +361,476 @@ export function createEnumType( return classType; } +export function synthesizeEnumBoundary(evaluator: TypeEvaluator, classType: ClassType, args: Arg[]) { + if (!ClassType.isEnumClass(classType)) { + return; + } + + const boundaryArg = args.find((arg) => arg.name?.d.value === 'boundary'); + let boundaryType = + boundaryArg?.typeResult?.type ?? + (boundaryArg?.valueExpression ? evaluator.getTypeOfExpression(boundaryArg.valueExpression).type : undefined); + + // None selects the inherited boundary, just as omitting the keyword does. + if (boundaryType && !isNoneInstance(boundaryType)) { + const inheritedBoundary = lookUpClassMember(classType, '_boundary_', MemberAccessFlags.SkipInstanceMembers); + const inheritedType = inheritedBoundary ? evaluator.getTypeOfMember(inheritedBoundary) : undefined; + if (inheritedType) { + boundaryType = mapSubtypes(boundaryType, (subtype) => (isNoneInstance(subtype) ? inheritedType : subtype)); + } + ClassType.getSymbolTable(classType).set( + '_boundary_', + Symbol.createWithType(SymbolFlags.ClassMember | SymbolFlags.IgnoredForProtocolMatch, boundaryType) + ); + } +} + +export function getTypeOfIntFlagMember( + evaluator: TypeEvaluator, + classType: ClassType, + memberType: Type, + nodeInfo: AnalyzerNodeInfoReader +): Type { + if ( + !ClassType.isEnumClass(classType) || + !isFunction(memberType) || + !intFlagBitwiseMethods.has(memberType.shared.fullName) + ) { + return memberType; + } + + if (!canFlagEject(evaluator, classType)) { + return memberType; + } + + const nonNegativeValues = getNonNegativeFlagMask(evaluator, classType, nodeInfo) !== undefined; + const intClass = classType.shared.mro.find((base) => isInstantiableClass(base) && ClassType.isBuiltIn(base, 'int')); + const otherIndex = memberType.shared.parameters.findIndex((param) => param.name === 'other'); + if (!intClass || !isInstantiableClass(intClass) || otherIndex < 0) { + return memberType; + } + + const parameterTypes = memberType.shared.parameters.map((_, index) => FunctionType.getParamType(memberType, index)); + const intResult = FunctionType.specialize(memberType, { + parameterTypes, + parameterDefaultTypes: memberType.priv.specializedTypes?.parameterDefaultTypes, + returnType: ClassType.cloneAsInstance(intClass), + }); + const zeroType = ClassType.cloneWithLiteral(ClassType.cloneAsInstance(intClass), 0); + if ( + !nonNegativeValues && + (!hasStandardFlagValueAccess(classType) || !isEnumClassWithMembers(evaluator, classType, nodeInfo)) + ) { + return intResult; + } + + // Zero is safe even for negative flag values. Nonnegative flags are also + // closed over operations with another value of the same enum. + const flagParameters = [...parameterTypes]; + flagParameters[otherIndex] = nonNegativeValues + ? combineTypes([ClassType.cloneAsInstance(classType), zeroType]) + : zeroType; + const flagResult = FunctionType.specialize(memberType, { + parameterTypes: flagParameters, + parameterDefaultTypes: memberType.priv.specializedTypes?.parameterDefaultTypes, + returnType: FunctionType.getEffectiveReturnType(memberType), + }); + return OverloadedType.create([flagResult, intResult]); +} + +export function applyIntFlagTransform( + evaluator: TypeEvaluator, + method: FunctionType, + argumentTypes: ReadonlyMap, + result: FunctionResult +): FunctionResult { + if (!isIntFlagBitwiseFunction(method)) { + return result; + } + + const isBound = !!method.priv.strippedFirstParamType; + const receiver = isBound ? method.priv.boundToType : argumentTypes.get('self'); + if (!receiver) { + return result; + } + + const returnType = getIntFlagReturnType( + evaluator, + method, + receiver, + argumentTypes.get('other') ?? UnknownType.create(), + result.returnType + ); + return { + ...result, + returnType, + }; +} + +export function getIntFlagReturnTypeForCallable( + evaluator: TypeEvaluator, + method: FunctionType, + srcParams: ParamListDetails, + destParams: ParamListDetails, + returnType: Type +): Type | undefined { + if ( + !intFlagBitwiseMethods.has(method.shared.fullName) || + srcParams.params.length !== destParams.params.length || + destParams.argsIndex !== undefined || + destParams.kwargsIndex !== undefined + ) { + return undefined; + } + + const getArgType = (index: number) => { + const name = srcParams.params[index]?.param.name; + const param = + destParams.params.find( + (param) => + param.kind !== ParamKind.Positional && + param.kind !== ParamKind.ExpandedArgs && + param.param.name === name + ) ?? destParams.params[index]; + return param?.type; + }; + const isBound = !!method.priv.strippedFirstParamType; + const receiver = isBound ? method.priv.boundToType : getArgType(0); + const other = getArgType(isBound ? 0 : 1); + // An empty argument domain adds no evidence that could invalidate the matched signature. + return receiver && other && !isNever(other) + ? getIntFlagReturnType(evaluator, method, receiver, other, returnType) + : undefined; +} + +function getIntFlagReturnType( + evaluator: TypeEvaluator, + method: FunctionType, + receiver: Type, + otherType: Type, + returnType: Type +): Type { + if (isNever(otherType)) { + return returnType; + } + + const overloads = method.priv.overloaded ? OverloadedType.getOverloads(method.priv.overloaded) : undefined; + const flagReturn = !method.priv.strippedFirstParamType + ? mapSubtypes(receiver, (subtype) => + isClassInstance(subtype) ? ClassType.cloneWithLiteral(subtype, /* value */ undefined) : subtype + ) + : overloads?.[0] + ? FunctionType.getEffectiveReturnType(overloads[0]) ?? returnType + : returnType; + let expandedOtherType: Type | undefined; + return mapSubtypes(evaluator.makeTopLevelTypeVarsConcrete(receiver), (classType) => { + if (!isClassInstance(classType) || !ClassType.isEnumClass(classType)) { + return returnType; + } + if (!canFlagEject(evaluator, classType)) { + return flagReturn; + } + const intClass = classType.shared.mro.find( + (base) => isInstantiableClass(base) && ClassType.isBuiltIn(base, 'int') + ); + if (!intClass || !isInstantiableClass(intClass)) { + return returnType; + } + expandedOtherType ??= mapSubtypes(evaluator.makeTopLevelTypeVarsConcrete(otherType), (other) => { + if ( + isClassInstance(other) && + other.priv.literalValue === undefined && + (ClassType.isBuiltIn(other, 'bool') || + (ClassType.isEnumClass(other) && hasStandardFlagConstruction(other))) + ) { + const literals = enumerateLiteralsForType(evaluator, other); + if (literals && literals.length > 0) { + // Keep the numeric alternatives rather than collapsing True | False back to bool. + return combineTypes( + literals.map((literal) => + ClassType.isBuiltIn(literal, 'bool') + ? ClassType.cloneWithLiteral( + ClassType.cloneAsInstance(intClass), + literal.priv.literalValue ? 1 : 0 + ) + : literal + ) + ); + } + } + return other; + }); + const mask = getNonNegativeFlagMask(evaluator, classType, getInfoReader(evaluator)); + const allBits = + mask !== undefined + ? (BigInt(1) << BigInt(mask === BigInt(0) ? 0 : mask.toString(2).length)) - BigInt(1) + : undefined; + const intType = ClassType.cloneAsInstance(intClass); + return mapSubtypes(expandedOtherType, (other) => { + if (flagReturn) { + const concreteOther = evaluator.makeTopLevelTypeVarsConcrete(other); + if ( + mask !== undefined && + isClassInstance(concreteOther) && + ClassType.isSameGenericClass(classType, concreteOther) + ) { + return flagReturn; + } + const valueType = + isClassInstance(concreteOther) && + ClassType.isSameGenericClass(classType, concreteOther) && + concreteOther.priv.literalValue instanceof EnumLiteral && + hasStandardFlagValueAccess(concreteOther) + ? concreteOther.priv.literalValue.itemType + : getIntFlagOperandValueType(evaluator, method, other, intClass, mask !== undefined); + if ( + valueType && + isClassInstance(valueType) && + ClassType.isBuiltIn(valueType, ['int', 'bool']) && + (typeof valueType.priv.literalValue === 'number' || + typeof valueType.priv.literalValue === 'bigint' || + typeof valueType.priv.literalValue === 'boolean') + ) { + const value = BigInt(valueType.priv.literalValue); + if (mask !== undefined && method.shared.name === '__and__') { + return flagReturn; + } + const memberValueType = + classType.priv.literalValue instanceof EnumLiteral + ? classType.priv.literalValue.itemType + : undefined; + if ( + mask === undefined && + memberValueType && + isClassInstance(memberValueType) && + ClassType.isBuiltIn(memberValueType, ['int', 'bool']) && + (typeof memberValueType.priv.literalValue === 'number' || + typeof memberValueType.priv.literalValue === 'bigint' || + typeof memberValueType.priv.literalValue === 'boolean') && + hasStandardFlagValueAccess(classType) + ) { + const memberValue = BigInt(memberValueType.priv.literalValue); + const result = + method.shared.name === '__or__' + ? memberValue | value + : method.shared.name === '__xor__' + ? memberValue ^ value + : memberValue & value; + // Enum construction reuses an existing member before considering EJECT. + if (result === memberValue) { + return flagReturn; + } + } + if ( + value === BigInt(0) && + hasStandardFlagValueAccess(classType) && + isEnumClassWithMembers(evaluator, classType, getInfoReader(evaluator)) + ) { + return flagReturn; + } + if ( + mask !== undefined && + allBits !== undefined && + value >= ~allBits && + value <= allBits && + (value & (allBits ^ mask)) === BigInt(0) + ) { + return flagReturn; + } + } + } + return intType; + }); + }); +} + +function getIntFlagOperandValueType( + evaluator: TypeEvaluator, + method: FunctionType, + other: Type, + intClass: ClassType, + nonNegativeReceiver: boolean +): Type | undefined { + if (!isClassInstance(other) || !(other.priv.literalValue instanceof EnumLiteral)) { + return other; + } + if (!hasStandardFlagConstruction(other)) { + return undefined; + } + + const reflectedName = `__r${method.shared.name.slice(2)}`; + const reflected = lookUpClassMember(other, reflectedName); + const reflectedType = reflected ? evaluator.getTypeOfMember(reflected) : undefined; + const functions = reflectedType + ? isOverloaded(reflectedType) + ? OverloadedType.getOverloads(reflectedType) + : isFunction(reflectedType) + ? [reflectedType] + : [] + : []; + if (functions.length === 0) { + return undefined; + } + const valueType = other.priv.literalValue.itemType; + if ( + functions.every( + (fn) => + fn.shared.fullName === `${intClass.shared.fullName}.${reflectedName}` || + fn.shared.fullName === `${intClass.shared.fullName}.${method.shared.name}` + ) + ) { + return valueType; + } + + // Another standard flag can normalize negative values or mask bits under CONFORM. + // For nonnegative operands, masking cannot introduce bits outside the receiver. + if ( + nonNegativeReceiver && + functions.every((fn) => fn.shared.fullName === method.shared.fullName) && + isClassInstance(valueType) && + (typeof valueType.priv.literalValue === 'number' || typeof valueType.priv.literalValue === 'bigint') && + valueType.priv.literalValue >= 0 && + getNonNegativeFlagMask(evaluator, other, getInfoReader(evaluator)) !== undefined + ) { + return valueType; + } + return undefined; +} + +function canFlagEject(evaluator: TypeEvaluator, classType: ClassType): boolean { + if (!ClassType.isEnumClass(classType)) { + return false; + } + const member = lookUpClassMember(classType, '_boundary_', MemberAccessFlags.SkipInstanceMembers); + return ( + (!!member && + !!findSubtype( + evaluator.getTypeOfMember(member), + (subtype) => + isAnyOrUnknown(subtype) || + (isClassInstance(subtype) && + ClassType.isBuiltIn(subtype, 'FlagBoundary') && + (!(subtype.priv.literalValue instanceof EnumLiteral) || + subtype.priv.literalValue.itemName === 'EJECT')) + )) || + !isEnumClassWithMembers(evaluator, classType, getInfoReader(evaluator)) + ); +} + +function getNonNegativeFlagMask( + evaluator: TypeEvaluator, + classType: ClassType, + nodeInfo: AnalyzerNodeInfoReader +): bigint | undefined { + if (!hasStandardFlagValueAccess(classType)) { + return undefined; + } + + let foundMember = false; + let mask = BigInt(0); + const memberValues = new Map(); + const getAutomaticValue = (node: ExpressionNode, recursionCount = 0): bigint | undefined => { + if (recursionCount > maxTypeRecursionCount) { + return undefined; + } + if (node.nodeType === ParseNodeType.Name) { + return memberValues.get(node.d.value); + } + if (node.nodeType === ParseNodeType.Number && node.d.isInteger && !node.d.isImaginary) { + return BigInt(node.d.value); + } + if (node.nodeType === ParseNodeType.BinaryOperation) { + const left = getAutomaticValue(node.d.leftExpr, recursionCount + 1); + const right = getAutomaticValue(node.d.rightExpr, recursionCount + 1); + if (left !== undefined && right !== undefined) { + switch (node.d.operator) { + case OperatorType.BitwiseOr: + return left | right; + case OperatorType.BitwiseAnd: + return left & right; + case OperatorType.BitwiseXor: + return left ^ right; + } + } + } + if (node.nodeType === ParseNodeType.Call) { + const callee = evaluator.getTypeOfExpression(node.d.leftExpr).type; + const generator = lookUpClassMember(classType, '_generate_next_value_'); + if ( + isInstantiableClass(callee) && + ClassType.isBuiltIn(callee, 'auto') && + generator && + isInstantiableClass(generator.classType) && + ClassType.isBuiltIn(generator.classType, ['Enum', 'Flag']) + ) { + return BigInt(1) << BigInt(mask === BigInt(0) ? 0 : mask.toString(2).length); + } + } + return undefined; + }; + for (const [name, symbol] of ClassType.getSymbolTable(classType)) { + if (symbol.isIgnoredForProtocolMatch()) { + continue; + } + + const type = + transformTypeForEnumMember(evaluator, classType, name, nodeInfo) ?? + evaluator.getEffectiveTypeOfSymbol(symbol); + if ( + isClassInstance(type) && + ClassType.isSameGenericClass(type, classType) && + type.priv.literalValue instanceof EnumLiteral + ) { + const valueType = type.priv.literalValue.itemType; + let value = memberValues.get(type.priv.literalValue.itemName); + if ( + value === undefined && + isClassInstance(valueType) && + ClassType.isBuiltIn(valueType, ['int', 'bool']) && + (typeof valueType.priv.literalValue === 'number' || + typeof valueType.priv.literalValue === 'bigint' || + typeof valueType.priv.literalValue === 'boolean') + ) { + value = BigInt(valueType.priv.literalValue); + } + if (value === undefined) { + const declaration = symbol.getDeclarations()[0]; + const assignment = declaration?.node.parent; + if (assignment?.nodeType === ParseNodeType.Assignment) { + value = getAutomaticValue(assignment.d.rightExpr); + } + } + if (value === undefined || value < BigInt(0)) { + return undefined; + } + memberValues.set(name, value); + mask |= value; + foundMember = true; + } + } + return foundMember ? mask : undefined; +} + +function hasStandardFlagValueAccess(classType: ClassType): boolean { + const member = lookUpClassMember(classType, '__getattribute__'); + // Flag operators read _value_; builtin int operators use the numeric payload instead. + return ( + hasStandardFlagConstruction(classType) && + (!member || !isClass(member.classType) || ClassType.isBuiltIn(member.classType)) + ); +} + +function hasStandardFlagConstruction(classType: ClassType): boolean { + if (ClassType.isEnumMemberSetMayBeDynamicallyModified(classType)) { + return false; + } + // Custom construction or storage may change the values assigned in the class body. + return !['__new__', '__init__', '_missing_', '_value_'].some((name) => { + const member = lookUpClassMember(classType, name); + return member && isClass(member.classType) && !ClassType.isBuiltIn(member.classType); + }); +} + // Performs the "magic" that the Enum metaclass does at runtime when it // transforms a value into an enum instance. If the specified name isn't // an enum member, this function returns undefined indicating that the diff --git a/packages/pyright-internal/src/analyzer/operations.ts b/packages/pyright-internal/src/analyzer/operations.ts index 9c242f4103cb..1e07860faabb 100644 --- a/packages/pyright-internal/src/analyzer/operations.ts +++ b/packages/pyright-internal/src/analyzer/operations.ts @@ -29,7 +29,9 @@ import { getTupleComparisonResult, isTupleOrdering } from './tupleComparison'; import { EvalFlags, MagicMethodDeprecationInfo, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { InferenceContext, + MemberAccessFlags, convertToInstantiable, + getClassMemberIterator, getLiteralTypeClassName, getTypeCondition, getUnionSubtypeCount, @@ -51,6 +53,7 @@ import { import { ClassType, NeverType, + OverloadedType, Type, TypeBase, UnknownType, @@ -60,9 +63,11 @@ import { isAnyOrUnknown, isClass, isClassInstance, + isFunction, isFunctionOrOverloaded, isInstantiableClass, isNever, + isOverloaded, isUnion, } from './types'; @@ -108,6 +113,56 @@ interface BinaryOperationOptions { // cut off the literal union and fall back to the non-literal supertype. const maxLiteralMathSubtypeCount = 64; +function shouldPrioritizeReflectedMethod( + evaluator: TypeEvaluator, + leftType: Type, + rightType: Type, + methodName: string +): boolean { + if ( + !isClassInstance(leftType) || + !isClassInstance(rightType) || + // A synthesized narrowing intersection is not evidence of a runtime subclass. + !rightType.shared.declaration || + ClassType.isSameGenericClass(leftType, rightType) || + !rightType.shared.mro.some( + (base) => + isInstantiableClass(base) && ClassType.isSameGenericClass(base, ClassType.cloneAsInstantiable(leftType)) + ) + ) { + return false; + } + + const rightMember = getClassMemberIterator(rightType, methodName, MemberAccessFlags.SkipInstanceMembers).next() + .value; + const leftMember = getClassMemberIterator(leftType, methodName, MemberAccessFlags.SkipInstanceMembers).next().value; + if (!rightMember || rightMember.symbol === leftMember?.symbol) { + return false; + } + + const rightMethod = evaluator.getTypeOfMember(rightMember); + const leftMethod = leftMember ? evaluator.getTypeOfMember(leftMember) : undefined; + const rightFunction = isOverloaded(rightMethod) ? OverloadedType.getOverloads(rightMethod)[0] : rightMethod; + const leftFunction = + leftMethod && isOverloaded(leftMethod) ? OverloadedType.getOverloads(leftMethod)[0] : leftMethod; + + // Compare unbound declarations, not specialized signatures: aliases and + // inherited generic methods can have different signatures but the same implementation. + return ( + !!rightFunction && + isFunction(rightFunction) && + !!rightFunction.shared.declaration && + (!leftFunction || + (isFunction(leftFunction) && rightFunction.shared.declaration !== leftFunction.shared.declaration)) + ); +} + +function removeNotImplemented(type: Type): Type { + return mapSubtypes(type, (subtype) => + isClassInstance(subtype) && subtype.shared.fullName === 'types.NotImplementedType' ? undefined : subtype + ); +} + export function validateBinaryOperation( evaluator: TypeEvaluator, operator: OperatorType, @@ -1371,8 +1426,48 @@ function validateArithmeticOperation( } } - const magicMethodName = binaryOperatorMap[operator][0]; - let resultTypeResult = evaluator.getTypeOfMagicMethodCall( + const [magicMethodName, altMagicMethodName] = binaryOperatorMap[operator]; + let resultTypeResult: TypeResult | undefined; + let decliningReflectedResult: TypeResult | undefined; + let skipReflectedFallback = false; + + // Rich comparisons have different precedence rules. Leave their + // dispatch, and all ordinary forward-first arithmetic, unchanged. + if ( + altMagicMethodName.startsWith('__r') && + shouldPrioritizeReflectedMethod( + evaluator, + leftSubtypeExpanded, + rightSubtypeExpanded, + altMagicMethodName + ) + ) { + const reflectedResult = evaluator.getTypeOfMagicMethodCall( + rightSubtypeUnexpanded, + altMagicMethodName, + [{ type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }], + errorNode, + inferenceContext + ); + if (reflectedResult) { + const acceptedType = removeNotImplemented(reflectedResult.type); + if (acceptedType === reflectedResult.type) { + resultTypeResult = reflectedResult; + } else if ( + reflectedResult.overloadsUsedForCall?.length && + reflectedResult.overloadsUsedForCall.every( + (overload) => overload.shared.declaredReturnType !== undefined + ) + ) { + // Only an explicit return contract establishes a decline; + // an inherited method's inferred sentinel may be contextual. + decliningReflectedResult = { ...reflectedResult, type: acceptedType }; + skipReflectedFallback = true; + } + } + } + + resultTypeResult ??= evaluator.getTypeOfMagicMethodCall( convertFunctionToObject(evaluator, leftSubtypeUnexpanded), magicMethodName, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], @@ -1402,9 +1497,28 @@ function validateArithmeticOperation( ); } - if (!resultTypeResult) { + if (skipReflectedFallback && resultTypeResult) { + const acceptedType = removeNotImplemented(resultTypeResult.type); + if ( + acceptedType !== resultTypeResult.type && + !( + resultTypeResult.overloadsUsedForCall?.length && + resultTypeResult.overloadsUsedForCall.every( + (overload) => overload.shared.declaredReturnType !== undefined + ) + ) + ) { + decliningReflectedResult = undefined; + } else { + resultTypeResult = + acceptedType !== resultTypeResult.type && isNever(acceptedType) + ? undefined + : { ...resultTypeResult, type: acceptedType }; + } + } + + if (!resultTypeResult && !skipReflectedFallback) { // Try the alternate form (swapping right and left). - const altMagicMethodName = binaryOperatorMap[operator][1]; resultTypeResult = evaluator.getTypeOfMagicMethodCall( convertFunctionToObject(evaluator, rightSubtypeUnexpanded), altMagicMethodName, @@ -1441,6 +1555,20 @@ function validateArithmeticOperation( } } + if (decliningReflectedResult && (resultTypeResult || !isNever(decliningReflectedResult.type))) { + resultTypeResult = { + ...decliningReflectedResult, + type: combineTypes([ + decliningReflectedResult.type, + resultTypeResult?.type ?? NeverType.createNever(), + ]), + isIncomplete: decliningReflectedResult.isIncomplete || resultTypeResult?.isIncomplete, + magicMethodDeprecationInfo: + decliningReflectedResult.magicMethodDeprecationInfo ?? + resultTypeResult?.magicMethodDeprecationInfo, + }; + } + if (!resultTypeResult) { if (inferenceContext && !isAnyOrUnknown(inferenceContext.expectedType)) { diag.addMessage( diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index adff759c1c92..4a70568bbf2e 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -135,12 +135,16 @@ import { getFunctionInfoFromDecorators, } from './decorators'; import { + applyIntFlagTransform, createEnumType, getEnumAutoValueType, getTypeOfEnumMember, + getTypeOfIntFlagMember, isDeclInEnumClass, isEnumClassWithMembers, isEnumMetaclass, + isIntFlagBitwiseFunction, + synthesizeEnumBoundary, } from './enums'; import { applyFunctionTransform } from './functionTransform'; import { createNamedTupleType } from './namedTuples'; @@ -202,6 +206,7 @@ import { EvaluatorUsage, ExpectedTypeOptions, ExpectedTypeResult, + FunctionResult, FunctionTypeResult, MapSubtypesOptions, MemberAccessDeprecationInfo, @@ -1893,7 +1898,10 @@ export function createTypeEvaluator( } return { - type: memberInfo.type, + type: + usage.method === 'get' + ? getTypeOfIntFlagMember(evaluatorInterface, objectType, memberInfo.type, nodeInfo) + : memberInfo.type, classType: memberInfo.classType, isIncomplete: !!memberInfo.isTypeIncomplete, isAsymmetricAccessor: memberInfo.isAsymmetricAccessor, @@ -8509,6 +8517,8 @@ export function createTypeEvaluator( overloadIndex: number ): MatchArgsToParamsResult { const overload = typeResult.type; + const unexpandedArgList = + isIntFlagBitwiseFunction(overload) && !overload.priv.strippedFirstParamType ? argList : undefined; const paramDetails = getParamListDetails(overload, { disallowExtraKwargsForTd: true }); const paramSpec = FunctionType.getParamSpecFromArgsKwargs(overload); @@ -9537,6 +9547,7 @@ export function createTypeEvaluator( argumentErrors: reportedArgError, isTypeIncomplete, argParams: validateArgTypeParams, + unexpandedArgList, paramSpecTarget, paramSpecArgList, activeParam, @@ -9991,6 +10002,16 @@ export function createTypeEvaluator( matchResults.argumentMatchScore = argumentMatchScore; + if (!argumentErrors && isIntFlagBitwiseFunction(type)) { + const transformed = applyIntFlagCallTransform(errorNode, matchResults, argResults, { + argumentErrors, + returnType: specializedReturnType, + isTypeIncomplete, + }); + specializedReturnType = transformed.returnType; + isTypeIncomplete = transformed.isTypeIncomplete; + } + return { argumentErrors, argResults, @@ -10003,6 +10024,88 @@ export function createTypeEvaluator( }; } + function applyIntFlagCallTransform( + errorNode: ExpressionNode, + matchResults: MatchArgsToParamsResult, + argResults: ArgResult[], + result: FunctionResult + ): FunctionResult { + const argumentTypes = new Map(); + matchResults.argParams.forEach((param, index) => { + if (param.paramName) { + argumentTypes.set(param.paramName, argResults[index].argType); + } + }); + result = applyIntFlagTransform(evaluatorInterface, matchResults.overload, argumentTypes, result); + + const argList = matchResults.unexpandedArgList; + if (!argList?.some((arg) => arg.argCategory !== ArgCategory.Simple)) { + return result; + } + + // Keep correlated packed alternatives together rather than treating their + // independently combined parameter types as a Cartesian product. + const contextFreeArgTypes = argList.map((arg) => + arg.argCategory === ArgCategory.Simple + ? UnknownType.create() + : getTypeOfArg(arg, /* inferenceContext */ undefined).type + ); + let expandedArgTypes: (Type | undefined)[][] = [argList.map(() => undefined)]; + while (true) { + const expanded = expressionEvaluation.expandArgTypes(contextFreeArgTypes, expandedArgTypes); + if (!expanded) { + break; + } + if (expanded.length > maxTotalOverloadArgTypeExpansionCount) { + return result; + } + expandedArgTypes = expanded; + } + if (expandedArgTypes.length === 1) { + return result; + } + + const returnTypes: Type[] = []; + let isTypeIncomplete = result.isTypeIncomplete; + for (const expandedTypes of expandedArgTypes) { + const expandedArgs = argList.map((arg, index): Arg => { + const type = expandedTypes[index]; + return type + ? { + ...arg, + valueExpression: undefined, + typeResult: { type, isIncomplete: result.isTypeIncomplete }, + } + : arg; + }); + const expandedMatch = useSpeculativeMode(getSpeculativeNodeForCall(errorNode), () => + matchArgsToParams( + errorNode, + expandedArgs, + { type: matchResults.overload, isIncomplete: result.isTypeIncomplete }, + matchResults.overloadIndex + ) + ); + isTypeIncomplete ||= expandedMatch.isTypeIncomplete; + if (expandedMatch.argumentErrors) { + return { ...result, isTypeIncomplete }; + } + + const expandedArgumentTypes = new Map(argumentTypes); + expandedMatch.argParams.forEach((param) => { + const type = param.argType ?? param.argument.typeResult?.type; + if (param.paramName && type) { + expandedArgumentTypes.set(param.paramName, type); + } + }); + returnTypes.push( + applyIntFlagTransform(evaluatorInterface, matchResults.overload, expandedArgumentTypes, result) + .returnType + ); + } + return { ...result, returnType: combineTypes(returnTypes), isTypeIncomplete }; + } + // In general, all in-scope type variables left in a return type should be // replaced with Unknown. However, if the return type is a callable that uses // type vars that are found nowhere within the function's input parameters, @@ -12788,6 +12891,7 @@ export function createTypeEvaluator( // Clear the "partially constructed" flag. classType.shared.flags &= ~ClassTypeFlags.PartiallyEvaluated; + synthesizeEnumBoundary(evaluatorInterface, classType, initSubclassArgs); // Now determine the decorated type of the class. let decoratedType: Type = classType; diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator/internals.ts b/packages/pyright-internal/src/analyzer/typeEvaluator/internals.ts index 427b18ccdfe3..0d35ee5b86f9 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator/internals.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator/internals.ts @@ -59,6 +59,7 @@ export interface MatchArgsToParamsResult { argumentErrors: boolean; isTypeIncomplete: boolean; argParams: ValidateArgTypeParams[]; + unexpandedArgList?: Arg[] | undefined; activeParam?: FunctionParam | undefined; paramSpecTarget?: ParamSpecType | undefined; paramSpecArgList?: Arg[] | undefined; diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator/typeCompatibility.ts b/packages/pyright-internal/src/analyzer/typeEvaluator/typeCompatibility.ts index cafca74bbe94..6f3ef41fe8c4 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator/typeCompatibility.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator/typeCompatibility.ts @@ -12,6 +12,7 @@ import { DiagnosticAddendum } from '../../common/diagnostic'; import { LocAddendum } from '../../localization/localize'; import { ParamCategory } from '../../parser/parseNodes'; import { ConstraintTracker } from '../constraintTracker'; +import { getIntFlagReturnTypeForCallable } from '../enums'; import { getParamListDetails, ParamKind, ParamListDetails, VirtualParamDetails } from '../parameterUtils'; import { assignClassToProtocol } from '../protocols'; import { assignTupleTypeArgs, makeTupleObject } from '../tuples'; @@ -1982,7 +1983,27 @@ export function createTypeCompatibility(context: Context) { if (checkReturnType) { const destReturnType = getEffectiveReturnType(destType); if (!isAnyOrUnknown(destReturnType)) { - const srcReturnType = solveAndApplyConstraints(getEffectiveReturnType(srcType), constraints); + let srcReturnType = solveAndApplyConstraints(getEffectiveReturnType(srcType), constraints); + // Intrinsic returns can depend on the target callable's narrower parameter domain. + if ( + canAssign && + (flags & + (AssignTypeFlags.OverloadOverlap | + AssignTypeFlags.PartialOverloadOverlap | + AssignTypeFlags.Contravariant)) === + 0 + ) { + const intrinsicReturnType = getIntFlagReturnTypeForCallable( + evaluatorInterface, + srcType, + srcParamDetails, + destParamDetails, + srcReturnType + ); + if (intrinsicReturnType) { + srcReturnType = solveAndApplyConstraints(intrinsicReturnType, constraints); + } + } const returnDiag = diag?.createAddendum(); let isReturnTypeCompatible = false; diff --git a/packages/pyright-internal/src/tests/samples/enum17.py b/packages/pyright-internal/src/tests/samples/enum17.py new file mode 100644 index 000000000000..e49d3feb8173 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/enum17.py @@ -0,0 +1,222 @@ +# Flag boundaries affect the standard bitwise methods, not user-defined methods. + +from enum import CONFORM, EJECT, KEEP, STRICT, FlagBoundary, IntFlag, auto +from typing import Callable, Literal, TypedDict, TypeVar + + +class EjectFlag(IntFlag, boundary=EJECT): + A = 1 + B = 2 + + +class KeepFlag(IntFlag): + A = 1 + + +class ExplicitKeep(IntFlag, boundary=KEEP): + A = 1 + + +class StrictFlag(IntFlag, boundary=STRICT): + A = 1 + + +class ConformFlag(IntFlag, boundary=CONFORM): + A = 1 + + +class EjectBase(IntFlag, boundary=EJECT): + pass + + +class InheritedEject(EjectBase): + A = 1 + + +class OverriddenKeep(EjectBase, boundary=KEEP): + A = 1 + + +class NegativeEject(IntFlag, boundary=EJECT): + A = -1 + + +class AutoEject(IntFlag, boundary=EJECT): + A = auto() + B = auto() + AB = A | B + C = auto() + + +class SparseEject(IntFlag, boundary=EJECT): + A = 1 + B = 4 + + +class BooleanEject(IntFlag, boundary=EJECT): + A = True + + +class CustomEject(IntFlag, boundary=EJECT): + A = 1 + + def __ror__(self, other: int) -> str: + return "custom" + + +def bitwise(value: int, flag: EjectFlag): + reveal_type(value | flag, expected_text="int") + reveal_type(flag | value, expected_text="int") + reveal_type(value ^ flag, expected_text="int") + reveal_type(flag ^ value, expected_text="int") + reveal_type(value & flag, expected_text="int") + reveal_type(flag & value, expected_text="int") + reveal_type(flag | flag, expected_text="EjectFlag") + reveal_type(flag ^ flag, expected_text="EjectFlag") + reveal_type(flag & flag, expected_text="EjectFlag") + reveal_type(flag.__ror__(value), expected_text="int") + reveal_type(flag.__or__(value), expected_text="int") + reveal_type(EjectFlag.__ror__(flag, value), expected_text="int") + reveal_type(flag.__rand__(value), expected_text="int") + reveal_type(flag.__or__(flag), expected_text="EjectFlag") + bound = flag.__ror__ + reveal_type(bound(value), expected_text="int") + reveal_type(bound(flag), expected_text="EjectFlag") + reveal_type(flag | 3, expected_text="EjectFlag") + reveal_type(flag ^ 3, expected_text="EjectFlag") + reveal_type(flag | 8, expected_text="int") + reveal_type(flag.__ror__(0), expected_text="EjectFlag") + reveal_type(EjectFlag.__ror__(flag, 3), expected_text="EjectFlag") + reveal_type(IntFlag.__ror__(flag, value), expected_text="int") + reveal_type(IntFlag.__or__(flag, 3), expected_text="EjectFlag") + reveal_type(IntFlag.__or__(other=3, self=flag), expected_text="EjectFlag") + reveal_type(flag | -1, expected_text="EjectFlag") + reveal_type(flag ^ -4, expected_text="EjectFlag") + reveal_type(flag | -8, expected_text="int") + reveal_type(flag | KeepFlag.A, expected_text="EjectFlag") + reveal_type(flag | True, expected_text="EjectFlag") + reveal_type(IntFlag.__or__(KeepFlag.A, value), expected_text="KeepFlag") + function: Callable[[int], int] = flag.__ror__ + reveal_type(function(value), expected_text="int") + + +def unsafe(value: int): + # Each should generate an error: EJECT can return int. + (value | EjectFlag.A).name + (value ^ EjectFlag.A).name + (EjectFlag.A | value).name + EjectFlag.A.__ror__(value).name + (value & EjectFlag.A).name + (EjectFlag.A & value).name + EjectFlag.A.__and__(value).name + + +class OperatorArgs(TypedDict): + self: EjectFlag + other: int + + +def general(flag: IntFlag, value: int, arguments: OperatorArgs): + reveal_type(flag | value, expected_text="int") + reveal_type(flag & value, expected_text="int") + reveal_type(flag ^ value, expected_text="int") + # Each should generate an error: the receiver may have EJECT behavior. + (flag | value).name + IntFlag.__or__(**arguments).name + + +class OpenFlag(IntFlag): + pass + + +def open_base(flag: OpenFlag, value: int): + reveal_type(flag | value, expected_text="int") + + +def valid(value: int, flag: EjectFlag): + reveal_type((0 & flag).name, expected_text="str | None") + reveal_type(value | KeepFlag.A, expected_text="KeepFlag") + reveal_type(value ^ KeepFlag.A, expected_text="KeepFlag") + reveal_type(value | ExplicitKeep.A, expected_text="ExplicitKeep") + reveal_type(value | StrictFlag.A, expected_text="StrictFlag") + reveal_type(value | ConformFlag.A, expected_text="ConformFlag") + reveal_type(value | InheritedEject.A, expected_text="int") + reveal_type(value | OverriddenKeep.A, expected_text="OverriddenKeep") + reveal_type(value | CustomEject.A, expected_text="str") + reveal_type(value & NegativeEject.A, expected_text="int") + reveal_type(NegativeEject.A & value, expected_text="int") + reveal_type(value & AutoEject.A, expected_text="int") + reveal_type(AutoEject.A | AutoEject.B, expected_text="AutoEject") + reveal_type(AutoEject.A | 7, expected_text="AutoEject") + reveal_type(AutoEject.A | 8, expected_text="int") + reveal_type(BooleanEject.A & value, expected_text="int") + reveal_type(SparseEject.A | -1, expected_text="int") + reveal_type(SparseEject.A | -3, expected_text="SparseEject") + + +def augmentation(value: int, flag: EjectFlag): + a = value + a |= flag + reveal_type(a, expected_text="int") + b = value + b ^= flag + reveal_type(b, expected_text="int") + c = value + c &= flag + reveal_type(c, expected_text="int") + d = flag + d |= value + reveal_type(d, expected_text="int") + e = flag + e ^= value + reveal_type(e, expected_text="int") + + +def dynamic_boundary(boundary: FlagBoundary): + class DynamicFlag(IntFlag, boundary=boundary): + A = 1 + + reveal_type(DynamicFlag.A.__ror__(int()), expected_text="int") + + +def aliased_boundary(): + boundary: Literal[FlagBoundary.EJECT] = EJECT + + class AliasedFlag(IntFlag, boundary=boundary): + A = 1 + + reveal_type(AliasedFlag.A.__ror__(int()), expected_text="int") + + +def optional_boundary(boundary: Literal[FlagBoundary.KEEP] | None): + class OptionalFlag(EjectBase, boundary=boundary): + A = 1 + + reveal_type(OptionalFlag.A.__ror__(int()), expected_text="int") + + +FlagT = TypeVar("FlagT", bound=EjectFlag) + + +def generic(flag: FlagT) -> FlagT: + reveal_type(flag | flag, expected_text="FlagT@generic") + reveal_type(flag | 3, expected_text="FlagT@generic") + reveal_type(flag & 0, expected_text="FlagT@generic") + reveal_type(flag & int(), expected_text="int") + reveal_type(flag | int(), expected_text="int") + return flag | flag + + +Functional = IntFlag("Functional", {"A": 1}, boundary=EJECT) +reveal_type(Functional.A.__ror__(int()), expected_text="int") +reveal_type(Functional.A.__and__(int()), expected_text="int") + +FunctionalNames = IntFlag("FunctionalNames", "A B C", boundary=EJECT) +reveal_type(FunctionalNames.C.value, expected_text="Literal[4]") +reveal_type(FunctionalNames.A | 7, expected_text="FunctionalNames") +reveal_type(FunctionalNames.A | 8, expected_text="int") + +FunctionalStart = IntFlag("FunctionalStart", ["A", "B", "C"], boundary=EJECT, start=8) +reveal_type(FunctionalStart.C.value, expected_text="Literal[32]") +reveal_type(FunctionalStart.A | 3, expected_text="int") +reveal_type(FunctionalStart.A | 56, expected_text="FunctionalStart") diff --git a/packages/pyright-internal/src/tests/samples/enum18.py b/packages/pyright-internal/src/tests/samples/enum18.py new file mode 100644 index 000000000000..e5a831a5ef0e --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/enum18.py @@ -0,0 +1,162 @@ +# Intrinsic flag results also apply when assigning operator methods to callbacks. + +from enum import EJECT, IntEnum, IntFlag +from typing import Callable, Literal, Never, Protocol, TypeVar + + +class Flags(IntFlag, boundary=EJECT): + A = 1 + B = 2 + + +class Keep(IntFlag): + A = 1 + + +class Negative(IntFlag, boundary=EJECT): + A = -1 + + +class Large(IntFlag, boundary=EJECT): + A = 1606938044258990275541962092341162602522202993782792835301376 + + +class Integers(IntEnum): + ZERO = 0 + THREE = 3 + + +def zero(flag: Flags) -> Callable[[Literal[0]], Flags]: + return flag.__or__ + + +def union(flag: Flags) -> Callable[[Literal[0, 1, 3]], Flags]: + return flag.__xor__ + + +def negative_operand(flag: Flags) -> Callable[[Literal[-1]], Flags]: + return flag.__or__ + + +def negative_flag(flag: Negative) -> Callable[[Literal[0]], Negative]: + return flag.__xor__ + + +def large(flag: Large) -> Callable[ + [Literal[1606938044258990275541962092341162602522202993782792835301376]], Large +]: + return flag.__or__ + + +def boolean(flag: Flags) -> Callable[[bool], Flags]: + return flag.__or__ + + +def enum_argument(flag: Flags) -> Callable[[Integers], Flags]: + return flag.__or__ + + +def captured(flag: Flags) -> Callable[[Literal[3]], Flags]: + operation = flag.__or__ + return operation + + +def unbound() -> Callable[[Flags, Literal[0]], Flags]: + return IntFlag.__or__ + + +def unbound_keep() -> Callable[[Keep, int], Keep]: + return IntFlag.__or__ + + +class KeywordCallback(Protocol): + def __call__(self, *, other: Literal[3]) -> Flags: ... + + +def keyword(flag: Flags) -> KeywordCallback: + return flag.__or__ + + +class UnboundKeywordCallback(Protocol): + def __call__(_self, *, other: Literal[3], self: Flags) -> Flags: ... + + +def unbound_keyword() -> UnboundKeywordCallback: + return IntFlag.__or__ + + +FlagT = TypeVar("FlagT", bound=Flags) + + +def generic(flag: FlagT) -> Callable[[Literal[3]], FlagT]: + return flag.__or__ + + +def factory(flag: FlagT, other: FlagT): + operation: Callable[[int], int] = flag.__or__ + return operation + + +def factory_check(flag: Flags): + operation = factory(flag, flag) + reveal_type(operation(3), expected_text="Flags") + callback: Callable[[Literal[3]], Flags] = operation + reveal_type(callback(3), expected_text="Flags") + + +def unbound_union() -> Callable[[Flags | Keep, Literal[0]], Flags | Keep]: + return IntFlag.__or__ + + +UnionT = TypeVar("UnionT", bound=Flags | Keep) + + +def unbound_generic() -> Callable[[UnionT, Literal[0]], UnionT]: + return IntFlag.__or__ + + +def empty_domain(flag: Flags) -> tuple[ + Callable[[Never], Flags], Callable[[Never], Flags], Callable[[Never], Flags] +]: + return flag.__or__, flag.__xor__, flag.__and__ + + +def negative_empty_domain(flag: Negative) -> tuple[ + Callable[[Never], Negative], Callable[[Never], Negative], Callable[[Never], Negative] +]: + return flag.__or__, flag.__xor__, flag.__and__ + + +def invalid_empty_return(flag: Flags) -> Callable[[Never], str]: + # This should generate an error: empty domains do not relax return compatibility. + return flag.__or__ + + +def unsafe(flag: Flags) -> Callable[[int], Flags]: + # This should generate an error: arbitrary ints may eject. + return flag.__or__ + + +def invalid_literal(flag: Flags) -> Callable[[Literal[8]], Flags]: + # This should generate an error: the bit is outside the enum's mask. + return flag.__or__ + + +def invalid_union(flag: Flags) -> Callable[[Literal[0, 8]], Flags]: + # This should generate an error: one alternative may eject. + return flag.__or__ + + +def invalid_parameter(flag: Flags) -> Callable[[str], Flags]: + # This should generate an error: return refinement cannot repair parameter mismatches. + return flag.__or__ + + +def invalid_receiver() -> Callable[[str, Literal[0]], Flags]: + # This should generate an error: the unbound receiver must be an IntFlag. + return IntFlag.__or__ + + +def invalid_boolean(flag: Large) -> Callable[[bool], Large]: + # This should generate an error: True would introduce an invalid bit. + return flag.__or__ diff --git a/packages/pyright-internal/src/tests/samples/enum19.py b/packages/pyright-internal/src/tests/samples/enum19.py new file mode 100644 index 000000000000..0c58eaa16d5a --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/enum19.py @@ -0,0 +1,115 @@ +# Intrinsic flag returns use parameter bindings, including unpacked arguments. + +from enum import EJECT, IntFlag +from typing import Literal, Never, TypedDict, TypeVar + + +class Flags(IntFlag, boundary=EJECT): + A = 1 + B = 2 + + +class Keep(IntFlag): + A = 1 + + +class Negative(IntFlag, boundary=EJECT): + A = -1 + + +class OtherArgs(TypedDict): + other: Literal[0, 3] + + +class FullArgs(OtherArgs): + self: Flags + + +class IntArgs(TypedDict): + other: int + + +class FullIntArgs(IntArgs): + self: Flags + + +def bound(flag: Flags, keywords: OtherArgs, positional: tuple[Literal[0, 3]]) -> Flags: + reveal_type(flag.__or__(**keywords), expected_text="Flags") + reveal_type(flag.__xor__(**keywords), expected_text="Flags") + reveal_type(flag.__and__(**keywords), expected_text="Flags") + reveal_type(flag.__ror__(*positional), expected_text="Flags") + reveal_type(flag.__rxor__(*positional), expected_text="Flags") + reveal_type(flag.__rand__(*positional), expected_text="Flags") + operation = flag.__or__ + reveal_type(operation(**keywords), expected_text="Flags") + return operation(*positional) + + +def concrete(keywords: FullArgs, positional: tuple[Flags, Literal[0, 3]]) -> Flags: + reveal_type(Flags.__or__(**keywords), expected_text="Flags") + reveal_type(Flags.__xor__(**keywords), expected_text="Flags") + reveal_type(Flags.__and__(**keywords), expected_text="Flags") + reveal_type(Flags.__ror__(*positional), expected_text="Flags") + reveal_type(Flags.__rxor__(*positional), expected_text="Flags") + reveal_type(Flags.__rand__(*positional), expected_text="Flags") + return Flags.__or__(**keywords) + + +def unbound(keywords: FullArgs, positional: tuple[Flags, Literal[0, 3]]) -> Flags: + reveal_type(IntFlag.__or__(**keywords), expected_text="Flags") + reveal_type(IntFlag.__xor__(**keywords), expected_text="Flags") + reveal_type(IntFlag.__and__(**keywords), expected_text="Flags") + reveal_type(IntFlag.__ror__(*positional), expected_text="Flags") + reveal_type(IntFlag.__rxor__(*positional), expected_text="Flags") + reveal_type(IntFlag.__rand__(*positional), expected_text="Flags") + return IntFlag.__or__(*positional) + + +def mixed(flag: Flags, keywords: OtherArgs, positional: tuple[Literal[0, 3]]) -> Flags: + reveal_type(IntFlag.__or__(flag, *positional), expected_text="Flags") + reveal_type(IntFlag.__xor__(*(flag,), **keywords), expected_text="Flags") + reveal_type(IntFlag.__and__(*(flag,), other=3), expected_text="Flags") + return IntFlag.__or__(other=3, self=flag) + + +def correlated(positional: tuple[Flags, Literal[0]] | tuple[Keep, int]) -> Flags | Keep: + reveal_type(IntFlag.__or__(*positional), expected_text="Flags | Keep") + reveal_type(IntFlag.__xor__(*positional), expected_text="Flags | Keep") + reveal_type(IntFlag.__and__(*positional), expected_text="Flags | Keep") + return IntFlag.__or__(*positional) + + +FlagT = TypeVar("FlagT", bound=Flags) + + +def generic(flag: FlagT, positional: tuple[FlagT, Literal[0]]) -> FlagT: + reveal_type(flag.__or__(*(3,)), expected_text="FlagT@generic") + reveal_type(IntFlag.__or__(*positional), expected_text="FlagT@generic") + return IntFlag.__or__(*positional) + + +def negative(flag: Negative) -> Negative: + reveal_type(flag.__or__(*(0,)), expected_text="Negative") + reveal_type(Negative.__xor__(*(flag, 0)), expected_text="Negative") + reveal_type(IntFlag.__and__(*(flag, 0)), expected_text="Negative") + return flag.__xor__(*(0,)) + + +def boolean(flag: Flags, values: tuple[bool]) -> Flags: + return flag.__or__(*values) + + +def empty(flag: Flags, value: Never) -> Flags: + return flag.__or__(value) + + +def invalid_bound(flag: Flags, keywords: IntArgs, positional: tuple[int]): + # Each should generate an error: arbitrary operands may eject. + flag.__or__(**keywords).name + flag.__xor__(*positional).name + + +def invalid_unbound(keywords: FullIntArgs, positional: tuple[Flags, int]): + # Each should generate an error: binding arguments does not prove safe bits. + IntFlag.__or__(**keywords).name + IntFlag.__xor__(*positional).name diff --git a/packages/pyright-internal/src/tests/samples/enum20.py b/packages/pyright-internal/src/tests/samples/enum20.py new file mode 100644 index 000000000000..93fd789ac4cc --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/enum20.py @@ -0,0 +1,107 @@ +# Numeric values only prove EJECT closure when the operand operation is standard. + +from enum import EJECT, IntEnum, IntFlag +from typing import Callable, Literal, TypedDict + + +class Flags(IntFlag, boundary=EJECT): + A = 1 + B = 2 + + +class TwistedInt(int): + def __rand__(self, other: int, /) -> int: + return 8 + + +class TwistedEnum(IntEnum): + ZERO = 0 + + def __ror__(self, other: int, /) -> int: + return 8 + + def __rxor__(self, other: int, /) -> int: + return 8 + + def __rand__(self, other: int, /) -> int: + return 8 + + +class Integers(IntEnum): + ZERO = 0 + THREE = 3 + NEGATIVE = -1 + + +class AliasedIntegers(IntEnum): + ZERO = 0 + THREE = 3 + __ror__ = int.__or__ + __rxor__ = int.__rxor__ + __rand__ = int.__and__ + + +class Operand(TypedDict): + other: Literal[TwistedEnum.ZERO] + + +def unsafe(value: int, flag: Flags, arguments: Operand): + reveal_type(flag.__and__(value), expected_text="int") + reveal_type(flag & TwistedInt(0), expected_text="int") + reveal_type(flag | TwistedEnum.ZERO, expected_text="int") + reveal_type(flag ^ TwistedEnum.ZERO, expected_text="int") + reveal_type(flag & TwistedEnum.ZERO, expected_text="int") + # Each should generate an error: a valid reflected method can introduce bit8. + flag.__and__(value).name + flag.__or__(TwistedEnum.ZERO).name + IntFlag.__xor__(flag, TwistedEnum.ZERO).name + flag.__and__(**arguments).name + + +def unsafe_int_callback(flag: Flags) -> Callable[[int], Flags]: + # This should generate an error: int includes subclasses with custom reflection. + return flag.__and__ + + +def unsafe_enum_callback(flag: Flags) -> Callable[[TwistedEnum], Flags]: + # This should generate an error: finite values alone do not prove closure. + return flag.__or__ + + +def unsafe_literal_callback(flag: Flags) -> Callable[[Literal[TwistedEnum.ZERO]], Flags]: + # This should generate an error: a literal enum retains its reflected methods. + return flag.__xor__ + + +def unsafe_unbound_callback() -> Callable[[Flags, TwistedInt], Flags]: + # This should generate an error: unbound methods use the same operand proof. + return IntFlag.__and__ + + +def safe(flag: Flags, value: Integers, alias: AliasedIntegers, boolean: bool): + reveal_type(flag & value, expected_text="Flags") + reveal_type(flag | Integers.THREE, expected_text="Flags") + reveal_type(flag ^ Integers.ZERO, expected_text="Flags") + reveal_type(flag & alias, expected_text="Flags") + reveal_type(flag | alias, expected_text="Flags") + reveal_type(flag ^ alias, expected_text="Flags") + reveal_type(flag & boolean, expected_text="Flags") + reveal_type(flag & -8, expected_text="Flags") + reveal_type(flag & 8, expected_text="Flags") + reveal_type(flag & flag, expected_text="Flags") + + +def safe_boolean_callback(flag: Flags) -> Callable[[bool], Flags]: + return flag.__and__ + + +def safe_enum_callback(flag: Flags) -> Callable[[Integers], Flags]: + return flag.__and__ + + +def safe_literal_callback(flag: Flags) -> Callable[[Literal[-8, 0, 8]], Flags]: + return flag.__and__ + + +def safe_alias_callback(flag: Flags) -> Callable[[AliasedIntegers], Flags]: + return flag.__or__ diff --git a/packages/pyright-internal/src/tests/samples/enum21.py b/packages/pyright-internal/src/tests/samples/enum21.py new file mode 100644 index 000000000000..2e0335f7426e --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/enum21.py @@ -0,0 +1,92 @@ +# Inherited value storage can change both the flag mask and enum alias payloads. + +from enum import EJECT, IntEnum, IntFlag +from typing import Callable, Literal + + +class ValueMixin: + _value_: int = 8 + + +class StoredFlag(ValueMixin, IntFlag, boundary=EJECT): + A = 1 + + +class StoredValues(ValueMixin, IntEnum): + ONE = 1 + TWO = 2 + + +class OnlyTwo(IntFlag, boundary=EJECT): + TWO = 2 + + +def stored_name(flag: StoredFlag) -> str | None: + # This should generate an error: the actual value is 8 and the operation ejects 9. + return flag.__or__(1).name + + +def stored_callback(flag: StoredFlag) -> Callable[[Literal[1]], StoredFlag]: + # This should generate an error: custom storage invalidates assigned-value proofs. + return flag.__or__ + + +def operand_name(flag: OnlyTwo) -> str | None: + # This should generate an error: TWO aliases ONE at runtime and introduces bit 1. + return flag.__or__(StoredValues.TWO).name + + +def operand_callback(flag: OnlyTwo) -> Callable[[Literal[StoredValues.TWO]], OnlyTwo]: + # This should generate an error: enum literal values require standard storage too. + return flag.__or__ + + +class NumericValue(int): + def __and__(self, other: int, /) -> int: + return 9 + + def __xor__(self, other: int, /) -> int: + return 9 + + +class NumericStorage: + _value_: int = NumericValue(8) + + +class NumericFlag(NumericStorage, IntFlag, boundary=EJECT): + A = 1 + + +def stored_and_name(flag: NumericFlag) -> str | None: + # This should generate an error: int storage may have custom forward operations. + return flag.__and__(8).name + + +def stored_xor_name(flag: NumericFlag) -> str | None: + # This should generate an error: the stored operation ejects 9. + return flag.__xor__(1).name + + +def stored_and_callback(flag: NumericFlag) -> Callable[[Literal[8]], NumericFlag]: + # This should generate an error: a literal operand cannot repair custom storage. + return flag.__and__ + + +def stored_xor_callback(flag: NumericFlag) -> Callable[[Literal[1]], NumericFlag]: + # This should generate an error: callback proofs require standard storage too. + return flag.__xor__ + + +class OrdinaryMixin: + description: str = "ordinary" + + +class OrdinaryFlag(OrdinaryMixin, IntFlag, boundary=EJECT): + A = 1 + B = 2 + + +def ordinary(flag: OrdinaryFlag) -> Callable[[Literal[3]], OrdinaryFlag]: + reveal_type(flag | 3, expected_text="OrdinaryFlag") + reveal_type(flag & 8, expected_text="OrdinaryFlag") + return flag.__or__ diff --git a/packages/pyright-internal/src/tests/samples/enum22.py b/packages/pyright-internal/src/tests/samples/enum22.py new file mode 100644 index 000000000000..ec607101d669 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/enum22.py @@ -0,0 +1,164 @@ +from enum import EJECT, IntEnum, IntFlag +from typing import Callable, Literal, TypedDict + + +class Intercepted(IntFlag, boundary=EJECT): + A = 1 + + def __getattribute__(self, name: str) -> object: + return 8 if name == "_value_" else super().__getattribute__(name) + + +class AccessMixin: + def __getattribute__(self, name: str) -> object: + return 8 if name == "_value_" else super().__getattribute__(name) + + +class Inherited(AccessMixin, IntFlag, boundary=EJECT): + A = 1 + + +class InterceptedNegative(AccessMixin, IntFlag, boundary=EJECT): + A = -1 + + +class OtherArgs(TypedDict): + other: Literal[1] + + +def intercepted(): + reveal_type(1 | Intercepted.A, expected_text="int") + reveal_type(1 ^ Intercepted.A, expected_text="int") + reveal_type(Intercepted.A | 1, expected_text="int") + reveal_type(Intercepted.A.__or__(1), expected_text="int") + reveal_type(Intercepted.__or__(Intercepted.A, 1), expected_text="int") + reveal_type(Intercepted.A & 8, expected_text="int") + # These should generate errors: the operation reads 8 rather than the assigned 1. + (1 | Intercepted.A).name + (1 ^ Intercepted.A).name + (Intercepted.A | 1).name + Intercepted.A.__or__(1).name + Intercepted.__or__(Intercepted.A, 1).name + (Intercepted.A & 8).name + bound = Intercepted.A.__or__ + bound(1).name + arguments: OtherArgs = {"other": 1} + bound(**arguments).name + + +def inherited(): + reveal_type(1 | Inherited.A, expected_text="int") + reveal_type(1 ^ Inherited.A, expected_text="int") + # These should generate errors: inherited access interception is equally unsafe. + (1 | Inherited.A).name + (1 ^ Inherited.A).name + Inherited.A.__xor__(1).name + (InterceptedNegative.A | 1).name + + +def intercepted_callback() -> Callable[[Literal[1]], Intercepted]: + # This should generate an error: the callback returns int(9). + return Intercepted.A.__or__ + + +def inherited_callback() -> Callable[[Literal[1]], Inherited]: + # This should generate an error: the inherited callback returns int(9). + return Inherited.A.__xor__ + + +class OrdinaryMixin: + description = "ordinary" + + def __getattr__(self, name: str) -> object: + raise AttributeError(name) + + +class Ordinary(OrdinaryMixin, IntFlag, boundary=EJECT): + A = 1 + + +def ordinary() -> Callable[[Literal[1]], Ordinary]: + reveal_type(1 | Ordinary.A, expected_text="Ordinary") + reveal_type(Ordinary.A.__or__(1), expected_text="Ordinary") + reveal_type((1 | Ordinary.A).name, expected_text="str | None") + # This should generate an error: ordinary storage does not make bit 8 valid. + (Ordinary.A | 8).name + return Ordinary.A.__or__ + + +class PayloadEnum(AccessMixin, IntEnum): + TWO = 2 + + +class OnlyTwo(IntFlag, boundary=EJECT): + TWO = 2 + + +def numeric_payload() -> Callable[[Literal[PayloadEnum.TWO]], OnlyTwo]: + # Builtin int reflection uses the numeric payload, not intercepted attribute access. + reveal_type(OnlyTwo.TWO | PayloadEnum.TWO, expected_text="OnlyTwo") + reveal_type((OnlyTwo.TWO | PayloadEnum.TWO).name, expected_text="str | None") + return OnlyTwo.TWO.__or__ + + +class Negative(IntFlag, boundary=EJECT): + A = -1 + + +def negative_operator() -> Negative: + reveal_type(Negative.A | 1, expected_text="Negative") + reveal_type(1 | Negative.A, expected_text="Negative") + reveal_type(Negative.A | Negative.A, expected_text="Negative") + reveal_type(Negative.A & -1, expected_text="Negative") + reveal_type(Negative.A ^ 0, expected_text="Negative") + reveal_type(Negative.A ^ 1, expected_text="int") + return Negative.A | 1 + + +def negative_bound() -> Negative: + bound = Negative.A.__or__ + reveal_type(bound(1), expected_text="Negative") + reveal_type(Negative.A.__or__(1), expected_text="Negative") + return bound(1) + + +def negative_unbound() -> Negative: + reveal_type(Negative.__or__(Negative.A, 1), expected_text="Negative") + return Negative.__or__(Negative.A, 1) + + +def negative_callback() -> Callable[[Literal[1]], Negative]: + return Negative.A.__or__ + + +def negative_zero_callback() -> Callable[[Literal[0]], Negative]: + return Negative.A.__xor__ + + +def negative_member_callback() -> Callable[[Literal[Negative.A]], Negative]: + return Negative.A.__or__ + + +def negative_packed() -> Negative: + arguments: tuple[Literal[Negative.A], Literal[1]] = (Negative.A, 1) + return Negative.__or__(*arguments) + + +def negative_xor() -> Negative: + # This should generate an error: -1 ^ 1 is the unknown value -2 and ejects. + return Negative.A ^ 1 + + +def negative_direct_xor() -> Negative: + # This should generate an error for the same operation called directly. + return Negative.A.__xor__(1) + + +def negative_xor_callback() -> Callable[[Literal[1]], Negative]: + # This should generate an error: a known member does not make XOR1 safe. + return Negative.A.__xor__ + + +def negative_general(value: int): + reveal_type(Negative.A | value, expected_text="int") + reveal_type(Negative.A & value, expected_text="int") diff --git a/packages/pyright-internal/src/tests/samples/operator13.py b/packages/pyright-internal/src/tests/samples/operator13.py new file mode 100644 index 000000000000..7f387565b56a --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/operator13.py @@ -0,0 +1,203 @@ +# This sample tests reflected arithmetic precedence without changing ordinary +# operator fallback or the meaning of direct dunder calls. + +from enum import IntFlag, auto +from types import NotImplementedType +from typing import Generic, TypeVar + + +class Flags(IntFlag): + A = auto() + B = auto() + + +def flags_check(value: int, flag: Flags): + reveal_type(value & flag, expected_text="Flags") + reveal_type(value | flag, expected_text="Flags") + reveal_type(value ^ flag, expected_text="Flags") + reveal_type(flag & value, expected_text="Flags") + reveal_type(flag | value, expected_text="Flags") + reveal_type(flag ^ value, expected_text="Flags") + reveal_type(value & Flags.A, expected_text="Flags") + reveal_type(value | Flags.A, expected_text="Flags") + reveal_type(value ^ Flags.A, expected_text="Flags") + reveal_type(value & int(), expected_text="int") + reveal_type(value | int(), expected_text="int") + reveal_type(value ^ int(), expected_text="int") + reveal_type(value.__and__(flag), expected_text="int") + reveal_type(flag.__rand__(value), expected_text="Flags") + + a = value + a &= flag + reveal_type(a, expected_text="Flags") + b = value + b |= flag + reveal_type(b, expected_text="Flags") + c = value + c ^= flag + reveal_type(c, expected_text="Flags") + + flag &= value + reveal_type(flag, expected_text="Flags") + flag |= value + reveal_type(flag, expected_text="Flags") + flag ^= value + reveal_type(flag, expected_text="Flags") + + +class Base: + def __add__(self, other: object) -> str: + return "forward" + + def __radd__(self, other: object) -> object: + return "base reflected" + + +class Override(Base): + def __radd__(self, other: object) -> int: + return 1 + + +class Inherited(Base): + pass + + +class Leaf(Override): + pass + + +class Alias(Base): + __radd__ = Base.__radd__ + + +class NewAlias(Base): + def reflect(self, other: object) -> int: + return 1 + + __radd__ = reflect + + +class Declines(Base): + def __radd__(self, other: object) -> NotImplementedType: + return NotImplemented + + +class MaybeDeclines(Base): + def __radd__(self, other: object) -> int | NotImplementedType: + return 1 if bool() else NotImplemented + + +class InferredDecline(Base): + def __radd__(self, other: object): + return NotImplemented + + +class Unrelated: + def __radd__(self, other: object) -> int: + return 1 + + +class InPlace(Base): + def __iadd__(self, other: object) -> float: + return 1.0 + + +class ForwardDeclines: + def __add__(self, other: object) -> NotImplementedType: + return NotImplemented + + def __radd__(self, other: object) -> object: + return 0 + + +class PartialOverride(ForwardDeclines): + def __radd__(self, other: object) -> int | NotImplementedType: + return 1 if bool() else NotImplemented + + +def partial_check(base: ForwardDeclines, child: PartialOverride): + reveal_type(base + child, expected_text="int") + + +class FullOverride(ForwardDeclines): + def __radd__(self, other: object) -> NotImplementedType: + return NotImplemented + + +def full_decline_check(base: ForwardDeclines, child: FullOverride): + # This should generate an error because both implementations decline. + _ = base + child + + +def precedence_check(base: Base, override: Override): + reveal_type(base + override, expected_text="int") + reveal_type(base + Inherited(), expected_text="str") + reveal_type(base + Leaf(), expected_text="int") + reveal_type(base + Alias(), expected_text="str") + reveal_type(base + NewAlias(), expected_text="int") + reveal_type(base + Declines(), expected_text="str") + reveal_type(base + MaybeDeclines(), expected_text="int | str") + reveal_type(base + InferredDecline(), expected_text="str") + reveal_type(base + Unrelated(), expected_text="str") + reveal_type(override + base, expected_text="str") + reveal_type(override + override, expected_text="str") + reveal_type(base.__add__(override), expected_text="str") + reveal_type(Declines().__radd__(base), expected_text="NotImplementedType") + + inplace = InPlace() + inplace += override + reveal_type(inplace, expected_text="float") + + +T = TypeVar("T") +FlagT = TypeVar("FlagT", bound=Flags) + + +def generic_flags_check(value: int, flag: FlagT) -> FlagT: + reveal_type(value & flag, expected_text="FlagT@generic_flags_check") + reveal_type(value | flag, expected_text="FlagT@generic_flags_check") + reveal_type(value ^ flag, expected_text="FlagT@generic_flags_check") + return value & flag + + +class Series(Generic[T]): + def __add__(self, other: T) -> "Series[T]": + return self + + def __radd__(self, other: "Series[T]") -> "Series[T]": + return self + + +def generic_check(series: Series[int]): + reveal_type(series + series, expected_text="Series[int]") + + +class GenericBase(Generic[T]): + def __init__(self, value: T): + self.value = value + + def __add__(self, other: object) -> str: + return "forward" + + def __radd__(self, other: object) -> T: + return self.value + + +class GenericChild(GenericBase[int]): + pass + + +def generic_inheritance_check(base: GenericBase[str], child: GenericChild): + reveal_type(base + child, expected_text="str") + + +def resize(size: list[int], short: int, long: int): + requested = size if isinstance(size, int) else size[0] + reveal_type(long * requested, expected_text="int") + return int(long * requested / short) + + +def padding_check(padding: tuple[int, int], limit: int): + py, px = (padding, padding) if isinstance(padding, int) else padding + reveal_type(2 * py, expected_text="int") + return 0 <= 2 * py <= limit and 0 <= 2 * px <= limit diff --git a/packages/pyright-internal/src/tests/samples/operator14.py b/packages/pyright-internal/src/tests/samples/operator14.py new file mode 100644 index 000000000000..c25077326389 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/operator14.py @@ -0,0 +1,69 @@ +# A nonreturning forward operation is not a NotImplemented decline. + +from types import NotImplementedType +from typing import Never, NoReturn + + +class Base: + def __add__(self, other: object) -> Never: + raise RuntimeError() + + def __radd__(self, other: object) -> object: + return 0 + + +class Declines(Base): + def __radd__(self, other: object) -> NotImplementedType: + return NotImplemented + + +class Partial(Base): + def __radd__(self, other: object) -> int | NotImplementedType: + return 1 if bool() else NotImplemented + + +class ReflectedNever(Base): + def __radd__(self, other: object) -> Never: + raise RuntimeError() + + +class Inherited(Base): + pass + + +def exact_check(base: Base, child: Declines) -> list[Never]: + return [base + child] + + +def reveal_check(base: Base, child: Declines): + reveal_type([base + child], expected_text="list[Never]") + + +def controls(base: Base, child: Inherited): + reveal_type([base.__add__(child)], expected_text="list[Never]") + reveal_type([base + child], expected_text="list[Never]") + + +def partial_check(base: Base, child: Partial): + reveal_type([base + child], expected_text="list[int]") + + +def reflected_check(base: Base, child: ReflectedNever): + reveal_type([base + child], expected_text="list[Never]") + + +class NoReturnBase: + def __add__(self, other: object) -> NoReturn: + raise RuntimeError() + + def __radd__(self, other: object) -> object: + return 0 + + +class NoReturnChild(NoReturnBase): + def __radd__(self, other: object) -> NotImplementedType: + return NotImplemented + + +def noreturn_check(base: NoReturnBase, child: NoReturnChild) -> list[NoReturn]: + return [base + child] diff --git a/packages/pyright-internal/src/tests/typeEvaluator8.test.ts b/packages/pyright-internal/src/tests/typeEvaluator8.test.ts index a1ec1ae7cfa0..983c3d5174e1 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator8.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator8.test.ts @@ -490,6 +490,80 @@ test('Operator12', () => { TestUtils.validateResults(analysisResults, 0); }); +test('Operator13', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['operator13.py']); + + TestUtils.validateResults(analysisResults, 1); + assert.strictEqual(analysisResults[0].errors[0].getRule(), DiagnosticRule.reportOperatorIssue); +}); + +test('Operator14', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['operator14.py']); + + TestUtils.validateResults(analysisResults, 0); +}); + +test('Enum17', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['enum17.py']); + + TestUtils.validateResults(analysisResults, 9); + analysisResults[0].errors.forEach((error) => + assert.strictEqual(error.getRule(), DiagnosticRule.reportAttributeAccessIssue) + ); +}); + +test('Enum18', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['enum18.py']); + + TestUtils.validateResults(analysisResults, 7); + analysisResults[0].errors.forEach((error) => assert.strictEqual(error.getRule(), DiagnosticRule.reportReturnType)); +}); + +test('Enum19', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['enum19.py']); + + TestUtils.validateResults(analysisResults, 4); + analysisResults[0].errors.forEach((error) => + assert.strictEqual(error.getRule(), DiagnosticRule.reportAttributeAccessIssue) + ); +}); + +test('Enum20', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['enum20.py']); + + TestUtils.validateResults(analysisResults, 8); + analysisResults[0].errors.forEach((error) => + assert.ok( + error.getRule() === DiagnosticRule.reportAttributeAccessIssue || + error.getRule() === DiagnosticRule.reportReturnType + ) + ); +}); + +test('Enum21', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['enum21.py']); + + TestUtils.validateResults(analysisResults, 8); + analysisResults[0].errors.forEach((error) => + assert.ok( + error.getRule() === DiagnosticRule.reportAttributeAccessIssue || + error.getRule() === DiagnosticRule.reportReturnType + ) + ); +}); + +test('Enum22', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['enum22.py']); + + TestUtils.validateResults(analysisResults, 18); + analysisResults[0].errors.forEach((error) => + assert.ok( + error.getRule() === DiagnosticRule.reportAttributeAccessIssue || + error.getRule() === DiagnosticRule.reportReturnType + ) + ); +}); + test('Optional1', () => { const configOptions = new ConfigOptions(Uri.empty());