import { CancellationToken } from 'vscode-languageserver-protocol' ; import { DiagnosticLevel } from '../common/configOptions' ; import { Diagnostic , DiagnosticAddendum } from '../common/diagnostic' ; import { TextRange } from '../common/textRange' ; import { ArgumentCategory , ArgumentNode , CallNode , CaseNode , ClassNode , ExpressionNode , FunctionNode , MatchNode , NameNode , ParameterCategory , ParameterNode , ParseNode , RaiseNode , } from '../parser/parseNodes' ; import * as DeclarationUtils from './aliasDeclarationUtils' ; import { AnalyzerFileInfo } from './analyzerFileInfo' ; import { Declaration } from './declaration' ; import { SymbolWithScope } from './scope' ; import { Symbol } from './symbol' ; import { ClassType , FunctionParameter , FunctionType , OverloadedFunctionType , Type , TypeCondition , TypeVarScopeId , TypeVarType , } from './types' ; import { CanAssignFlags , ClassMember } from './typeUtils' ; import { TypeVarMap } from './typeVarMap' ; export const enum EvaluatorFlags { None = 0 , ConvertEllipsisToAny = 1 << 0 , DoNotSpecialize = 1 << 1 , AllowForwardReferences = 1 << 2 , EvaluateStringLiteralAsType = 1 << 3 , FinalDisallowed = 1 << 4 , ParamSpecDisallowed = 1 << 5 , ExpectingType = 1 << 6 , TypeVarTupleDisallowed = 1 << 7 , ConvertEllipsisToUnknown = 1 << 8 , AllowGenericClassType = 1 << 9 , ExpectingTypeAnnotation = 1 << 10 , DisallowTypeVarsWithScopeId = 1 << 11 , DisallowTypeVarsWithoutScopeId = 1 << 12 , AssociateTypeVarsWithCurrentScope = 1 << 13 , VariableTypeAnnotation = 1 << 15 , DisallowRecursiveTypeAliasPlaceholder = 1 << 16 , ClassVarDisallowed = 1 << 17 , DisallowNakedGeneric = 1 << 18 , NotParsedByInterpreter = 1 << 19 , RequiredAllowed = 1 << 20 } export interface TypeArgumentResult { type : Type ; isIncomplete ? : boolean | undefined ; } export interface TypeResult { type : Type ; node : ParseNode ; typeErrors ? : boolean | undefined ; isEmptyTupleShorthand ? : boolean | undefined ; isIncomplete ? : boolean | undefined ; unpackedType ? : Type | undefined ; typeList ? : TypeResult [ ] | undefined ; expectedTypeDiagAddendum ? : DiagnosticAddendum | undefined ; bindToType ? : ClassType | TypeVarType | undefined ; isAsymmetricDescriptor ? : boolean ; isRequired ? : boolean ; isNotRequired ? : boolean ; } export interface EvaluatorUsage { method : 'get' | 'set' | 'del' ; setType ? : Type | undefined ; setErrorNode ? : ExpressionNode | undefined ; setExpectedTypeDiag ? : DiagnosticAddendum | undefined ; } export interface ClassTypeResult { classType : ClassType ; decoratedType : Type ; } export interface FunctionTypeResult { functionType : FunctionType ; decoratedType : Type ; } export interface CallSignature { type : FunctionType ; activeParam ? : FunctionParameter | undefined ; } export interface CallSignatureInfo { signatures : CallSignature [ ] ; callNode : CallNode ; } export interface AbstractMethod { symbol : Symbol ; symbolName : string ; classType : Type ; isAbstract : boolean ; } export interface FunctionArgumentBase { argumentCategory : ArgumentCategory ; node ? : ArgumentNode | undefined ; name ? : NameNode | undefined ; type ? : Type | undefined ; valueExpression ? : ExpressionNode | undefined ; active ? : boolean | undefined ; } export interface FunctionArgumentWithType extends FunctionArgumentBase { type : Type ; } export interface FunctionArgumentWithExpression extends FunctionArgumentBase { valueExpression : ExpressionNode ; } export type FunctionArgument = FunctionArgumentWithType | FunctionArgumentWithExpression ; export interface EffectiveTypeResult { type : Type ; isIncomplete : boolean ; includesVariableDecl : boolean ; isRecursiveDefinition : boolean ; } export interface ValidateArgTypeParams { paramCategory : ParameterCategory ; paramType : Type ; requiresTypeVarMatching : boolean ; argument : FunctionArgument ; argType ? : Type | undefined ; errorNode : ExpressionNode ; paramName ? : string | undefined ; mapsToVarArgList ? : boolean | undefined ; expectingType ? : boolean ; } export interface AnnotationTypeOptions { isVariableAnnotation ? : boolean ; allowFinal ? : boolean ; allowClassVar ? : boolean ; associateTypeVarsWithScope ? : boolean ; allowTypeVarTuple ? : boolean ; allowParamSpec ? : boolean ; disallowRecursiveTypeAlias ? : boolean ; } export interface ExpectedTypeResult { type : Type ; node : ParseNode ; } export interface FunctionResult { returnType : Type ; argumentErrors : boolean ; isTypeIncomplete : boolean ; } export interface TypeEvaluator { runWithCancellationToken < T > ( token , callback ) : T ; getType : ( node : ExpressionNode ) => Type | undefined ; getTypeOfExpression : ( node : ExpressionNode , expectedType ? : Type , flags ? : EvaluatorFlags ) => TypeResult ; getTypeOfAnnotation : ( node : ExpressionNode , options ? : AnnotationTypeOptions ) => Type ; getTypeOfClass : ( node : ClassNode ) => ClassTypeResult | undefined ; getTypeOfFunction : ( node : FunctionNode ) => FunctionTypeResult | undefined ; getTypeForExpressionExpectingType : ( node : ExpressionNode , allowFinal ? : boolean , allowRequired ? : boolean ) => TypeResult ; evaluateTypeForSubnode : ( subnode : ParseNode , callback : ( ) => void ) => TypeResult | undefined ; evaluateTypesForStatement : ( node : ParseNode ) => void ; evaluateTypesForMatchNode : ( node : MatchNode ) => void ; evaluateTypesForCaseNode : ( node : CaseNode ) => void ; evaluateTypeOfParameter : ( node : ParameterNode ) => void ; canBeTruthy : ( type : Type ) => boolean ; canBeFalsy : ( type : Type ) => boolean ; removeTruthinessFromType : ( type : Type ) => Type ; removeFalsinessFromType : ( type : Type ) => Type ; getExpectedType : ( node : ExpressionNode ) => ExpectedTypeResult | undefined ; verifyRaiseExceptionType : ( node : RaiseNode ) => void ; verifyDeleteExpression : ( node : ExpressionNode ) => void ; isAfterNodeReachable : ( node : ParseNode ) => boolean ; isNodeReachable : ( node : ParseNode , sourceNode : ParseNode | undefined ) => boolean ; isAsymmetricDescriptorAssignment : ( node : ParseNode ) => boolean ; suppressDiagnostics : ( node : ParseNode , callback : ( ) => void ) => void ; getDeclarationsForNameNode : ( node : NameNode ) => Declaration [ ] | undefined ; getTypeForDeclaration : ( declaration : Declaration ) => Type | undefined ; resolveAliasDeclaration : ( declaration : Declaration , resolveLocalNames : boolean , allowExternallyHiddenAccess ? : boolean ) => Declaration | undefined ; resolveAliasDeclarationWithInfo : ( declaration : Declaration , resolveLocalNames : boolean , allowExternallyHiddenAccess ? : boolean ) => DeclarationUtils . ResolvedAliasInfo | undefined ; getTypeFromIterable : ( type : Type , isAsync : boolean , errorNode : ParseNode | undefined ) => Type | undefined ; getTypeFromIterator : ( type : Type , isAsync : boolean , errorNode : ParseNode | undefined ) => Type | undefined ; getGetterTypeFromProperty : ( propertyClass : ClassType , inferTypeIfNeeded : boolean ) => Type | undefined ; getTypeForArgument : ( arg : FunctionArgument ) => TypeArgumentResult ; markNamesAccessed : ( node : ParseNode , names : string [ ] ) => void ; getScopeIdForNode : ( node : ParseNode ) => string ; makeTopLevelTypeVarsConcrete : ( type : Type ) => Type ; mapSubtypesExpandTypeVars : ( type : Type , conditionFilter : TypeCondition [ ] | undefined , callback : ( expandedSubtype : Type , unexpandedSubtype : Type ) => Type | undefined ) => Type ; populateTypeVarMapBasedOnExpectedType : ( type : ClassType , expectedType : Type , typeVarMap : TypeVarMap , liveTypeVarScopes : TypeVarScopeId [ ] | undefined ) => boolean ; lookUpSymbolRecursive : ( node : ParseNode , name : string , honorCodeFlow : boolean ) => SymbolWithScope | undefined ; getDeclaredTypeOfSymbol : ( symbol : Symbol ) => Type | undefined ; getEffectiveTypeOfSymbol : ( symbol : Symbol ) => Type ; getEffectiveTypeOfSymbolForUsage : ( symbol : Symbol , usageNode ? : NameNode , useLastDecl ? : boolean ) => EffectiveTypeResult ; getInferredTypeOfDeclaration : ( decl : Declaration ) => Type | undefined ; getDeclaredTypeForExpression : ( expression : ExpressionNode , usage ? : EvaluatorUsage ) => Type | undefined ; getFunctionDeclaredReturnType : ( node : FunctionNode ) => Type | undefined ; getFunctionInferredReturnType : ( type : FunctionType , args ? : ValidateArgTypeParams [ ] ) => Type ; getBestOverloadForArguments : ( errorNode : ExpressionNode , type : OverloadedFunctionType , argList : FunctionArgument [ ] ) => FunctionType | undefined ; getBuiltInType : ( node : ParseNode , name : string ) => Type ; getTypeOfMember : ( member : ClassMember ) => Type ; getTypeFromObjectMember ( errorNode , objectType , memberName ) : TypeResult | undefined ; getBoundMethod : ( classType : ClassType , memberName : string , treatConstructorAsClassMember ? : boolean ) => FunctionType | OverloadedFunctionType | undefined ; getTypeFromMagicMethodReturn : ( objType : Type , args : Type [ ] , magicMethodName : string , errorNode : ExpressionNode , expectedType : Type | undefined ) => Type | undefined ; bindFunctionToClassOrObject : ( baseType : ClassType | undefined , memberType : FunctionType | OverloadedFunctionType ) => FunctionType | OverloadedFunctionType | undefined ; getCallSignatureInfo : ( node : CallNode , activeIndex : number , activeOrFake : boolean ) => CallSignatureInfo | undefined ; getTypeAnnotationForParameter : ( node : FunctionNode , paramIndex : number ) => ExpressionNode | undefined ; getAbstractMethods : ( classType : ClassType ) => AbstractMethod [ ] ; narrowConstrainedTypeVar : ( node : ParseNode , typeVar : TypeVarType ) => Type | undefined ; canAssignType : ( destType : Type , srcType : Type , diag ? : DiagnosticAddendum , typeVarMap ? : TypeVarMap , flags ? : CanAssignFlags , recursionCount ? : number ) => boolean ; canOverrideMethod : ( baseMethod : Type , overrideMethod : FunctionType , diag : DiagnosticAddendum , enforceParamNames ? : boolean ) => boolean ; canAssignProtocolClassToSelf : ( destType : ClassType , srcType : ClassType ) => boolean ; assignTypeToExpression : ( target : ExpressionNode , type : Type , isTypeIncomplete : boolean , srcExpr : ExpressionNode ) => void ; getBuiltInObject : ( node : ParseNode , name : string , typeArguments ? : Type [ ] ) => Type ; getTypingType : ( node : ParseNode , symbolName : string ) => Type | undefined ; inferReturnTypeIfNecessary : ( type : Type ) => void ; addError : ( message : string , node : ParseNode ) => Diagnostic | undefined ; addWarning : ( message : string , node : ParseNode ) => Diagnostic | undefined ; addInformation : ( message : string , node : ParseNode ) => Diagnostic | undefined ; addUnusedCode : ( node : ParseNode , textRange : TextRange ) => void ; addDeprecated : ( message : string , node : ParseNode ) => void ; addDiagnostic : ( diagLevel : DiagnosticLevel , rule : string , message : string , node : ParseNode ) => Diagnostic | undefined ; addDiagnosticForTextRange : ( fileInfo : AnalyzerFileInfo , diagLevel : DiagnosticLevel , rule : string , message : string , range : TextRange ) => Diagnostic | undefined ; printType : ( type : Type , expandTypeAlias ? : boolean ) => string ; printFunctionParts : ( type : FunctionType ) => [ string [ ] , string ] ; getTypeCacheSize : ( ) => number ; useSpeculativeMode : < T > ( speculativeNode : ParseNode , callback : ( ) => T ) => T ; setTypeForNode : ( node : ParseNode , type ? : Type , flags ? : EvaluatorFlags ) => void ; checkForCancellation : ( ) => void ; }