File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
packages/pyright-internal/src/analyzer Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -425,6 +425,33 @@ export class ImportResolver {
425
425
return this . _cachedPythonSearchPaths . paths ;
426
426
}
427
427
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
+
428
455
protected readdirEntriesCached ( path : string ) : Dirent [ ] {
429
456
const cachedValue = this . _cachedEntriesForPath . get ( path ) ;
430
457
if ( cachedValue ) {
Original file line number Diff line number Diff line change @@ -681,6 +681,19 @@ export class AnalyzerService {
681
681
configOptions . applyDiagnosticOverrides ( commandLineOptions . diagnosticSeverityOverrides ) ;
682
682
}
683
683
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
+
684
697
// Override the analyzeUnannotatedFunctions setting based on the command-line setting.
685
698
if ( commandLineOptions . analyzeUnannotatedFunctions !== undefined ) {
686
699
configOptions . diagnosticRuleSet . analyzeUnannotatedFunctions =
You can’t perform that action at this time.
0 commit comments