Shortened names of EvalFlags to improve readability. No functional change.

This commit is contained in:
Eric Traut 2024-07-02 13:38:58 -07:00
parent 33a72e3d6a
commit 280ed52e8c
9 changed files with 395 additions and 476 deletions

View File

@ -41,7 +41,7 @@ import { isMatchingExpression, isPartialMatchingExpression, printExpression } fr
import { getPatternSubtypeNarrowingCallback } from './patternMatching'; import { getPatternSubtypeNarrowingCallback } from './patternMatching';
import { SpeculativeTypeTracker } from './typeCacheUtils'; import { SpeculativeTypeTracker } from './typeCacheUtils';
import { narrowForKeyAssignment } from './typedDicts'; import { narrowForKeyAssignment } from './typedDicts';
import { EvaluatorFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { EvalFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import { getTypeNarrowingCallback } from './typeGuards'; import { getTypeNarrowingCallback } from './typeGuards';
import { import {
ClassType, ClassType,
@ -1485,7 +1485,7 @@ export function getCodeFlowEngine(
const classType = evaluator.getTypeOfExpression( const classType = evaluator.getTypeOfExpression(
classPatternNode.className, classPatternNode.className,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
).type; ).type;
if (isInstantiableClass(classType)) { if (isInstantiableClass(classType)) {
@ -1534,12 +1534,12 @@ export function getCodeFlowEngine(
const arg1Expr = testExpression.arguments[1].valueExpression; const arg1Expr = testExpression.arguments[1].valueExpression;
const arg1Type = evaluator.getTypeOfExpression( const arg1Type = evaluator.getTypeOfExpression(
arg1Expr, arg1Expr,
EvaluatorFlags.AllowMissingTypeArgs | EvalFlags.AllowMissingTypeArgs |
EvaluatorFlags.EvaluateStringLiteralAsType | EvalFlags.StrLiteralAsType |
EvaluatorFlags.DisallowParamSpec | EvalFlags.NoParamSpec |
EvaluatorFlags.DisallowTypeVarTuple | EvalFlags.NoTypeVarTuple |
EvaluatorFlags.DisallowFinal | EvalFlags.NoFinal |
EvaluatorFlags.DoNotSpecialize EvalFlags.NoSpecialize
).type; ).type;
if (isInstantiableClass(arg1Type)) { if (isInstantiableClass(arg1Type)) {
@ -1659,7 +1659,7 @@ export function getCodeFlowEngine(
let subtypeCount = 0; let subtypeCount = 0;
// Evaluate the call base type. // Evaluate the call base type.
const callTypeResult = evaluator.getTypeOfExpression(node.leftExpression, EvaluatorFlags.CallBaseDefaults); const callTypeResult = evaluator.getTypeOfExpression(node.leftExpression, EvalFlags.CallBaseDefaults);
const callType = callTypeResult.type; const callType = callTypeResult.type;
doForEachSubtype(callType, (callSubtype) => { doForEachSubtype(callType, (callSubtype) => {

View File

@ -33,7 +33,7 @@ import { getClassFullName, getEnclosingClassOrFunction, getScopeIdForNode, getTy
import { evaluateStaticBoolExpression } from './staticExpressions'; import { evaluateStaticBoolExpression } from './staticExpressions';
import { Symbol, SymbolFlags } from './symbol'; import { Symbol, SymbolFlags } from './symbol';
import { isPrivateName } from './symbolNameUtils'; import { isPrivateName } from './symbolNameUtils';
import { EvaluatorFlags, FunctionArgument, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { EvalFlags, FunctionArgument, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import { import {
AnyType, AnyType,
ClassType, ClassType,
@ -225,7 +225,7 @@ export function synthesizeDataClassMethods(
if (statement.rightExpression.nodeType === ParseNodeType.Call) { if (statement.rightExpression.nodeType === ParseNodeType.Call) {
const callTypeResult = evaluator.getTypeOfExpression( const callTypeResult = evaluator.getTypeOfExpression(
statement.rightExpression.leftExpression, statement.rightExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const callType = callTypeResult.type; const callType = callTypeResult.type;
@ -458,7 +458,7 @@ export function synthesizeDataClassMethods(
if (statement.rightExpression.nodeType === ParseNodeType.Call) { if (statement.rightExpression.nodeType === ParseNodeType.Call) {
const callType = evaluator.getTypeOfExpression( const callType = evaluator.getTypeOfExpression(
statement.rightExpression.leftExpression, statement.rightExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
).type; ).type;
if ( if (

View File

@ -33,7 +33,7 @@ import {
createProperty, createProperty,
validatePropertyMethod, validatePropertyMethod,
} from './properties'; } from './properties';
import { EvaluatorFlags, FunctionArgument, TypeEvaluator } from './typeEvaluatorTypes'; import { EvalFlags, FunctionArgument, TypeEvaluator } from './typeEvaluatorTypes';
import { isPartlyUnknown, isProperty } from './typeUtils'; import { isPartlyUnknown, isProperty } from './typeUtils';
import { import {
ClassType, ClassType,
@ -85,9 +85,9 @@ export function getFunctionInfoFromDecorators(
for (const decoratorNode of node.decorators) { for (const decoratorNode of node.decorators) {
// Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators. // Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators.
let evaluatorFlags = fileInfo.isStubFile ? EvaluatorFlags.AllowForwardReferences : EvaluatorFlags.None; let evaluatorFlags = fileInfo.isStubFile ? EvalFlags.ForwardRefs : EvalFlags.None;
if (decoratorNode.expression.nodeType !== ParseNodeType.Call) { if (decoratorNode.expression.nodeType !== ParseNodeType.Call) {
evaluatorFlags |= EvaluatorFlags.CallBaseDefaults; evaluatorFlags |= EvalFlags.CallBaseDefaults;
} }
const decoratorTypeResult = evaluator.getTypeOfExpression(decoratorNode.expression, evaluatorFlags); const decoratorTypeResult = evaluator.getTypeOfExpression(decoratorNode.expression, evaluatorFlags);
@ -143,9 +143,9 @@ export function applyFunctionDecorator(
const fileInfo = getFileInfo(decoratorNode); const fileInfo = getFileInfo(decoratorNode);
// Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators. // Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators.
let evaluatorFlags = fileInfo.isStubFile ? EvaluatorFlags.AllowForwardReferences : EvaluatorFlags.None; let evaluatorFlags = fileInfo.isStubFile ? EvalFlags.ForwardRefs : EvalFlags.None;
if (decoratorNode.expression.nodeType !== ParseNodeType.Call) { if (decoratorNode.expression.nodeType !== ParseNodeType.Call) {
evaluatorFlags |= EvaluatorFlags.CallBaseDefaults; evaluatorFlags |= EvalFlags.CallBaseDefaults;
} }
const decoratorTypeResult = evaluator.getTypeOfExpression(decoratorNode.expression, evaluatorFlags); const decoratorTypeResult = evaluator.getTypeOfExpression(decoratorNode.expression, evaluatorFlags);
@ -167,7 +167,7 @@ export function applyFunctionDecorator(
if (decoratorNode.expression.nodeType === ParseNodeType.Call) { if (decoratorNode.expression.nodeType === ParseNodeType.Call) {
const decoratorCallType = evaluator.getTypeOfExpression( const decoratorCallType = evaluator.getTypeOfExpression(
decoratorNode.expression.leftExpression, decoratorNode.expression.leftExpression,
evaluatorFlags | EvaluatorFlags.CallBaseDefaults evaluatorFlags | EvalFlags.CallBaseDefaults
).type; ).type;
if (isFunction(decoratorCallType)) { if (isFunction(decoratorCallType)) {
@ -201,7 +201,7 @@ export function applyFunctionDecorator(
if (decoratorNode.expression.nodeType === ParseNodeType.MemberAccess) { if (decoratorNode.expression.nodeType === ParseNodeType.MemberAccess) {
const baseType = evaluator.getTypeOfExpression( const baseType = evaluator.getTypeOfExpression(
decoratorNode.expression.leftExpression, decoratorNode.expression.leftExpression,
evaluatorFlags | EvaluatorFlags.MemberAccessBaseDefaults evaluatorFlags | EvalFlags.MemberAccessBaseDefaults
).type; ).type;
if (isProperty(baseType)) { if (isProperty(baseType)) {
@ -298,16 +298,16 @@ export function applyClassDecorator(
decoratorNode: DecoratorNode decoratorNode: DecoratorNode
): Type { ): Type {
const fileInfo = getFileInfo(decoratorNode); const fileInfo = getFileInfo(decoratorNode);
let flags = fileInfo.isStubFile ? EvaluatorFlags.AllowForwardReferences : EvaluatorFlags.None; let flags = fileInfo.isStubFile ? EvalFlags.ForwardRefs : EvalFlags.None;
if (decoratorNode.expression.nodeType !== ParseNodeType.Call) { if (decoratorNode.expression.nodeType !== ParseNodeType.Call) {
flags |= EvaluatorFlags.CallBaseDefaults; flags |= EvalFlags.CallBaseDefaults;
} }
const decoratorType = evaluator.getTypeOfExpression(decoratorNode.expression, flags).type; const decoratorType = evaluator.getTypeOfExpression(decoratorNode.expression, flags).type;
if (decoratorNode.expression.nodeType === ParseNodeType.Call) { if (decoratorNode.expression.nodeType === ParseNodeType.Call) {
const decoratorCallType = evaluator.getTypeOfExpression( const decoratorCallType = evaluator.getTypeOfExpression(
decoratorNode.expression.leftExpression, decoratorNode.expression.leftExpression,
flags | EvaluatorFlags.CallBaseDefaults flags | EvalFlags.CallBaseDefaults
).type; ).type;
if (isFunction(decoratorCallType)) { if (isFunction(decoratorCallType)) {
@ -367,7 +367,7 @@ export function applyClassDecorator(
callNode = decoratorNode.expression; callNode = decoratorNode.expression;
const decoratorCallType = evaluator.getTypeOfExpression( const decoratorCallType = evaluator.getTypeOfExpression(
callNode.leftExpression, callNode.leftExpression,
flags | EvaluatorFlags.CallBaseDefaults flags | EvalFlags.CallBaseDefaults
).type; ).type;
dataclassBehaviors = getDataclassDecoratorBehaviors(decoratorCallType); dataclassBehaviors = getDataclassDecoratorBehaviors(decoratorCallType);
} else { } else {
@ -391,9 +391,9 @@ export function applyClassDecorator(
function getTypeOfDecorator(evaluator: TypeEvaluator, node: DecoratorNode, functionOrClassType: Type): Type { function getTypeOfDecorator(evaluator: TypeEvaluator, node: DecoratorNode, functionOrClassType: Type): Type {
// Evaluate the type of the decorator expression. // Evaluate the type of the decorator expression.
let flags = getFileInfo(node).isStubFile ? EvaluatorFlags.AllowForwardReferences : EvaluatorFlags.None; let flags = getFileInfo(node).isStubFile ? EvalFlags.ForwardRefs : EvalFlags.None;
if (node.expression.nodeType !== ParseNodeType.Call) { if (node.expression.nodeType !== ParseNodeType.Call) {
flags |= EvaluatorFlags.CallBaseDefaults; flags |= EvalFlags.CallBaseDefaults;
} }
const decoratorTypeResult = evaluator.getTypeOfExpression(node.expression, flags); const decoratorTypeResult = evaluator.getTypeOfExpression(node.expression, flags);

View File

@ -14,7 +14,7 @@ import { VariableDeclaration } from './declaration';
import { getClassFullName, getEnclosingClass, getTypeSourceId } from './parseTreeUtils'; import { getClassFullName, getEnclosingClass, getTypeSourceId } from './parseTreeUtils';
import { Symbol, SymbolFlags } from './symbol'; import { Symbol, SymbolFlags } from './symbol';
import { isPrivateName, isSingleDunderName } from './symbolNameUtils'; import { isPrivateName, isSingleDunderName } from './symbolNameUtils';
import { EvaluatorFlags, FunctionArgument, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { EvalFlags, FunctionArgument, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import { enumerateLiteralsForType } from './typeGuards'; import { enumerateLiteralsForType } from './typeGuards';
import { MemberAccessFlags, computeMroLinearization, lookUpClassMember, makeInferenceContext } from './typeUtils'; import { MemberAccessFlags, computeMroLinearization, lookUpClassMember, makeInferenceContext } from './typeUtils';
import { import {
@ -369,7 +369,7 @@ export function transformTypeForEnumMember(
let assignedType: Type | undefined; let assignedType: Type | undefined;
if (valueTypeExprNode) { if (valueTypeExprNode) {
const evalFlags = getFileInfo(valueTypeExprNode).isStubFile ? EvaluatorFlags.ConvertEllipsisToAny : undefined; const evalFlags = getFileInfo(valueTypeExprNode).isStubFile ? EvalFlags.ConvertEllipsisToAny : undefined;
assignedType = evaluator.getTypeOfExpression(valueTypeExprNode, evalFlags).type; assignedType = evaluator.getTypeOfExpression(valueTypeExprNode, evalFlags).type;
} }

View File

@ -25,7 +25,7 @@ import { getFileInfo } from './analyzerNodeInfo';
import { getEnclosingLambda, isWithinLoop, operatorSupportsChaining, printOperator } from './parseTreeUtils'; import { getEnclosingLambda, isWithinLoop, operatorSupportsChaining, printOperator } from './parseTreeUtils';
import { getScopeForNode } from './scopeUtils'; import { getScopeForNode } from './scopeUtils';
import { evaluateStaticBoolExpression } from './staticExpressions'; import { evaluateStaticBoolExpression } from './staticExpressions';
import { EvaluatorFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { EvalFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import { import {
InferenceContext, InferenceContext,
convertToInstantiable, convertToInstantiable,
@ -501,7 +501,7 @@ export function validateBinaryOperation(
export function getTypeOfBinaryOperation( export function getTypeOfBinaryOperation(
evaluator: TypeEvaluator, evaluator: TypeEvaluator,
node: BinaryOperationNode, node: BinaryOperationNode,
flags: EvaluatorFlags, flags: EvalFlags,
inferenceContext: InferenceContext | undefined inferenceContext: InferenceContext | undefined
): TypeResult { ): TypeResult {
const leftExpression = node.leftExpression; const leftExpression = node.leftExpression;
@ -622,7 +622,7 @@ export function getTypeOfBinaryOperation(
const fileInfo = getFileInfo(node); const fileInfo = getFileInfo(node);
const unionNotationSupported = const unionNotationSupported =
fileInfo.isStubFile || fileInfo.isStubFile ||
(flags & EvaluatorFlags.AllowForwardReferences) !== 0 || (flags & EvalFlags.ForwardRefs) !== 0 ||
fileInfo.executionEnvironment.pythonVersion.isGreaterOrEqualTo(pythonVersion3_10); fileInfo.executionEnvironment.pythonVersion.isGreaterOrEqualTo(pythonVersion3_10);
if (!unionNotationSupported) { if (!unionNotationSupported) {
@ -648,12 +648,12 @@ export function getTypeOfBinaryOperation(
adjustedLeftType = evaluator.reportMissingTypeArguments( adjustedLeftType = evaluator.reportMissingTypeArguments(
node.leftExpression, node.leftExpression,
adjustedLeftType, adjustedLeftType,
flags | EvaluatorFlags.ExpectingInstantiableType flags | EvalFlags.InstantiableType
); );
adjustedRightType = evaluator.reportMissingTypeArguments( adjustedRightType = evaluator.reportMissingTypeArguments(
node.rightExpression, node.rightExpression,
adjustedRightType, adjustedRightType,
flags | EvaluatorFlags.ExpectingInstantiableType flags | EvalFlags.InstantiableType
); );
let newUnion = combineTypes([adjustedLeftType, adjustedRightType]); let newUnion = combineTypes([adjustedLeftType, adjustedRightType]);
@ -703,7 +703,7 @@ export function getTypeOfBinaryOperation(
} }
} }
if ((flags & EvaluatorFlags.ExpectingTypeExpression) !== 0) { if ((flags & EvalFlags.TypeExpression) !== 0) {
// Exempt "|" because it might be a union operation involving unknowns. // Exempt "|" because it might be a union operation involving unknowns.
if (node.operator !== OperatorType.BitwiseOr) { if (node.operator !== OperatorType.BitwiseOr) {
evaluator.addDiagnostic(DiagnosticRule.reportInvalidTypeForm, LocMessage.binaryOperationNotAllowed(), node); evaluator.addDiagnostic(DiagnosticRule.reportInvalidTypeForm, LocMessage.binaryOperationNotAllowed(), node);
@ -947,10 +947,10 @@ export function getTypeOfAugmentedAssignment(
export function getTypeOfUnaryOperation( export function getTypeOfUnaryOperation(
evaluator: TypeEvaluator, evaluator: TypeEvaluator,
node: UnaryOperationNode, node: UnaryOperationNode,
flags: EvaluatorFlags, flags: EvalFlags,
inferenceContext: InferenceContext | undefined inferenceContext: InferenceContext | undefined
): TypeResult { ): TypeResult {
if ((flags & EvaluatorFlags.ExpectingTypeExpression) !== 0) { if ((flags & EvalFlags.TypeExpression) !== 0) {
evaluator.addDiagnostic(DiagnosticRule.reportInvalidTypeForm, LocMessage.unaryOperationNotAllowed(), node); evaluator.addDiagnostic(DiagnosticRule.reportInvalidTypeForm, LocMessage.unaryOperationNotAllowed(), node);
return { type: UnknownType.create() }; return { type: UnknownType.create() };
} }
@ -1082,12 +1082,12 @@ export function getTypeOfUnaryOperation(
export function getTypeOfTernaryOperation( export function getTypeOfTernaryOperation(
evaluator: TypeEvaluator, evaluator: TypeEvaluator,
node: TernaryNode, node: TernaryNode,
flags: EvaluatorFlags, flags: EvalFlags,
inferenceContext: InferenceContext | undefined inferenceContext: InferenceContext | undefined
): TypeResult { ): TypeResult {
const fileInfo = getFileInfo(node); const fileInfo = getFileInfo(node);
if ((flags & EvaluatorFlags.ExpectingTypeExpression) !== 0) { if ((flags & EvalFlags.TypeExpression) !== 0) {
evaluator.addDiagnostic(DiagnosticRule.reportInvalidTypeForm, LocMessage.ternaryNotAllowed(), node); evaluator.addDiagnostic(DiagnosticRule.reportInvalidTypeForm, LocMessage.ternaryNotAllowed(), node);
return { type: UnknownType.create() }; return { type: UnknownType.create() };
} }

View File

@ -31,7 +31,7 @@ import {
import { CodeFlowReferenceExpressionNode } from './codeFlowTypes'; import { CodeFlowReferenceExpressionNode } from './codeFlowTypes';
import { addConstraintsForExpectedType } from './constraintSolver'; import { addConstraintsForExpectedType } from './constraintSolver';
import { getTypeVarScopesForNode, isMatchingExpression } from './parseTreeUtils'; import { getTypeVarScopesForNode, isMatchingExpression } from './parseTreeUtils';
import { EvaluatorFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { EvalFlags, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import { import {
enumerateLiteralsForType, enumerateLiteralsForType,
narrowTypeForDiscriminatedDictEntryComparison, narrowTypeForDiscriminatedDictEntryComparison,
@ -679,7 +679,7 @@ function narrowTypeBasedOnClassPattern(
pattern: PatternClassNode, pattern: PatternClassNode,
isPositiveTest: boolean isPositiveTest: boolean
): Type { ): Type {
let exprType = evaluator.getTypeOfExpression(pattern.className, EvaluatorFlags.CallBaseDefaults).type; let exprType = evaluator.getTypeOfExpression(pattern.className, EvalFlags.CallBaseDefaults).type;
// If this is a class (but not a type alias that refers to a class), // If this is a class (but not a type alias that refers to a class),
// specialize it with Unknown type arguments. // specialize it with Unknown type arguments.
@ -1829,7 +1829,7 @@ function wrapTypeInList(evaluator: TypeEvaluator, node: ParseNode, type: Type):
} }
export function validateClassPattern(evaluator: TypeEvaluator, pattern: PatternClassNode) { export function validateClassPattern(evaluator: TypeEvaluator, pattern: PatternClassNode) {
let exprType = evaluator.getTypeOfExpression(pattern.className, EvaluatorFlags.CallBaseDefaults).type; let exprType = evaluator.getTypeOfExpression(pattern.className, EvalFlags.CallBaseDefaults).type;
// If the expression is a type alias or other special form, treat it // If the expression is a type alias or other special form, treat it
// as the special form rather than the class. // as the special form rather than the class.
@ -1931,7 +1931,7 @@ export function getPatternSubtypeNarrowingCallback(
if (ClassType.isBuiltIn(indexType, ['int', 'str'])) { if (ClassType.isBuiltIn(indexType, ['int', 'str'])) {
const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression( const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression(
subjectExpression.baseExpression, subjectExpression.baseExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const unnarrowedReferenceType = unnarrowedReferenceTypeResult.type; const unnarrowedReferenceType = unnarrowedReferenceTypeResult.type;
@ -2027,7 +2027,7 @@ export function getPatternSubtypeNarrowingCallback(
) { ) {
const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression( const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression(
subjectExpression.leftExpression, subjectExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const unnarrowedReferenceType = unnarrowedReferenceTypeResult.type; const unnarrowedReferenceType = unnarrowedReferenceTypeResult.type;

File diff suppressed because it is too large Load Diff

View File

@ -56,7 +56,7 @@ import {
// a list) before the type is considered an "Any". // a list) before the type is considered an "Any".
export const maxSubtypesForInferredType = 64; export const maxSubtypesForInferredType = 64;
export const enum EvaluatorFlags { export const enum EvalFlags {
None = 0, None = 0,
// Interpret an ellipsis type annotation to mean "Any". // Interpret an ellipsis type annotation to mean "Any".
@ -65,106 +65,107 @@ export const enum EvaluatorFlags {
// Normally a generic named type is specialized with "Any" // Normally a generic named type is specialized with "Any"
// types. This flag indicates that specialization shouldn't take // types. This flag indicates that specialization shouldn't take
// place. // place.
DoNotSpecialize = 1 << 1, NoSpecialize = 1 << 1,
// Allow forward references. Don't report unbound errors. // Allow forward references. Don't report unbound errors.
AllowForwardReferences = 1 << 2, ForwardRefs = 1 << 2,
// Treat string literal as a type. // Treat string literal as a type.
EvaluateStringLiteralAsType = 1 << 3, StrLiteralAsType = 1 << 3,
// 'Final' is not allowed in this context. // 'Final' is not allowed in this context.
DisallowFinal = 1 << 4, NoFinal = 1 << 4,
// A ParamSpec isn't allowed in this context. // A ParamSpec isn't allowed in this context.
DisallowParamSpec = 1 << 5, NoParamSpec = 1 << 5,
// A TypeVarTuple isn't allowed in this context. // A TypeVarTuple isn't allowed in this context.
DisallowTypeVarTuple = 1 << 6, NoTypeVarTuple = 1 << 6,
// Expression is expected to be an instantiable type rather // Expression is expected to be an instantiable type rather
// than an instance (object) // than an instance (object)
ExpectingInstantiableType = 1 << 7, InstantiableType = 1 << 7,
// A type expression imposes grammatical and semantic limits on an // A type expression imposes grammatical and semantic limits on an
// expression. If this flag is set, illegal type expressions are // expression. If this flag is set, illegal type expressions are
// flagged as errors. // flagged as errors.
ExpectingTypeExpression = 1 << 8, TypeExpression = 1 << 8,
// Suppress the reportMissingTypeArgument diagnostic in this context. // Suppress the reportMissingTypeArgument diagnostic in this context.
AllowMissingTypeArgs = 1 << 9, AllowMissingTypeArgs = 1 << 9,
// The Generic class type is allowed in this context. It is // The Generic class type is allowed in this context. It is
// normally not allowed if ExpectingType is set. // normally not allowed if ExpectingType is set.
AllowGenericClassType = 1 << 10, AllowGeneric = 1 << 10,
// TypeVars within this expression must not refer to type vars // TypeVars within this expression must not refer to type vars
// used in an outer scope. // used in an outer scope.
DisallowTypeVarsWithScopeId = 1 << 11, NoTypeVarWithScopeId = 1 << 11,
// TypeVars within this expression do not need to refer to type vars // TypeVars within this expression do not need to refer to type vars
// used in an outer scope. // used in an outer scope.
AllowTypeVarsWithoutScopeId = 1 << 12, AllowTypeVarWithoutScopeId = 1 << 12,
// TypeVars within this expression that are otherwise not // TypeVars within this expression that are otherwise not
// associated with an outer scope should be associated with // associated with an outer scope should be associated with
// the containing function's scope. // the containing function's scope.
AssociateTypeVarsWithCurrentScope = 1 << 13, TypeVarGetsCurScope = 1 << 13,
// When a new class-scoped TypeVar is used within a class // When a new class-scoped TypeVar is used within a class
// declaration, make sure that it is not used to parameterize // declaration, make sure that it is not used to parameterize
// a base class whose TypeVar variance is inconsistent. // a base class whose TypeVar variance is inconsistent.
EnforceTypeVarVarianceConsistency = 1 << 14, EnforceVarianceConsistency = 1 << 14,
// Used for PEP 526-style variable type annotations // Used for PEP 526-style variable type annotations
VariableTypeAnnotation = 1 << 15, VarTypeAnnotation = 1 << 15,
// 'ClassVar' is not allowed in this context. // 'ClassVar' is not allowed in this context.
DisallowClassVar = 1 << 17, NoClassVar = 1 << 17,
// 'Generic' cannot be used without type arguments in this context. // 'Generic' cannot be used without type arguments in this context.
DisallowNakedGeneric = 1 << 18, NoNakedGeneric = 1 << 18,
// The node is not parsed by the interpreter because it is within // The node is not parsed by the interpreter because it is within
// a comment or a string literal. // a comment or a string literal.
NotParsedByInterpreter = 1 << 19, NotParsed = 1 << 19,
// Required and NotRequired are allowed in this context. // Required and NotRequired are allowed in this context.
AllowRequired = 1 << 20, AllowRequired = 1 << 20,
// Allow Unpack annotation for a tuple or TypeVarTuple. // Allow Unpack annotation for a tuple or TypeVarTuple.
AllowUnpackedTupleOrTypeVarTuple = 1 << 21, AllowUnpackedTuple = 1 << 21,
// Allow Unpack annotation for TypedDict.
AllowUnpackedTypedDict = 1 << 22,
// Even though an expression is enclosed in a string literal, // Even though an expression is enclosed in a string literal,
// the interpreter (within a source file, not a stub) still // the interpreter (within a source file, not a stub) still
// parses the expression and generates parse errors. // parses the expression and generates parse errors.
InterpreterParsesStringLiteral = 1 << 22, ParsesStringLiteral = 1 << 23,
// Allow Unpack annotation for TypedDict.
AllowUnpackedTypedDict = 1 << 23,
// Do not convert special forms to their corresponding runtime // Do not convert special forms to their corresponding runtime
// objects even when expecting a type expression. // objects even when expecting a type expression.
SkipConvertSpecialFormToRuntimeObject = 1 << 25, NoConvertSpecialForm = 1 << 25,
// Protocol and TypedDict are not allowed in this context. // Certain special forms (Protocol, TypedDict, etc.) are not allowed
DisallowNonTypeSpecialForms = 1 << 26, // in this context.
NoNonTypeSpecialForms = 1 << 26,
// Allow use of the Concatenate special form. // Allow use of the Concatenate special form.
AllowConcatenate = 1 << 27, AllowConcatenate = 1 << 27,
// Do not infer literal types within a tuple (used for tuples nested within // Do not infer literal types within a tuple (used for tuples nested within
// other container classes). // other container classes).
StripLiteralTypeForTuple = 1 << 28, StripTupleLiterals = 1 << 28,
// Defaults used for evaluating the LHS of a call expression. // Defaults used for evaluating the LHS of a call expression.
CallBaseDefaults = DoNotSpecialize, CallBaseDefaults = NoSpecialize,
// Defaults used for evaluating the LHS of a member access expression. // Defaults used for evaluating the LHS of a member access expression.
IndexBaseDefaults = DoNotSpecialize, IndexBaseDefaults = NoSpecialize,
// Defaults used for evaluating the LHS of a member access expression. // Defaults used for evaluating the LHS of a member access expression.
MemberAccessBaseDefaults = DoNotSpecialize, MemberAccessBaseDefaults = NoSpecialize,
} }
export interface TypeResult<T extends Type = Type> { export interface TypeResult<T extends Type = Type> {
@ -470,7 +471,7 @@ export interface TypeEvaluator {
getTypeResult: (node: ExpressionNode) => TypeResult | undefined; getTypeResult: (node: ExpressionNode) => TypeResult | undefined;
getTypeResultForDecorator: (node: DecoratorNode) => TypeResult | undefined; getTypeResultForDecorator: (node: DecoratorNode) => TypeResult | undefined;
getCachedType: (node: ExpressionNode) => Type | undefined; getCachedType: (node: ExpressionNode) => Type | undefined;
getTypeOfExpression: (node: ExpressionNode, flags?: EvaluatorFlags, context?: InferenceContext) => TypeResult; getTypeOfExpression: (node: ExpressionNode, flags?: EvalFlags, context?: InferenceContext) => TypeResult;
getTypeOfAnnotation: (node: ExpressionNode, options?: AnnotationTypeOptions) => Type; getTypeOfAnnotation: (node: ExpressionNode, options?: AnnotationTypeOptions) => Type;
getTypeOfClass: (node: ClassNode) => ClassTypeResult | undefined; getTypeOfClass: (node: ClassNode) => ClassTypeResult | undefined;
getTypeOfFunction: (node: FunctionNode) => FunctionTypeResult | undefined; getTypeOfFunction: (node: FunctionNode) => FunctionTypeResult | undefined;
@ -648,7 +649,7 @@ export interface TypeEvaluator {
flags: AssignTypeFlags, flags: AssignTypeFlags,
recursionCount: number recursionCount: number
) => boolean; ) => boolean;
reportMissingTypeArguments: (node: ExpressionNode, type: Type, flags: EvaluatorFlags) => Type; reportMissingTypeArguments: (node: ExpressionNode, type: Type, flags: EvalFlags) => Type;
isFinalVariable: (symbol: Symbol) => boolean; isFinalVariable: (symbol: Symbol) => boolean;
isFinalVariableDeclaration: (decl: Declaration) => boolean; isFinalVariableDeclaration: (decl: Declaration) => boolean;
@ -680,7 +681,7 @@ export interface TypeEvaluator {
disposeEvaluator: () => void; disposeEvaluator: () => void;
useSpeculativeMode: <T>(speculativeNode: ParseNode | undefined, callback: () => T) => T; useSpeculativeMode: <T>(speculativeNode: ParseNode | undefined, callback: () => T) => T;
isSpeculativeModeInUse: (node: ParseNode | undefined) => boolean; isSpeculativeModeInUse: (node: ParseNode | undefined) => boolean;
setTypeResultForNode: (node: ParseNode, typeResult: TypeResult, flags?: EvaluatorFlags) => void; setTypeResultForNode: (node: ParseNode, typeResult: TypeResult, flags?: EvalFlags) => void;
checkForCancellation: () => void; checkForCancellation: () => void;
printControlFlowGraph: ( printControlFlowGraph: (

View File

@ -30,7 +30,7 @@ import { ScopeType } from './scope';
import { getScopeForNode } from './scopeUtils'; import { getScopeForNode } from './scopeUtils';
import { Symbol, SymbolFlags } from './symbol'; import { Symbol, SymbolFlags } from './symbol';
import { getTypedDictMembersForClass } from './typedDicts'; import { getTypedDictMembersForClass } from './typedDicts';
import { EvaluatorFlags, TypeEvaluator } from './typeEvaluatorTypes'; import { EvalFlags, TypeEvaluator } from './typeEvaluatorTypes';
import { import {
ClassType, ClassType,
ClassTypeFlags, ClassTypeFlags,
@ -219,7 +219,7 @@ export function getTypeNarrowingCallback(
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) { if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
const callType = evaluator.getTypeOfExpression( const callType = evaluator.getTypeOfExpression(
testExpression.leftExpression.leftExpression, testExpression.leftExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
).type; ).type;
if (isInstantiableClass(callType) && ClassType.isBuiltIn(callType, 'type')) { if (isInstantiableClass(callType) && ClassType.isBuiltIn(callType, 'type')) {
@ -512,7 +512,7 @@ export function getTypeNarrowingCallback(
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) { if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
const callTypeResult = evaluator.getTypeOfExpression( const callTypeResult = evaluator.getTypeOfExpression(
testExpression.leftExpression.leftExpression, testExpression.leftExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const callType = callTypeResult.type; const callType = callTypeResult.type;
@ -618,7 +618,7 @@ export function getTypeNarrowingCallback(
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) { if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
const callTypeResult = evaluator.getTypeOfExpression( const callTypeResult = evaluator.getTypeOfExpression(
testExpression.leftExpression, testExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const callType = callTypeResult.type; const callType = callTypeResult.type;
@ -629,12 +629,12 @@ export function getTypeNarrowingCallback(
const isInstanceCheck = callType.details.builtInName === 'isinstance'; const isInstanceCheck = callType.details.builtInName === 'isinstance';
const arg1TypeResult = evaluator.getTypeOfExpression( const arg1TypeResult = evaluator.getTypeOfExpression(
arg1Expr, arg1Expr,
EvaluatorFlags.AllowMissingTypeArgs | EvalFlags.AllowMissingTypeArgs |
EvaluatorFlags.EvaluateStringLiteralAsType | EvalFlags.StrLiteralAsType |
EvaluatorFlags.DisallowParamSpec | EvalFlags.NoParamSpec |
EvaluatorFlags.DisallowTypeVarTuple | EvalFlags.NoTypeVarTuple |
EvaluatorFlags.DisallowFinal | EvalFlags.NoFinal |
EvaluatorFlags.DoNotSpecialize EvalFlags.NoSpecialize
); );
const arg1Type = arg1TypeResult.type; const arg1Type = arg1TypeResult.type;
@ -676,7 +676,7 @@ export function getTypeNarrowingCallback(
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) { if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
const callTypeResult = evaluator.getTypeOfExpression( const callTypeResult = evaluator.getTypeOfExpression(
testExpression.leftExpression, testExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const callType = callTypeResult.type; const callType = callTypeResult.type;
@ -711,7 +711,7 @@ export function getTypeNarrowingCallback(
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.arguments[0].valueExpression)) { if (ParseTreeUtils.isMatchingExpression(reference, testExpression.arguments[0].valueExpression)) {
const callTypeResult = evaluator.getTypeOfExpression( const callTypeResult = evaluator.getTypeOfExpression(
testExpression.leftExpression, testExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const callType = callTypeResult.type; const callType = callTypeResult.type;
@ -743,7 +743,7 @@ export function getTypeNarrowingCallback(
const callTypeResult = evaluator.getTypeOfExpression( const callTypeResult = evaluator.getTypeOfExpression(
testExpression.leftExpression, testExpression.leftExpression,
EvaluatorFlags.CallBaseDefaults EvalFlags.CallBaseDefaults
); );
const callType = callTypeResult.type; const callType = callTypeResult.type;