Skip to content

Commit

Permalink
Pull Pylance with Pyright 1.1.382 (#9077)
Browse files Browse the repository at this point in the history
* Push pylance changes to pyright

* Update jest.config.js
  • Loading branch information
StellaHuang95 authored Sep 26, 2024
1 parent da667e6 commit e46efc6
Show file tree
Hide file tree
Showing 33 changed files with 11,583 additions and 24,404 deletions.
21,981 changes: 9,522 additions & 12,459 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
},
"devDependencies": {
"@types/glob": "^7.2.0",
"@types/node": "^17.0.45",
"@types/node": "^22.7.0",
"@types/yargs": "^16.0.9",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint": "^8.57.1",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"glob": "^7.2.3",
"jsonc-parser": "^3.2.1",
"lerna": "^7.4.2",
"npm-check-updates": "^16.14.14",
"jsonc-parser": "^3.3.1",
"lerna": "^8.1.8",
"npm-check-updates": "^16.14.20",
"p-queue": "^6.6.2",
"prettier": "2.8.8",
"syncpack": "~10.9.3",
Expand Down
6,114 changes: 911 additions & 5,203 deletions packages/pyright-internal/package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions packages/pyright-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"chalk": "^4.1.2",
"chokidar": "^3.6.0",
"command-line-args": "^5.2.1",
"jsonc-parser": "^3.2.1",
"jsonc-parser": "^3.3.1",
"leven": "3.1.0",
"source-map-support": "^0.5.21",
"tmp": "^0.2.1",
"tmp": "^0.2.3",
"vscode-jsonrpc": "^9.0.0-next.5",
"vscode-languageserver": "^10.0.0-next.10",
"vscode-languageserver-protocol": "^3.17.6-next.10",
Expand All @@ -39,19 +39,19 @@
"devDependencies": {
"@types/command-line-args": "^5.2.3",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.202",
"@types/node": "^17.0.45",
"@types/jest": "^29.5.13",
"@types/lodash": "^4.17.7",
"@types/node": "^22.7.0",
"@types/tmp": "^0.2.6",
"copy-webpack-plugin": "^11.0.0",
"esbuild-loader": "^3.2.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"shx": "^0.3.4",
"ts-jest": "^29.1.2",
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.1",
"typescript": "~5.5.4",
"webpack": "^5.90.1",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
"word-wrap": "1.2.5"
}
Expand Down
3 changes: 0 additions & 3 deletions packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1514,9 +1514,6 @@ export class AnalyzerService {
// this can affect how we resolve imports. This requires us to reset caches and reanalyze everything.
//
// However, we don't need to rebuild any indexes in this situation. Changes to workspace files don't affect library indices.
// As for user files, their indices don't contain import alias symbols, so adding or removing user files won't affect the existing indices.
// We only rebuild the indices for a user file when the symbols within the file are changed, like when a user edits the file.
// The index scanner will index any new files during its next background run.
this.invalidateAndForceReanalysis(InvalidatedReason.SourceWatcherChanged);
this._scheduleReanalysis(/* requireTrackedFileUpdate */ true);
});
Expand Down
15 changes: 14 additions & 1 deletion packages/pyright-internal/src/common/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ export namespace TempFile {
}

export class VirtualDirent implements fs.Dirent {
constructor(public name: string, private _file: boolean) {}
parentPath: string;

constructor(public name: string, private _file: boolean, parentPath: string) {
this.parentPath = parentPath;
}

/**
* Alias for `dirent.parentPath`.
* @since v20.1.0
* @deprecated Since v20.12.0
*/
get path(): string {
return this.parentPath;
}

isFile(): boolean {
return this._file;
Expand Down
3 changes: 3 additions & 0 deletions packages/pyright-internal/src/common/memUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ function getHeapStatisticsFunc(): () => HeapInfo {
does_zap_garbage: 0,
number_of_native_contexts: 0,
number_of_detached_contexts: 0,
total_global_handles_size: 0,
used_global_handles_size: 0,
external_memory: 0,
});
}
export const getHeapStatistics = getHeapStatisticsFunc();
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/common/realFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ export class RealFileSystem implements FileSystem {
if (entry.isFile() && yarnFS.isZip(path)) {
return {
name: entry.name,
parentPath: path,
path: path,
isFile: () => false,
isDirectory: () => true,
isBlockDevice: () => false,
Expand Down
100 changes: 55 additions & 45 deletions packages/pyright-internal/src/languageService/autoImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface AutoImportSymbol {
readonly symbol?: Symbol;
readonly kind?: SymbolKind;
readonly itemKind?: CompletionItemKind;
readonly inDunderAll?: boolean;
readonly hasRedundantAlias?: boolean;
}

export interface ModuleSymbolTable {
Expand All @@ -70,16 +72,26 @@ export interface AutoImportResult {

export interface AutoImportOptions {
readonly patternMatcher?: (pattern: string, name: string) => boolean;
readonly allowVariableInAll?: boolean;
readonly lazyEdit?: boolean;
}

export interface ImportParts {
// The name of the module or symbol including alias from the `import` or `from ... import` statement
readonly importName: string;

// The actual name of the symbol (not alias)
readonly symbolName?: string;

// The name of the module from `from ... import` statement
readonly importFrom?: string;

// Uri of the module
readonly fileUri: Uri;

// The number of dots in the module name, indicating its depth in the module hierarchy
readonly dotCount: number;

// `ModuleNameAndType` of the module.
readonly moduleNameAndType: ModuleNameAndType;
}

Expand All @@ -89,6 +101,10 @@ export interface ImportAliasData {
readonly symbol?: Symbol;
readonly kind?: SymbolKind;
readonly itemKind?: CompletionItemKind;
readonly inDunderAll?: boolean;
readonly hasRedundantAlias?: boolean;

// Uri pointing to the original module that contains the actual symbol that the alias resolves to.
readonly fileUri: Uri;
}

Expand All @@ -97,12 +113,8 @@ export type AutoImportResultMap = Map<string, AutoImportResult[]>;
// Build a map of all modules within this program and the module-
// level scope that contains the symbol table for the module.
export function buildModuleSymbolsMap(files: readonly SourceFileInfo[]): ModuleSymbolMap {
const map = new Map<string, ModuleSymbolTable>();
addModuleSymbolsMap(files, map);
return map;
}
const moduleSymbolMap = new Map<string, ModuleSymbolTable>();

export function addModuleSymbolsMap(files: readonly SourceFileInfo[], moduleSymbolMap: ModuleSymbolMap): void {
files.forEach((file) => {
if (file.shadows.length > 0) {
// There is corresponding stub file. Don't add
Expand Down Expand Up @@ -158,15 +170,16 @@ export function addModuleSymbolsMap(files: readonly SourceFileInfo[], moduleSymb
});
return;
});

return moduleSymbolMap;
}

export class AutoImporter {
private readonly _importStatements: ImportStatements;

constructor(
protected readonly execEnvironment: ExecutionEnvironment,
protected readonly program: ProgramView,
protected readonly importResolver: ImportResolver,
protected readonly execEnvironment: ExecutionEnvironment,
protected readonly parseResults: ParseFileResults,
private readonly _invocationPosition: Position,
private readonly _excludes: CompletionMap,
Expand All @@ -192,6 +205,10 @@ export class AutoImporter {
return results;
}

protected get importResolver(): ImportResolver {
return this.program.importResolver;
}

protected getCompletionItemData(item: CompletionItem): CompletionItemData | undefined {
return fromLSPAny<CompletionItemData>(item.data);
}
Expand Down Expand Up @@ -335,9 +352,7 @@ export class AutoImporter {

const dotCount = StringUtils.getCharacterCount(importSource, '.');
topLevelSymbols.forEach((autoImportSymbol, name) => {
if (
!this._shouldIncludeVariable(autoImportSymbol, name, fileProperties.isStub, !fileProperties.isUserCode)
) {
if (!this.shouldIncludeVariable(autoImportSymbol, name, fileProperties.isStub)) {
return;
}

Expand Down Expand Up @@ -371,6 +386,8 @@ export class AutoImporter {
symbol: autoImportSymbol.symbol,
kind: autoImportSymbol.importAlias.kind,
itemKind: autoImportSymbol.importAlias.itemKind,
inDunderAll: autoImportSymbol.inDunderAll,
hasRedundantAlias: autoImportSymbol.hasRedundantAlias,
fileUri: autoImportSymbol.importAlias.moduleUri,
},
importAliasMap
Expand Down Expand Up @@ -456,19 +473,34 @@ export class AutoImporter {
return { isStub, hasInit, isUserCode: isUserCode(sourceFileInfo) };
}

private _shouldIncludeVariable(
autoImportSymbol: AutoImportSymbol,
name: string,
isStub: boolean,
library: boolean
) {
// If it is not a stub file and symbol is Variable, we only include it if
// name is public constant or type alias unless it is in __all__ for user files.
if (isStub || autoImportSymbol.kind !== SymbolKind.Variable) {
return true;
protected compareImportAliasData(left: ImportAliasData, right: ImportAliasData) {
// Choose a better alias for the same declaration based on where the alias is defined.
// For example, we would prefer alias defined in builtin over defined in user files.
const groupComparison = left.importGroup - right.importGroup;
if (groupComparison !== 0) {
return groupComparison;
}

const dotComparison = left.importParts.dotCount - right.importParts.dotCount;
if (dotComparison !== 0) {
return dotComparison;
}

if (left.symbol && !right.symbol) {
return -1;
}

if (this.options.allowVariableInAll && !library && autoImportSymbol.symbol?.isInDunderAll()) {
if (!left.symbol && right.symbol) {
return 1;
}

return StringUtils.getStringComparer()(left.importParts.importName, right.importParts.importName);
}

protected shouldIncludeVariable(autoImportSymbol: AutoImportSymbol, name: string, isStub: boolean) {
// If it is not a stub file and symbol is Variable, we only include it if
// name is public constant or type alias
if (isStub || autoImportSymbol.kind !== SymbolKind.Variable) {
return true;
}

Expand Down Expand Up @@ -497,7 +529,7 @@ export class AutoImporter {
}

const existingData = map.get(alias.originalName)!;
const comparison = this._compareImportAliasData(existingData, data);
const comparison = this.compareImportAliasData(existingData, data);
if (comparison <= 0) {
// Existing data is better than new one.
return;
Expand All @@ -507,28 +539,6 @@ export class AutoImporter {
map.set(alias.originalName, data);
}

private _compareImportAliasData(left: ImportAliasData, right: ImportAliasData) {
const groupComparison = left.importGroup - right.importGroup;
if (groupComparison !== 0) {
return groupComparison;
}

const dotComparison = left.importParts.dotCount - right.importParts.dotCount;
if (dotComparison !== 0) {
return dotComparison;
}

if (left.symbol && !right.symbol) {
return -1;
}

if (!left.symbol && right.symbol) {
return 1;
}

return StringUtils.getStringComparer()(left.importParts.importName, right.importParts.importName);
}

private _getImportPartsForSymbols(uri: Uri): [string | undefined, ImportGroup, ModuleNameAndType] {
const localImport = this._importStatements.mapByFilePath.get(uri.key);
if (localImport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,8 @@ export class CompletionProvider {
);

return new AutoImporter(
this.execEnv,
this.program,
this.importResolver,
this.execEnv,
this.parseResults,
this.position,
completionMap,
Expand Down
22 changes: 20 additions & 2 deletions packages/pyright-internal/src/languageService/symbolIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,27 @@ export interface IndexAliasData {
readonly itemKind?: CompletionItemKind | undefined;
}

export const enum IndexSymbolVisibilityFlags {
None = 0,

// Indicates that the symbol is visible externally
ExternallyVisible = 1 << 0,

// Indicates that the symbol is included in `__all__`
InDunderAll = 1 << 1,

// Indicates that the symbol is redundantly aliased, such as
// in 'from module import xx as xx' or 'import yy as yy'
RedundantAlias = 1 << 2,
}

export function isIndexSymbolVisibleFlagSet(data: IndexSymbolData, flag: IndexSymbolVisibilityFlags) {
return !!(data.visibilityFlags & flag);
}

export interface IndexSymbolData {
readonly name: string;
readonly externallyVisible: boolean;
readonly visibilityFlags: IndexSymbolVisibilityFlags;
readonly kind: SymbolKind;
readonly itemKind?: CompletionItemKind | undefined;
readonly alias?: IndexAliasData | undefined;
Expand Down Expand Up @@ -169,7 +187,7 @@ function collectSymbolIndexDataForName(

const data: IndexSymbolData = {
name,
externallyVisible,
visibilityFlags: IndexSymbolVisibilityFlags.ExternallyVisible,
kind: symbolKind,
itemKind: convertSymbolKindToCompletionItemKind(symbolKind),
alias: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@
"unionUnpackedTypeVarTuple": "Union nemůže obsahovat rozbalený typ TypeVarTuple.",
"unnecessaryCast": "Nepotřebné volání „cast“; typ už je „{type}“.",
"unnecessaryIsInstanceAlways": "Zbytečné volání isinstance; {testType} je vždy instancí třídy {classType}",
"unnecessaryIsInstanceNever": "Unnecessary isinstance call; \"{testType}\" is never an instance of \"{classType}\"",
"unnecessaryIsInstanceNever": "Zbytečné volání isinstance; {testType} není nikdy instancí třídy {classType}",
"unnecessaryIsSubclassAlways": "Nepotřebné volání issubclass; „{testType}“ je vždy podtřídou třídy „{classType}“",
"unnecessaryIsSubclassNever": "Unnecessary issubclass call; \"{testType}\" is never a subclass of \"{classType}\"",
"unnecessaryIsSubclassNever": "Zbytečné volání issubclass; {testType} není nikdy podtřídou třídy {classType}",
"unnecessaryPyrightIgnore": "Nepotřebný komentář „# pyright: ignore“",
"unnecessaryPyrightIgnoreRule": "Nepotřebné pravidlo # pyright: ignore: {name}",
"unnecessaryTypeIgnore": "Nepotřebný komentář „# type: ignore“",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@
"unionUnpackedTypeVarTuple": "Die Union kann kein entpacktes TypeVarTuple enthalten.",
"unnecessaryCast": "Nicht erforderlicher \"cast\"-Aufruf; der Typ ist bereits \"{type}\".",
"unnecessaryIsInstanceAlways": "Nicht erforderlicher isinstance-Aufruf; \"{testType}\" ist immer eine Instanz von \"{classType}\"",
"unnecessaryIsInstanceNever": "Unnecessary isinstance call; \"{testType}\" is never an instance of \"{classType}\"",
"unnecessaryIsInstanceNever": "Nicht erforderlicher isinstance-Aufruf; \"{testType}\" ist nie eine Instanz von \"{classType}\"",
"unnecessaryIsSubclassAlways": "Nicht erforderlicher issubclass-Aufruf; \"{testType}\" ist immer eine Unterklasse von \"{classType}\"",
"unnecessaryIsSubclassNever": "Unnecessary issubclass call; \"{testType}\" is never a subclass of \"{classType}\"",
"unnecessaryIsSubclassNever": "Nicht erforderlicher issubclass-Aufruf; \"{testType}\" ist nie eine Unterklasse von \"{classType}\"",
"unnecessaryPyrightIgnore": "Unnötiger \"# pyright: ignore\"-Kommentar",
"unnecessaryPyrightIgnoreRule": "Unnötiger \"# pyright: ignore\"-Regel: \"{name}\"",
"unnecessaryTypeIgnore": "Nicht erforderlicher \"# type: ignore\"-Kommentar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@
"unionUnpackedTypeVarTuple": "La Union no puede incluir un TypeVarTuple desempaquetado",
"unnecessaryCast": "Llamada \"cast\" innecesaria; el tipo ya es \"{type}\"",
"unnecessaryIsInstanceAlways": "Llamada isinstance innecesaria; \"{testType}\" es siempre una instancia de \"{classType}\"",
"unnecessaryIsInstanceNever": "Unnecessary isinstance call; \"{testType}\" is never an instance of \"{classType}\"",
"unnecessaryIsInstanceNever": "Llamada isinstance innecesaria; \"{testType}\" es nunca una instancia de \"{classType}\"",
"unnecessaryIsSubclassAlways": "Llamada de issubclass innecesaria; \"{testType}\" siempre es una subclase de \"{classType}\"",
"unnecessaryIsSubclassNever": "Unnecessary issubclass call; \"{testType}\" is never a subclass of \"{classType}\"",
"unnecessaryIsSubclassNever": "Llamada de issubclass innecesaria; \"{testType}\" nunca es una subclase de \"{classType}\"",
"unnecessaryPyrightIgnore": "Comentario \"# pyright: ignore\" innecesario",
"unnecessaryPyrightIgnoreRule": "Regla innecesaria \"# pyright: ignore\": \"{name}\"",
"unnecessaryTypeIgnore": "Comentario \"# type: ignore\" innecesario",
Expand Down
Loading

0 comments on commit e46efc6

Please sign in to comment.