Skip to content

Commit

Permalink
Skip unnecessary py.typed file exist checks. (microsoft#7652)
Browse files Browse the repository at this point in the history
* skip unnecessary py.typed file exist checks.

* passed wrong uri

* PR feedbacks

* more comments

* PR feedbacks
  • Loading branch information
heejaechang committed Apr 9, 2024
1 parent ed92822 commit bd08098
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 4 additions & 3 deletions packages/pyright-internal/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { Host } from '../common/host';
import { stubsSuffix } from '../common/pathConsts';
import { stripFileExtension } from '../common/pathUtils';
import { PythonVersion, pythonVersion3_0 } from '../common/pythonVersion';
import { ServiceProvider } from '../common/serviceProvider';
import { ServiceKeys } from '../common/serviceKeys';
import { ServiceProvider } from '../common/serviceProvider';
import * as StringUtils from '../common/stringUtils';
import { equateStringsCaseInsensitive } from '../common/stringUtils';
import { Uri } from '../common/uri/uri';
Expand All @@ -26,7 +26,7 @@ import { isIdentifierChar, isIdentifierStartChar } from '../parser/characters';
import { ImplicitImport, ImportResult, ImportType } from './importResult';
import { getDirectoryLeadingDotsPointsTo } from './importStatementUtils';
import { ImportPath, ParentDirectoryCache } from './parentDirectoryCache';
import { PyTypedInfo, getPyTypedInfo } from './pyTypedUtils';
import { PyTypedInfo, getPyTypedInfoForPyTypedFile } from './pyTypedUtils';
import * as PythonPathUtils from './pythonPathUtils';
import * as SymbolNameUtils from './symbolNameUtils';
import { isDunderName } from './symbolNameUtils';
Expand Down Expand Up @@ -2708,7 +2708,8 @@ export class ImportResolver {
if (!this.fileExistsCached(filePath.pytypedUri)) {
return undefined;
}
return getPyTypedInfo(this.fileSystem, filePath);

return getPyTypedInfoForPyTypedFile(this.fileSystem, filePath.pytypedUri);
}

private _resolveNativeModuleStub(
Expand Down
18 changes: 16 additions & 2 deletions packages/pyright-internal/src/analyzer/pyTypedUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,32 @@ export interface PyTypedInfo {
isPartiallyTyped: boolean;
}

//
// Retrieves information about a py.typed file, if it exists, under the given path.
//
export function getPyTypedInfo(fileSystem: FileSystem, dirPath: Uri): PyTypedInfo | undefined {
if (!fileSystem.existsSync(dirPath) || !isDirectory(fileSystem, dirPath)) {
return undefined;
}

let isPartiallyTyped = false;
const pyTypedPath = dirPath.pytypedUri;

if (!fileSystem.existsSync(pyTypedPath) || !isFile(fileSystem, pyTypedPath)) {
return undefined;
}

return getPyTypedInfoForPyTypedFile(fileSystem, pyTypedPath);
}

//
// Retrieves information about a py.typed file. The pyTypedPath provided must be a valid path.
//
export function getPyTypedInfoForPyTypedFile(fileSystem: FileSystem, pyTypedPath: Uri) {
// This function intentionally doesn't check whether the given py.typed path exists or not,
// as filesystem access is expensive if done repeatedly.
// The caller should verify the file's validity before calling this method and use a cache if possible
// to avoid high filesystem access costs.
let isPartiallyTyped = false;

// Read the contents of the file as text.
const fileStats = fileSystem.statSync(pyTypedPath);

Expand Down

0 comments on commit bd08098

Please sign in to comment.