Skip to content

Commit 2c97966

Browse files
committed
Added auto-exclude logic specifically for typeshed so stdlib stubs that are not "in scope" for the current python version are auto-excluded from the project. This applies only if the typeshedPath is set to .. This addresses python/typeshed#10258.
1 parent d345e20 commit 2c97966

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/pyright-internal/src/analyzer/importResolver.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,33 @@ export class ImportResolver {
425425
return this._cachedPythonSearchPaths.paths;
426426
}
427427

428+
getTypeshedStdlibExcludeList(execEnv: ExecutionEnvironment): string[] {
429+
const typeshedStdlibPath = this.getTypeshedStdLibPath(execEnv);
430+
const excludes: string[] = [];
431+
432+
if (!typeshedStdlibPath) {
433+
return excludes;
434+
}
435+
436+
if (!this._cachedTypeshedStdLibModuleVersions) {
437+
this._cachedTypeshedStdLibModuleVersions = this._readTypeshedStdLibVersions(execEnv, []);
438+
}
439+
440+
this._cachedTypeshedStdLibModuleVersions.forEach((versionRange, moduleName) => {
441+
if (versionRange.max !== undefined && execEnv.pythonVersion > versionRange.max) {
442+
// Add excludes for both the ".pyi" file and the directory that contains it
443+
// (in case it's using a "__init__.pyi" file).
444+
const moduleDirPath = combinePaths(typeshedStdlibPath, ...moduleName.split('.'));
445+
excludes.push(moduleDirPath);
446+
447+
const moduleFilePath = moduleDirPath + '.pyi';
448+
excludes.push(moduleFilePath);
449+
}
450+
});
451+
452+
return excludes;
453+
}
454+
428455
protected readdirEntriesCached(path: string): Dirent[] {
429456
const cachedValue = this._cachedEntriesForPath.get(path);
430457
if (cachedValue) {

packages/pyright-internal/src/analyzer/service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,19 @@ export class AnalyzerService {
681681
configOptions.applyDiagnosticOverrides(commandLineOptions.diagnosticSeverityOverrides);
682682
}
683683

684+
// If the caller specified that "typeshedPath" is the root of the project,
685+
// then we're presumably running in the typeshed project itself. Auto-exclude
686+
// stdlib packages that don't match the current Python version.
687+
if (configOptions.typeshedPath === projectRoot && configOptions.defaultPythonVersion !== undefined) {
688+
const excludeList = this.getImportResolver().getTypeshedStdlibExcludeList(
689+
configOptions.getDefaultExecEnvironment()
690+
);
691+
692+
excludeList.forEach((exclude) => {
693+
configOptions.exclude.push(getFileSpec(this.fs, commandLineOptions.executionRoot, exclude));
694+
});
695+
}
696+
684697
// Override the analyzeUnannotatedFunctions setting based on the command-line setting.
685698
if (commandLineOptions.analyzeUnannotatedFunctions !== undefined) {
686699
configOptions.diagnosticRuleSet.analyzeUnannotatedFunctions =

0 commit comments

Comments
 (0)