import { isAbsolute } from 'path' ; import { getPathsFromPthFiles } from '../analyzer/pythonPathUtils' ; import * as pathConsts from '../common/pathConsts' ; import { DiagnosticSeverityOverridesMap } from './commandLineOptions' ; import { ConsoleInterface } from './console' ; import { DiagnosticRule } from './diagnosticRules' ; import { FileSystem } from './fileSystem' ; import { Host } from './host' ; import { combinePaths , ensureTrailingDirectorySeparator , FileSpec , getFileSpec , isDirectory , normalizePath , resolvePaths , } from './pathUtils' ; import { latestStablePythonVersion , PythonVersion , versionFromString , versionToString } from './pythonVersion' ; export enum PythonPlatform { Darwin = 'Darwin' , Windows = 'Windows' , Linux = 'Linux' } export class ExecutionEnvironment { constructor ( root , defaultPythonVersion , defaultPythonPlatform , defaultExtraPaths ) { this . root = root || undefined ; this . pythonVersion = defaultPythonVersion || latestStablePythonVersion ; this . pythonPlatform = defaultPythonPlatform ; this . extraPaths = defaultExtraPaths || [ ] ; } root ? : string ; pythonVersion : PythonVersion ; pythonPlatform ? : string | undefined ; extraPaths : string [ ] = [ ] ; } export type DiagnosticLevel = 'none' | 'information' | 'warning' | 'error' ; export interface DiagnosticRuleSet { printUnknownAsAny : boolean ; omitTypeArgsIfAny : boolean ; omitUnannotatedParamType : boolean ; omitConditionalConstraint : boolean ; pep604Printing : boolean ; strictListInference : boolean ; strictSetInference : boolean ; strictDictionaryInference : boolean ; strictParameterNoneValue : boolean ; enableTypeIgnoreComments : boolean ; reportGeneralTypeIssues : DiagnosticLevel ; reportPropertyTypeMismatch : DiagnosticLevel ; reportFunctionMemberAccess : DiagnosticLevel ; reportMissingImports : DiagnosticLevel ; reportMissingModuleSource : DiagnosticLevel ; reportMissingTypeStubs : DiagnosticLevel ; reportImportCycles : DiagnosticLevel ; reportUnusedImport : DiagnosticLevel ; reportUnusedClass : DiagnosticLevel ; reportUnusedFunction : DiagnosticLevel ; reportUnusedVariable : DiagnosticLevel ; reportDuplicateImport : DiagnosticLevel ; reportWildcardImportFromLibrary : DiagnosticLevel ; reportOptionalSubscript : DiagnosticLevel ; reportOptionalMemberAccess : DiagnosticLevel ; reportOptionalCall : DiagnosticLevel ; reportOptionalIterable : DiagnosticLevel ; reportOptionalContextManager : DiagnosticLevel ; reportOptionalOperand : DiagnosticLevel ; reportTypedDictNotRequiredAccess : DiagnosticLevel ; reportUntypedFunctionDecorator : DiagnosticLevel ; reportUntypedClassDecorator : DiagnosticLevel ; reportUntypedBaseClass : DiagnosticLevel ; reportUntypedNamedTuple : DiagnosticLevel ; reportPrivateUsage : DiagnosticLevel ; reportPrivateImportUsage : DiagnosticLevel ; reportConstantRedefinition : DiagnosticLevel ; reportIncompatibleMethodOverride : DiagnosticLevel ; reportIncompatibleVariableOverride : DiagnosticLevel ; reportOverlappingOverload : DiagnosticLevel ; reportUninitializedInstanceVariable : DiagnosticLevel ; reportInvalidStringEscapeSequence : DiagnosticLevel ; reportUnknownParameterType : DiagnosticLevel ; reportUnknownArgumentType : DiagnosticLevel ; reportUnknownLambdaType : DiagnosticLevel ; reportUnknownVariableType : DiagnosticLevel ; reportUnknownMemberType : DiagnosticLevel ; reportMissingParameterType : DiagnosticLevel ; reportMissingTypeArgument : DiagnosticLevel ; reportInvalidTypeVarUse : DiagnosticLevel ; reportCallInDefaultInitializer : DiagnosticLevel ; reportUnnecessaryIsInstance : DiagnosticLevel ; reportUnnecessaryCast : DiagnosticLevel ; reportUnnecessaryComparison : DiagnosticLevel ; reportAssertAlwaysTrue : DiagnosticLevel ; reportSelfClsParameterName : DiagnosticLevel ; reportImplicitStringConcatenation : DiagnosticLevel ; reportUndefinedVariable : DiagnosticLevel ; reportUnboundVariable : DiagnosticLevel ; reportInvalidStubStatement : DiagnosticLevel ; reportIncompleteStub : DiagnosticLevel ; reportUnsupportedDunderAll : DiagnosticLevel ; reportUnusedCallResult : DiagnosticLevel ; reportUnusedCoroutine : DiagnosticLevel ; } export function cloneDiagnosticRuleSet ( diagSettings ) { return Object . assign ( { } , diagSettings ) ; } export function getBooleanDiagnosticRules ( ) { return [ DiagnosticRule . strictListInference , DiagnosticRule . strictSetInference , DiagnosticRule . strictDictionaryInference , DiagnosticRule . strictParameterNoneValue , ] ; } export function getDiagLevelDiagnosticRules ( ) { return [ DiagnosticRule . reportGeneralTypeIssues , DiagnosticRule . reportPropertyTypeMismatch , DiagnosticRule . reportFunctionMemberAccess , DiagnosticRule . reportMissingImports , DiagnosticRule . reportMissingModuleSource , DiagnosticRule . reportMissingTypeStubs , DiagnosticRule . reportImportCycles , DiagnosticRule . reportUnusedImport , DiagnosticRule . reportUnusedClass , DiagnosticRule . reportUnusedFunction , DiagnosticRule . reportUnusedVariable , DiagnosticRule . reportDuplicateImport , DiagnosticRule . reportWildcardImportFromLibrary , DiagnosticRule . reportOptionalSubscript , DiagnosticRule . reportOptionalMemberAccess , DiagnosticRule . reportOptionalCall , DiagnosticRule . reportOptionalIterable , DiagnosticRule . reportOptionalContextManager , DiagnosticRule . reportOptionalOperand , DiagnosticRule . reportTypedDictNotRequiredAccess , DiagnosticRule . reportUntypedFunctionDecorator , DiagnosticRule . reportUntypedClassDecorator , DiagnosticRule . reportUntypedBaseClass , DiagnosticRule . reportUntypedNamedTuple , DiagnosticRule . reportPrivateUsage , DiagnosticRule . reportPrivateImportUsage , DiagnosticRule . reportConstantRedefinition , DiagnosticRule . reportIncompatibleMethodOverride , DiagnosticRule . reportIncompatibleVariableOverride , DiagnosticRule . reportOverlappingOverload , DiagnosticRule . reportUninitializedInstanceVariable , DiagnosticRule . reportInvalidStringEscapeSequence , DiagnosticRule . reportUnknownParameterType , DiagnosticRule . reportUnknownArgumentType , DiagnosticRule . reportUnknownLambdaType , DiagnosticRule . reportUnknownVariableType , DiagnosticRule . reportUnknownMemberType , DiagnosticRule . reportMissingParameterType , DiagnosticRule . reportMissingTypeArgument , DiagnosticRule . reportInvalidTypeVarUse , DiagnosticRule . reportCallInDefaultInitializer , DiagnosticRule . reportUnnecessaryIsInstance , DiagnosticRule . reportUnnecessaryCast , DiagnosticRule . reportUnnecessaryComparison , DiagnosticRule . reportAssertAlwaysTrue , DiagnosticRule . reportSelfClsParameterName , DiagnosticRule . reportImplicitStringConcatenation , DiagnosticRule . reportUndefinedVariable , DiagnosticRule . reportUnboundVariable , DiagnosticRule . reportInvalidStubStatement , DiagnosticRule . reportIncompleteStub , DiagnosticRule . reportUnsupportedDunderAll , DiagnosticRule . reportUnusedCallResult , DiagnosticRule . reportUnusedCoroutine , ] ; } export function getStrictModeNotOverriddenRules ( ) { return [ DiagnosticRule . reportMissingModuleSource ] ; } export function getOffDiagnosticRuleSet ( ) { const diagSettings = { printUnknownAsAny : true , omitTypeArgsIfAny : true , omitUnannotatedParamType : true , omitConditionalConstraint : true , pep604Printing : true , strictListInference : false , strictSetInference : false , strictDictionaryInference : false , strictParameterNoneValue : false , enableTypeIgnoreComments : true , reportGeneralTypeIssues : 'none' , reportPropertyTypeMismatch : 'none' , reportFunctionMemberAccess : 'none' , reportMissingImports : 'warning' , reportMissingModuleSource : 'warning' , reportMissingTypeStubs : 'none' , reportImportCycles : 'none' , reportUnusedImport : 'none' , reportUnusedClass : 'none' , reportUnusedFunction : 'none' , reportUnusedVariable : 'none' , reportDuplicateImport : 'none' , reportWildcardImportFromLibrary : 'none' , reportOptionalSubscript : 'none' , reportOptionalMemberAccess : 'none' , reportOptionalCall : 'none' , reportOptionalIterable : 'none' , reportOptionalContextManager : 'none' , reportOptionalOperand : 'none' , reportTypedDictNotRequiredAccess : 'none' , reportUntypedFunctionDecorator : 'none' , reportUntypedClassDecorator : 'none' , reportUntypedBaseClass : 'none' , reportUntypedNamedTuple : 'none' , reportPrivateUsage : 'none' , reportPrivateImportUsage : 'none' , reportConstantRedefinition : 'none' , reportIncompatibleMethodOverride : 'none' , reportIncompatibleVariableOverride : 'none' , reportOverlappingOverload : 'none' , reportUninitializedInstanceVariable : 'none' , reportInvalidStringEscapeSequence : 'none' , reportUnknownParameterType : 'none' , reportUnknownArgumentType : 'none' , reportUnknownLambdaType : 'none' , reportUnknownVariableType : 'none' , reportUnknownMemberType : 'none' , reportMissingParameterType : 'none' , reportMissingTypeArgument : 'none' , reportInvalidTypeVarUse : 'none' , reportCallInDefaultInitializer : 'none' , reportUnnecessaryIsInstance : 'none' , reportUnnecessaryCast : 'none' , reportUnnecessaryComparison : 'none' , reportAssertAlwaysTrue : 'none' , reportSelfClsParameterName : 'none' , reportImplicitStringConcatenation : 'none' , reportUnboundVariable : 'none' , reportUndefinedVariable : 'warning' , reportInvalidStubStatement : 'none' , reportIncompleteStub : 'none' , reportUnsupportedDunderAll : 'none' , reportUnusedCallResult : 'none' , reportUnusedCoroutine : 'none' , } ; return diagSettings ; } export function getBasicDiagnosticRuleSet ( ) { const diagSettings = { printUnknownAsAny : false , omitTypeArgsIfAny : false , omitUnannotatedParamType : true , omitConditionalConstraint : false , pep604Printing : true , strictListInference : false , strictSetInference : false , strictDictionaryInference : false , strictParameterNoneValue : false , enableTypeIgnoreComments : true , reportGeneralTypeIssues : 'error' , reportPropertyTypeMismatch : 'none' , reportFunctionMemberAccess : 'none' , reportMissingImports : 'error' , reportMissingModuleSource : 'warning' , reportMissingTypeStubs : 'none' , reportImportCycles : 'none' , reportUnusedImport : 'none' , reportUnusedClass : 'none' , reportUnusedFunction : 'none' , reportUnusedVariable : 'none' , reportDuplicateImport : 'none' , reportWildcardImportFromLibrary : 'warning' , reportOptionalSubscript : 'error' , reportOptionalMemberAccess : 'error' , reportOptionalCall : 'error' , reportOptionalIterable : 'error' , reportOptionalContextManager : 'error' , reportOptionalOperand : 'error' , reportTypedDictNotRequiredAccess : 'error' , reportUntypedFunctionDecorator : 'none' , reportUntypedClassDecorator : 'none' , reportUntypedBaseClass : 'none' , reportUntypedNamedTuple : 'none' , reportPrivateUsage : 'none' , reportPrivateImportUsage : 'error' , reportConstantRedefinition : 'none' , reportIncompatibleMethodOverride : 'none' , reportIncompatibleVariableOverride : 'none' , reportOverlappingOverload : 'none' , reportUninitializedInstanceVariable : 'none' , reportInvalidStringEscapeSequence : 'warning' , reportUnknownParameterType : 'none' , reportUnknownArgumentType : 'none' , reportUnknownLambdaType : 'none' , reportUnknownVariableType : 'none' , reportUnknownMemberType : 'none' , reportMissingParameterType : 'none' , reportMissingTypeArgument : 'none' , reportInvalidTypeVarUse : 'warning' , reportCallInDefaultInitializer : 'none' , reportUnnecessaryIsInstance : 'none' , reportUnnecessaryCast : 'none' , reportUnnecessaryComparison : 'none' , reportAssertAlwaysTrue : 'warning' , reportSelfClsParameterName : 'warning' , reportImplicitStringConcatenation : 'none' , reportUnboundVariable : 'error' , reportUndefinedVariable : 'error' , reportInvalidStubStatement : 'none' , reportIncompleteStub : 'none' , reportUnsupportedDunderAll : 'warning' , reportUnusedCallResult : 'none' , reportUnusedCoroutine : 'error' , } ; return diagSettings ; } export function getStrictDiagnosticRuleSet ( ) { const diagSettings = { printUnknownAsAny : false , omitTypeArgsIfAny : false , omitUnannotatedParamType : false , omitConditionalConstraint : false , pep604Printing : true , strictListInference : true , strictSetInference : true , strictDictionaryInference : true , strictParameterNoneValue : true , enableTypeIgnoreComments : true , reportGeneralTypeIssues : 'error' , reportPropertyTypeMismatch : 'none' , reportFunctionMemberAccess : 'error' , reportMissingImports : 'error' , reportMissingModuleSource : 'warning' , reportMissingTypeStubs : 'error' , reportImportCycles : 'error' , reportUnusedImport : 'error' , reportUnusedClass : 'error' , reportUnusedFunction : 'error' , reportUnusedVariable : 'error' , reportDuplicateImport : 'error' , reportWildcardImportFromLibrary : 'error' , reportOptionalSubscript : 'error' , reportOptionalMemberAccess : 'error' , reportOptionalCall : 'error' , reportOptionalIterable : 'error' , reportOptionalContextManager : 'error' , reportOptionalOperand : 'error' , reportTypedDictNotRequiredAccess : 'error' , reportUntypedFunctionDecorator : 'error' , reportUntypedClassDecorator : 'error' , reportUntypedBaseClass : 'error' , reportUntypedNamedTuple : 'error' , reportPrivateUsage : 'error' , reportPrivateImportUsage : 'error' , reportConstantRedefinition : 'error' , reportIncompatibleMethodOverride : 'error' , reportIncompatibleVariableOverride : 'error' , reportOverlappingOverload : 'error' , reportUninitializedInstanceVariable : 'none' , reportInvalidStringEscapeSequence : 'error' , reportUnknownParameterType : 'error' , reportUnknownArgumentType : 'error' , reportUnknownLambdaType : 'error' , reportUnknownVariableType : 'error' , reportUnknownMemberType : 'error' , reportMissingParameterType : 'error' , reportMissingTypeArgument : 'error' , reportInvalidTypeVarUse : 'error' , reportCallInDefaultInitializer : 'none' , reportUnnecessaryIsInstance : 'error' , reportUnnecessaryCast : 'error' , reportUnnecessaryComparison : 'error' , reportAssertAlwaysTrue : 'error' , reportSelfClsParameterName : 'error' , reportImplicitStringConcatenation : 'none' , reportUnboundVariable : 'error' , reportUndefinedVariable : 'error' , reportInvalidStubStatement : 'error' , reportIncompleteStub : 'error' , reportUnsupportedDunderAll : 'error' , reportUnusedCallResult : 'none' , reportUnusedCoroutine : 'error' , } ; return diagSettings ; } export class ConfigOptions { constructor ( projectRoot , typeCheckingMode ? ) { this . projectRoot = projectRoot ; this . typeCheckingMode = typeCheckingMode ; this . diagnosticRuleSet = ConfigOptions . getDiagnosticRuleSet ( typeCheckingMode ) ; if ( typeCheckingMode === 'off' ) { this . disableInferenceForPyTypedSources = false ; } } projectRoot : string ; pythonPath ? : string | undefined ; typeshedPath ? : string | undefined ; stubPath ? : string | undefined ; include : FileSpec [ ] = [ ] ; exclude : FileSpec [ ] = [ ] ; autoExcludeVenv ? : boolean | undefined ; ignore : FileSpec [ ] = [ ] ; strict : FileSpec [ ] = [ ] ; verboseOutput ? : boolean | undefined ; checkOnlyOpenFiles ? : boolean | undefined ; useLibraryCodeForTypes ? : boolean | undefined ; autoImportCompletions = true ; indexing = false ; logTypeEvaluationTime = false ; typeEvaluationTimeThreshold = 50 ; disableInferenceForPyTypedSources = true ; typeCheckingMode ? : string ; initializedFromJson = false ; analyzeUnannotatedFunctions = true ; diagnosticRuleSet : DiagnosticRuleSet ; executionEnvironments : ExecutionEnvironment [ ] = [ ] ; venvPath ? : string | undefined ; venv ? : string | undefined ; defaultPythonVersion ? : PythonVersion | undefined ; defaultPythonPlatform ? : string | undefined ; defaultExtraPaths ? : string [ ] | undefined ; internalTestMode ? : boolean | undefined ; indexGenerationMode ? : boolean | undefined ; evaluateUnknownImportsAsAny ? : boolean ; static getDiagnosticRuleSet ( typeCheckingMode ? ) { if ( typeCheckingMode === 'strict' ) { return getStrictDiagnosticRuleSet ( ) ; } if ( typeCheckingMode === 'off' ) { return getOffDiagnosticRuleSet ( ) ; } return getBasicDiagnosticRuleSet ( ) ; } getDefaultExecEnvironment ( ) { return new ExecutionEnvironment ( this . projectRoot , this . defaultPythonVersion , this . defaultPythonPlatform , this . defaultExtraPaths ) ; } findExecEnvironment ( filePath ) { return ( this . executionEnvironments . find ( ( env ) => { const envRoot = ensureTrailingDirectorySeparator ( normalizePath ( combinePaths ( this . projectRoot , env . root ) ) ) ; return filePath . startsWith ( envRoot ) ; } ) ?? this . getDefaultExecEnvironment ( ) ) ; } getExecutionEnvironments ( ) { if ( this . executionEnvironments . length > 0 ) { return this . executionEnvironments ; } return [ this . getDefaultExecEnvironment ( ) ] ; } initializeFromJson ( configObj , typeCheckingMode , console , host , diagnosticOverrides ? , skipIncludeSection = false ) { this . initializedFromJson = true ; if ( ! skipIncludeSection ) { this . include = [ ] ; if ( configObj . include !== undefined ) { if ( ! Array . isArray ( configObj . include ) ) { console . error ( `Config "include" entry must must contain an array.` ) ; } else { const filesList = configObj . include as string [ ] ; filesList . forEach ( ( fileSpec , index ) => { if ( typeof fileSpec !== 'string' ) { console . error ( `Index ${ index } of "include" array should be a string.` ) ; } else if ( isAbsolute ( fileSpec ) ) { console . error ( `Ignoring path "${ fileSpec }" in "include" array because it is not relative.` ) ; } else { this . include . push ( getFileSpec ( this . projectRoot , fileSpec ) ) ; } } ) ; } } } this . exclude = [ ] ; if ( configObj . exclude !== undefined ) { if ( ! Array . isArray ( configObj . exclude ) ) { console . error ( `Config "exclude" entry must contain an array.` ) ; } else { const filesList = configObj . exclude as string [ ] ; filesList . forEach ( ( fileSpec , index ) => { if ( typeof fileSpec !== 'string' ) { console . error ( `Index ${ index } of "exclude" array should be a string.` ) ; } else if ( isAbsolute ( fileSpec ) ) { console . error ( `Ignoring path "${ fileSpec }" in "exclude" array because it is not relative.` ) ; } else { this . exclude . push ( getFileSpec ( this . projectRoot , fileSpec ) ) ; } } ) ; } } this . ignore = [ ] ; if ( configObj . ignore !== undefined ) { if ( ! Array . isArray ( configObj . ignore ) ) { console . error ( `Config "ignore" entry must contain an array.` ) ; } else { const filesList = configObj . ignore as string [ ] ; filesList . forEach ( ( fileSpec , index ) => { if ( typeof fileSpec !== 'string' ) { console . error ( `Index ${ index } of "ignore" array should be a string.` ) ; } else if ( isAbsolute ( fileSpec ) ) { console . error ( `Ignoring path "${ fileSpec }" in "ignore" array because it is not relative.` ) ; } else { this . ignore . push ( getFileSpec ( this . projectRoot , fileSpec ) ) ; } } ) ; } } this . strict = [ ] ; if ( configObj . strict !== undefined ) { if ( ! Array . isArray ( configObj . strict ) ) { console . error ( `Config "strict" entry must contain an array.` ) ; } else { const filesList = configObj . strict as string [ ] ; filesList . forEach ( ( fileSpec , index ) => { if ( typeof fileSpec !== 'string' ) { console . error ( `Index ${ index } of "strict" array should be a string.` ) ; } else if ( isAbsolute ( fileSpec ) ) { console . error ( `Ignoring path "${ fileSpec }" in "strict" array because it is not relative.` ) ; } else { this . strict . push ( getFileSpec ( this . projectRoot , fileSpec ) ) ; } } ) ; } } let configTypeCheckingMode ; if ( configObj . typeCheckingMode !== undefined ) { if ( configObj . typeCheckingMode === 'off' || configObj . typeCheckingMode === 'basic' || configObj . typeCheckingMode === 'strict' ) { configTypeCheckingMode = configObj . typeCheckingMode ; } else { console . error ( `Config "typeCheckingMode" entry must contain "off", "basic", or "strict".` ) ; } } if ( configObj . useLibraryCodeForTypes !== undefined ) { if ( typeof configObj . useLibraryCodeForTypes === 'boolean' ) { this . useLibraryCodeForTypes = configObj . useLibraryCodeForTypes ; } else { console . error ( `Config "useLibraryCodeForTypes" entry must be true or false.` ) ; } } this . typeCheckingMode = configTypeCheckingMode || typeCheckingMode ; const defaultSettings = ConfigOptions . getDiagnosticRuleSet ( this . typeCheckingMode ) ; if ( this . typeCheckingMode === 'off' ) { this . disableInferenceForPyTypedSources = false ; } this . applyDiagnosticOverrides ( diagnosticOverrides ) ; this . diagnosticRuleSet = { printUnknownAsAny : defaultSettings . printUnknownAsAny , omitTypeArgsIfAny : defaultSettings . omitTypeArgsIfAny , omitConditionalConstraint : defaultSettings . omitConditionalConstraint , omitUnannotatedParamType : defaultSettings . omitUnannotatedParamType , pep604Printing : defaultSettings . pep604Printing , strictListInference : this . _convertBoolean ( configObj . strictListInference , DiagnosticRule . strictListInference , defaultSettings . strictListInference ) , strictSetInference : this . _convertBoolean ( configObj . strictSetInference , DiagnosticRule . strictSetInference , defaultSettings . strictSetInference ) , strictDictionaryInference : this . _convertBoolean ( configObj . strictDictionaryInference , DiagnosticRule . strictDictionaryInference , defaultSettings . strictDictionaryInference ) , strictParameterNoneValue : this . _convertBoolean ( configObj . strictParameterNoneValue , DiagnosticRule . strictParameterNoneValue , defaultSettings . strictParameterNoneValue ) , enableTypeIgnoreComments : this . _convertBoolean ( configObj . enableTypeIgnoreComments , DiagnosticRule . enableTypeIgnoreComments , defaultSettings . enableTypeIgnoreComments ) , reportGeneralTypeIssues : this . _convertDiagnosticLevel ( configObj . reportGeneralTypeIssues , DiagnosticRule . reportGeneralTypeIssues , defaultSettings . reportGeneralTypeIssues ) , reportPropertyTypeMismatch : this . _convertDiagnosticLevel ( configObj . reportPropertyTypeMismatch , DiagnosticRule . reportPropertyTypeMismatch , defaultSettings . reportPropertyTypeMismatch ) , reportFunctionMemberAccess : this . _convertDiagnosticLevel ( configObj . reportFunctionMemberAccess , DiagnosticRule . reportFunctionMemberAccess , defaultSettings . reportFunctionMemberAccess ) , reportMissingImports : this . _convertDiagnosticLevel ( configObj . reportMissingImports , DiagnosticRule . reportMissingImports , defaultSettings . reportMissingImports ) , reportUnusedImport : this . _convertDiagnosticLevel ( configObj . reportUnusedImport , DiagnosticRule . reportUnusedImport , defaultSettings . reportUnusedImport ) , reportUnusedClass : this . _convertDiagnosticLevel ( configObj . reportUnusedClass , DiagnosticRule . reportUnusedClass , defaultSettings . reportUnusedClass ) , reportUnusedFunction : this . _convertDiagnosticLevel ( configObj . reportUnusedFunction , DiagnosticRule . reportUnusedFunction , defaultSettings . reportUnusedFunction ) , reportUnusedVariable : this . _convertDiagnosticLevel ( configObj . reportUnusedVariable , DiagnosticRule . reportUnusedVariable , defaultSettings . reportUnusedVariable ) , reportDuplicateImport : this . _convertDiagnosticLevel ( configObj . reportDuplicateImport , DiagnosticRule . reportDuplicateImport , defaultSettings . reportDuplicateImport ) , reportWildcardImportFromLibrary : this . _convertDiagnosticLevel ( configObj . reportWildcardImportFromLibrary , DiagnosticRule . reportWildcardImportFromLibrary , defaultSettings . reportWildcardImportFromLibrary ) , reportMissingModuleSource : this . _convertDiagnosticLevel ( configObj . reportMissingModuleSource , DiagnosticRule . reportMissingModuleSource , defaultSettings . reportMissingModuleSource ) , reportMissingTypeStubs : this . _convertDiagnosticLevel ( configObj . reportMissingTypeStubs , DiagnosticRule . reportMissingTypeStubs , defaultSettings . reportMissingTypeStubs ) , reportImportCycles : this . _convertDiagnosticLevel ( configObj . reportImportCycles , DiagnosticRule . reportImportCycles , defaultSettings . reportImportCycles ) , reportOptionalSubscript : this . _convertDiagnosticLevel ( configObj . reportOptionalSubscript , DiagnosticRule . reportOptionalSubscript , defaultSettings . reportOptionalSubscript ) , reportOptionalMemberAccess : this . _convertDiagnosticLevel ( configObj . reportOptionalMemberAccess , DiagnosticRule . reportOptionalMemberAccess , defaultSettings . reportOptionalMemberAccess ) , reportOptionalCall : this . _convertDiagnosticLevel ( configObj . reportOptionalCall , DiagnosticRule . reportOptionalCall , defaultSettings . reportOptionalCall ) , reportOptionalIterable : this . _convertDiagnosticLevel ( configObj . reportOptionalIterable , DiagnosticRule . reportOptionalIterable , defaultSettings . reportOptionalIterable ) , reportOptionalContextManager : this . _convertDiagnosticLevel ( configObj . reportOptionalContextManager , DiagnosticRule . reportOptionalContextManager , defaultSettings . reportOptionalContextManager ) , reportOptionalOperand : this . _convertDiagnosticLevel ( configObj . reportOptionalOperand , DiagnosticRule . reportOptionalOperand , defaultSettings . reportOptionalOperand ) , reportTypedDictNotRequiredAccess : this . _convertDiagnosticLevel ( configObj . reportTypedDictNotRequiredAccess , DiagnosticRule . reportTypedDictNotRequiredAccess , defaultSettings . reportTypedDictNotRequiredAccess ) , reportUntypedFunctionDecorator : this . _convertDiagnosticLevel ( configObj . reportUntypedFunctionDecorator , DiagnosticRule . reportUntypedFunctionDecorator , defaultSettings . reportUntypedFunctionDecorator ) , reportUntypedClassDecorator : this . _convertDiagnosticLevel ( configObj . reportUntypedClassDecorator , DiagnosticRule . reportUntypedClassDecorator , defaultSettings . reportUntypedClassDecorator ) , reportUntypedBaseClass : this . _convertDiagnosticLevel ( configObj . reportUntypedBaseClass , DiagnosticRule . reportUntypedBaseClass , defaultSettings . reportUntypedBaseClass ) , reportUntypedNamedTuple : this . _convertDiagnosticLevel ( configObj . reportUntypedNamedTuple , DiagnosticRule . reportUntypedNamedTuple , defaultSettings . reportUntypedNamedTuple ) , reportPrivateUsage : this . _convertDiagnosticLevel ( configObj . reportPrivateUsage , DiagnosticRule . reportPrivateUsage , defaultSettings . reportPrivateUsage ) , reportPrivateImportUsage : this . _convertDiagnosticLevel ( configObj . reportPrivateImportUsage , DiagnosticRule . reportPrivateImportUsage , defaultSettings . reportPrivateImportUsage ) , reportConstantRedefinition : this . _convertDiagnosticLevel ( configObj . reportConstantRedefinition , DiagnosticRule . reportConstantRedefinition , defaultSettings . reportConstantRedefinition ) , reportIncompatibleMethodOverride : this . _convertDiagnosticLevel ( configObj . reportIncompatibleMethodOverride , DiagnosticRule . reportIncompatibleMethodOverride , defaultSettings . reportIncompatibleMethodOverride ) , reportIncompatibleVariableOverride : this . _convertDiagnosticLevel ( configObj . reportIncompatibleVariableOverride , DiagnosticRule . reportIncompatibleVariableOverride , defaultSettings . reportIncompatibleVariableOverride ) , reportOverlappingOverload : this . _convertDiagnosticLevel ( configObj . reportOverlappingOverload , DiagnosticRule . reportOverlappingOverload , defaultSettings . reportOverlappingOverload ) , reportUninitializedInstanceVariable : this . _convertDiagnosticLevel ( configObj . reportUninitializedInstanceVariable , DiagnosticRule . reportUninitializedInstanceVariable , defaultSettings . reportUninitializedInstanceVariable ) , reportInvalidStringEscapeSequence : this . _convertDiagnosticLevel ( configObj . reportInvalidStringEscapeSequence , DiagnosticRule . reportInvalidStringEscapeSequence , defaultSettings . reportInvalidStringEscapeSequence ) , reportUnknownParameterType : this . _convertDiagnosticLevel ( configObj . reportUnknownParameterType , DiagnosticRule . reportUnknownParameterType , defaultSettings . reportUnknownParameterType ) , reportUnknownArgumentType : this . _convertDiagnosticLevel ( configObj . reportUnknownArgumentType , DiagnosticRule . reportUnknownArgumentType , defaultSettings . reportUnknownArgumentType ) , reportUnknownLambdaType : this . _convertDiagnosticLevel ( configObj . reportUnknownLambdaType , DiagnosticRule . reportUnknownLambdaType , defaultSettings . reportUnknownLambdaType ) , reportUnknownVariableType : this . _convertDiagnosticLevel ( configObj . reportUnknownVariableType , DiagnosticRule . reportUnknownVariableType , defaultSettings . reportUnknownVariableType ) , reportUnknownMemberType : this . _convertDiagnosticLevel ( configObj . reportUnknownMemberType , DiagnosticRule . reportUnknownMemberType , defaultSettings . reportUnknownMemberType ) , reportMissingParameterType : this . _convertDiagnosticLevel ( configObj . reportMissingParameterType , DiagnosticRule . reportMissingParameterType , defaultSettings . reportMissingParameterType ) , reportMissingTypeArgument : this . _convertDiagnosticLevel ( configObj . reportMissingTypeArgument , DiagnosticRule . reportMissingTypeArgument , defaultSettings . reportMissingTypeArgument ) , reportInvalidTypeVarUse : this . _convertDiagnosticLevel ( configObj . reportInvalidTypeVarUse , DiagnosticRule . reportInvalidTypeVarUse , defaultSettings . reportInvalidTypeVarUse ) , reportCallInDefaultInitializer : this . _convertDiagnosticLevel ( configObj . reportCallInDefaultInitializer , DiagnosticRule . reportCallInDefaultInitializer , defaultSettings . reportCallInDefaultInitializer ) , reportUnnecessaryIsInstance : this . _convertDiagnosticLevel ( configObj . reportUnnecessaryIsInstance , DiagnosticRule . reportUnnecessaryIsInstance , defaultSettings . reportUnnecessaryIsInstance ) , reportUnnecessaryCast : this . _convertDiagnosticLevel ( configObj . reportUnnecessaryCast , DiagnosticRule . reportUnnecessaryCast , defaultSettings . reportUnnecessaryCast ) , reportUnnecessaryComparison : this . _convertDiagnosticLevel ( configObj . reportUnnecessaryComparison , DiagnosticRule . reportUnnecessaryComparison , defaultSettings . reportUnnecessaryComparison ) , reportAssertAlwaysTrue : this . _convertDiagnosticLevel ( configObj . reportAssertAlwaysTrue , DiagnosticRule . reportAssertAlwaysTrue , defaultSettings . reportAssertAlwaysTrue ) , reportSelfClsParameterName : this . _convertDiagnosticLevel ( configObj . reportSelfClsParameterName , DiagnosticRule . reportSelfClsParameterName , defaultSettings . reportSelfClsParameterName ) , reportImplicitStringConcatenation : this . _convertDiagnosticLevel ( configObj . reportImplicitStringConcatenation , DiagnosticRule . reportImplicitStringConcatenation , defaultSettings . reportImplicitStringConcatenation ) , reportUndefinedVariable : this . _convertDiagnosticLevel ( configObj . reportUndefinedVariable , DiagnosticRule . reportUndefinedVariable , defaultSettings . reportUndefinedVariable ) , reportUnboundVariable : this . _convertDiagnosticLevel ( configObj . reportUnboundVariable , DiagnosticRule . reportUnboundVariable , defaultSettings . reportUnboundVariable ) , reportInvalidStubStatement : this . _convertDiagnosticLevel ( configObj . reportInvalidStubStatement , DiagnosticRule . reportInvalidStubStatement , defaultSettings . reportInvalidStubStatement ) , reportIncompleteStub : this . _convertDiagnosticLevel ( configObj . reportIncompleteStub , DiagnosticRule . reportIncompleteStub , defaultSettings . reportIncompleteStub ) , reportUnsupportedDunderAll : this . _convertDiagnosticLevel ( configObj . reportUnsupportedDunderAll , DiagnosticRule . reportUnsupportedDunderAll , defaultSettings . reportUnsupportedDunderAll ) , reportUnusedCallResult : this . _convertDiagnosticLevel ( configObj . reportUnusedCallResult , DiagnosticRule . reportUnusedCallResult , defaultSettings . reportUnusedCallResult ) , reportUnusedCoroutine : this . _convertDiagnosticLevel ( configObj . reportUnusedCoroutine , DiagnosticRule . reportUnusedCoroutine , defaultSettings . reportUnusedCoroutine ) , } ; this . venvPath = undefined ; if ( configObj . venvPath !== undefined ) { if ( typeof configObj . venvPath !== 'string' ) { console . error ( `Config "venvPath" field must contain a string.` ) ; } else { this . venvPath = normalizePath ( combinePaths ( this . projectRoot , configObj . venvPath ) ) ; } } this . venv = undefined ; if ( configObj . venv !== undefined ) { if ( typeof configObj . venv !== 'string' ) { console . error ( `Config "venv" field must contain a string.` ) ; } else { this . venv = configObj . venv ; } } if ( configObj . extraPaths !== undefined ) { this . defaultExtraPaths = [ ] ; if ( ! Array . isArray ( configObj . extraPaths ) ) { console . error ( `Config "extraPaths" field must contain an array.` ) ; } else { const pathList = configObj . extraPaths as string [ ] ; pathList . forEach ( ( path , pathIndex ) => { if ( typeof path !== 'string' ) { console . error ( `Config "extraPaths" field ${ pathIndex } must be a string.` ) ; } else { this . defaultExtraPaths ! . push ( normalizePath ( combinePaths ( this . projectRoot , path ) ) ) ; } } ) ; } } if ( configObj . pythonVersion !== undefined ) { if ( typeof configObj . pythonVersion === 'string' ) { const version = versionFromString ( configObj . pythonVersion ) ; if ( version ) { this . defaultPythonVersion = version ; } else { console . error ( `Config "pythonVersion" field contains unsupported version.` ) ; } } else { console . error ( `Config "pythonVersion" field must contain a string.` ) ; } } this . ensureDefaultPythonVersion ( host , console ) ; if ( configObj . pythonPlatform !== undefined ) { if ( typeof configObj . pythonPlatform !== 'string' ) { console . error ( `Config "pythonPlatform" field must contain a string.` ) ; } else { this . defaultPythonPlatform = configObj . pythonPlatform ; } } this . ensureDefaultPythonPlatform ( host , console ) ; this . typeshedPath = undefined ; if ( configObj . typeshedPath !== undefined ) { if ( typeof configObj . typeshedPath !== 'string' ) { console . error ( `Config "typeshedPath" field must contain a string.` ) ; } else { this . typeshedPath = configObj . typeshedPath ? normalizePath ( combinePaths ( this . projectRoot , configObj . typeshedPath ) ) : '' ; } } this . stubPath = undefined ; if ( configObj . typingsPath !== undefined ) { if ( typeof configObj . typingsPath !== 'string' ) { console . error ( `Config "typingsPath" field must contain a string.` ) ; } else { console . error ( `Config "typingsPath" is now deprecated. Please, use stubPath instead.` ) ; this . stubPath = normalizePath ( combinePaths ( this . projectRoot , configObj . typingsPath ) ) ; } } if ( configObj . stubPath !== undefined ) { if ( typeof configObj . stubPath !== 'string' ) { console . error ( `Config "stubPath" field must contain a string.` ) ; } else { this . stubPath = normalizePath ( combinePaths ( this . projectRoot , configObj . stubPath ) ) ; } } if ( configObj . verboseOutput !== undefined ) { if ( typeof configObj . verboseOutput !== 'boolean' ) { console . error ( `Config "verboseOutput" field must be true or false.` ) ; } else { this . verboseOutput = configObj . verboseOutput ; } } if ( configObj . useLibraryCodeForTypes !== undefined ) { if ( typeof configObj . useLibraryCodeForTypes !== 'boolean' ) { console . error ( `Config "useLibraryCodeForTypes" field must be true or false.` ) ; } else { this . useLibraryCodeForTypes = configObj . useLibraryCodeForTypes ; } } this . executionEnvironments = [ ] ; if ( configObj . executionEnvironments !== undefined ) { if ( ! Array . isArray ( configObj . executionEnvironments ) ) { console . error ( `Config "executionEnvironments" field must contain an array.` ) ; } else { const execEnvironments = configObj . executionEnvironments as ExecutionEnvironment [ ] ; execEnvironments . forEach ( ( env , index ) => { const execEnv = this . _initExecutionEnvironmentFromJson ( env , index , console ) ; if ( execEnv ) { this . executionEnvironments . push ( execEnv ) ; } } ) ; } } if ( configObj . autoImportCompletions !== undefined ) { if ( typeof configObj . autoImportCompletions !== 'boolean' ) { console . error ( `Config "autoImportCompletions" field must be true or false.` ) ; } else { this . autoImportCompletions = configObj . autoImportCompletions ; } } if ( configObj . indexing !== undefined ) { if ( typeof configObj . indexing !== 'boolean' ) { console . error ( `Config "indexing" field must be true or false.` ) ; } else { this . indexing = configObj . indexing ; } } if ( configObj . logTypeEvaluationTime !== undefined ) { if ( typeof configObj . logTypeEvaluationTime !== 'boolean' ) { console . error ( `Config "logTypeEvaluationTime" field must be true or false.` ) ; } else { this . logTypeEvaluationTime = configObj . logTypeEvaluationTime ; } } if ( configObj . typeEvaluationTimeThreshold !== undefined ) { if ( typeof configObj . typeEvaluationTimeThreshold !== 'number' ) { console . error ( `Config "typeEvaluationTimeThreshold" field must be a number.` ) ; } else { this . typeEvaluationTimeThreshold = configObj . typeEvaluationTimeThreshold ; } } } ensureDefaultPythonPlatform ( host , console ) { if ( this . defaultPythonPlatform !== undefined ) { return ; } this . defaultPythonPlatform = host . getPythonPlatform ( ) ; if ( this . defaultPythonPlatform !== undefined ) { console . info ( `Assuming Python platform ${ this . defaultPythonPlatform }` ) ; } } ensureDefaultPythonVersion ( host , console ) { if ( this . defaultPythonVersion !== undefined ) { return ; } const importFailureInfo = [ ] ; this . defaultPythonVersion = host . getPythonVersion ( this . pythonPath , importFailureInfo ) ; if ( this . defaultPythonVersion !== undefined ) { console . info ( `Assuming Python version ${ versionToString ( this . defaultPythonVersion ) }` ) ; } for ( const log of importFailureInfo ) { console . info ( log ) ; } } ensureDefaultExtraPaths ( fs , autoSearchPaths , extraPaths ) { const paths = [ ] ; if ( autoSearchPaths ) { const srcPath = resolvePaths ( this . projectRoot , pathConsts . src ) ; if ( fs . existsSync ( srcPath ) && ! fs . existsSync ( resolvePaths ( srcPath , '__init__.py' ) ) ) { paths . push ( srcPath ) ; } } if ( extraPaths && extraPaths . length > 0 ) { for ( const p of extraPaths ) { const path = resolvePaths ( this . projectRoot , p ) ; paths . push ( path ) ; if ( isDirectory ( fs , path ) ) { paths . push ( ... getPathsFromPthFiles ( fs , path ) ) ; } } } if ( paths . length > 0 ) { this . defaultExtraPaths = paths ; } } applyDiagnosticOverrides ( diagnosticSeverityOverrides ) { if ( ! diagnosticSeverityOverrides ) { return ; } for ( const [ ruleName , severity ] of Object . entries ( diagnosticSeverityOverrides ) ) { ( this . diagnosticRuleSet as any ) [ ruleName ] = severity ; } } private _convertBoolean ( value , fieldName , defaultValue ) { if ( value === undefined ) { return defaultValue ; } else if ( typeof value === 'boolean' ) { return value ? true : false ; } console . log ( `Config "${ fieldName }" entry must be true or false.` ) ; return defaultValue ; } private _convertDiagnosticLevel ( value , fieldName , defaultValue ) { if ( value === undefined ) { return defaultValue ; } else if ( typeof value === 'boolean' ) { return value ? 'error' : 'none' ; } else if ( typeof value === 'string' ) { if ( value === 'error' || value === 'warning' || value === 'information' || value === 'none' ) { return value ; } } console . log ( `Config "${ fieldName }" entry must be true, false, "error", "warning", "information" or "none".` ) ; return defaultValue ; } private _initExecutionEnvironmentFromJson ( envObj , index , console ) { try { const newExecEnv = new ExecutionEnvironment ( this . projectRoot , this . defaultPythonVersion , this . defaultPythonPlatform , this . defaultExtraPaths ) ; if ( envObj . root && typeof envObj . root === 'string' ) { newExecEnv . root = normalizePath ( combinePaths ( this . projectRoot , envObj . root ) ) ; } else { console . error ( `Config executionEnvironments index ${ index }: missing root value.` ) ; } if ( envObj . extraPaths ) { if ( ! Array . isArray ( envObj . extraPaths ) ) { console . error ( `Config executionEnvironments index ${ index }: extraPaths field must contain an array.` ) ; } else { const pathList = envObj . extraPaths as string [ ] ; pathList . forEach ( ( path , pathIndex ) => { if ( typeof path !== 'string' ) { console . error ( `Config executionEnvironments index ${ index }:` + ` extraPaths field ${ pathIndex } must be a string.` ) ; } else { newExecEnv . extraPaths . push ( normalizePath ( combinePaths ( this . projectRoot , path ) ) ) ; } } ) ; } } if ( envObj . pythonVersion ) { if ( typeof envObj . pythonVersion === 'string' ) { const version = versionFromString ( envObj . pythonVersion ) ; if ( version ) { newExecEnv . pythonVersion = version ; } else { console . warn ( `Config executionEnvironments index ${ index } contains unsupported pythonVersion.` ) ; } } else { console . error ( `Config executionEnvironments index ${ index } pythonVersion must be a string.` ) ; } } if ( envObj . pythonPlatform ) { if ( typeof envObj . pythonPlatform === 'string' ) { newExecEnv . pythonPlatform = envObj . pythonPlatform ; } else { console . error ( `Config executionEnvironments index ${ index } pythonPlatform must be a string.` ) ; } } return newExecEnv ; } catch { console . error ( `Config executionEnvironments index ${ index } is not accessible.` ) ; } return undefined ; } }