From ad17925f503582017390bdb2d52eceb92952cf5d Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 19 Nov 2019 21:50:27 -0800 Subject: [PATCH] Added new VS Code experimental setting "useLibraryCodeForTypes". If enabled, pyright will attempt to extract type information from library implementation files. --- client/package.json | 8 +++++++- docs/settings.md | 2 ++ server/src/analyzer/program.ts | 6 +----- server/src/analyzer/service.ts | 1 + server/src/common/commandLineOptions.ts | 4 ++++ server/src/common/configOptions.ts | 4 ++++ server/src/pyright.ts | 1 + server/src/server.ts | 3 +++ 8 files changed, 23 insertions(+), 6 deletions(-) diff --git a/client/package.json b/client/package.json index bfaef0f5c..fa7e46fa2 100644 --- a/client/package.json +++ b/client/package.json @@ -91,7 +91,13 @@ "pyright.openFilesOnly": { "type": "boolean", "default": true, - "description": "Report errors for all files in the workspace or only currently-open files?", + "description": "Report errors only for currently-open files.", + "scope": "resource" + }, + "pyright.useLibraryCodeForTypes": { + "type": "boolean", + "default": false, + "description": "In the absence of type stub files, use library implementations to extract type information.", "scope": "resource" } } diff --git a/docs/settings.md b/docs/settings.md index 69a6b3f7a..ecb96a6b2 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -6,6 +6,8 @@ The Pyright VS Code extension honors the following settings. **pyright.openFilesOnly** [boolean]: Determines whether pyright analyzes (and reports errors for) all files in the workspace, as indicated by the config file. If this option is set to true, pyright analyzes only open files. +**pyright.useLibraryCodeForTypes** [boolean]: Determines whether pyright reads, parses and analyzes library code to extract type information in the absence of type stub files. This can add significant overhead and may result in poor-quality type information. The default value for this option is false. + **python.analysis.typeshedPaths** [array of paths]: Paths to look for typeshed modules. Pyright currently honors only the first path in the array. **python.pythonPath** [path]: Path to Python interpreter. diff --git a/server/src/analyzer/program.ts b/server/src/analyzer/program.ts index dd95def07..c3b1cb215 100644 --- a/server/src/analyzer/program.ts +++ b/server/src/analyzer/program.ts @@ -36,10 +36,6 @@ import { TypeStubWriter } from './typeStubWriter'; const _maxImportDepth = 256; -// We may enable this through a switch in future versions. For now, -// disable analysis of third-party library implementations. -const _allowAllThirdPartyImports = false; - export interface SourceFileInfo { sourceFile: SourceFile; isTracked: boolean; @@ -968,7 +964,7 @@ export class Program { private _isImportAllowed(importer: SourceFileInfo, importResult: ImportResult, isImportStubFile: boolean): boolean { - let thirdPartyImportAllowed = _allowAllThirdPartyImports; + let thirdPartyImportAllowed = this._configOptions.useLibraryCodeForTypes; if (importResult.importType === ImportType.ThirdParty || (importer.isThirdPartyImport && importResult.importType === ImportType.Local)) { diff --git a/server/src/analyzer/service.ts b/server/src/analyzer/service.ts index c2401729e..86a66085a 100644 --- a/server/src/analyzer/service.ts +++ b/server/src/analyzer/service.ts @@ -328,6 +328,7 @@ export class AnalyzerService { configOptions.verboseOutput = !!commandLineOptions.verboseOutput; configOptions.checkOnlyOpenFiles = !!commandLineOptions.checkOnlyOpenFiles; + configOptions.useLibraryCodeForTypes = !!commandLineOptions.useLibraryCodeForTypes; // Do some sanity checks on the specified settings and report missing // or inconsistent information. diff --git a/server/src/common/commandLineOptions.ts b/server/src/common/commandLineOptions.ts index 759cf1afb..2cd1037ad 100644 --- a/server/src/common/commandLineOptions.ts +++ b/server/src/common/commandLineOptions.ts @@ -49,6 +49,10 @@ export class CommandLineOptions { // Indicates that only open files should be checked. checkOnlyOpenFiles?: boolean; + // In the absence of type stubs, use library implementations + // to extract type information? + useLibraryCodeForTypes?: boolean; + // Indicates that the settings came from VS Code rather than // from the command-line. Useful for providing clearer error // messages. diff --git a/server/src/common/configOptions.ts b/server/src/common/configOptions.ts index cdac02ff0..7c65db894 100644 --- a/server/src/common/configOptions.ts +++ b/server/src/common/configOptions.ts @@ -332,6 +332,10 @@ export class ConfigOptions { // Perform type checking and report diagnostics only for open files? checkOnlyOpenFiles: boolean; + // In the absence of type stubs, use library implementations to extract + // type information? + useLibraryCodeForTypes: boolean; + //--------------------------------------------------------------- // Diagnostics Settings diff --git a/server/src/pyright.ts b/server/src/pyright.ts index 4c634f71d..52f78e503 100644 --- a/server/src/pyright.ts +++ b/server/src/pyright.ts @@ -110,6 +110,7 @@ function processArgs() { options.verboseOutput = !!args.verbose; options.checkOnlyOpenFiles = false; + options.useLibraryCodeForTypes = false; const watch = args.watch !== undefined; options.watch = watch; diff --git a/server/src/server.ts b/server/src/server.ts index 508f2616a..98151041f 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -33,6 +33,7 @@ interface PythonSettings { interface PyrightSettings { disableLanguageServices?: boolean; openFilesOnly?: boolean; + useLibraryCodeForTypes?: boolean; } interface WorkspaceServiceInstance { @@ -618,6 +619,8 @@ function updateOptionsAndRestartService(workspace: WorkspaceServiceInstance, commandLineOptions.watch = true; commandLineOptions.checkOnlyOpenFiles = pyrightSettings ? !!pyrightSettings.openFilesOnly : true; + commandLineOptions.useLibraryCodeForTypes = pyrightSettings ? + !!pyrightSettings.useLibraryCodeForTypes : false; if (pythonSettings.venvPath) { commandLineOptions.venvPath = combinePaths(workspace.rootPath || _rootPath,