Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rely on pyright import resolution for module names #139

Draft
wants to merge 6 commits into
base: scip
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/pyright-internal/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,13 @@ export class ImportResolver {
filePath: string,
stripTopContainerDir = false
): ModuleNameInfoFromPath | undefined {
/**
* TODO(https://github.com/sourcegraph/scip-python/issues/91)
* Both paths are casted to lowercase to make the comparison case-insensitive. This has been fixed upstream and should be removed on merge.
*/
containerPath = containerPath.toLowerCase();
filePath = filePath.toLowerCase();

containerPath = ensureTrailingDirectorySeparator(containerPath);
let filePathWithoutExtension = stripFileExtension(filePath);

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def baz_function():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def mod_function():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def toc_function():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .bar.baz import baz_function
from bar.mod import mod_function
from src.bar.toc import toc_function

baz_function()
mod_function()
toc_function()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# < definition scip-python python snapshot-util 0.1 /__init__:
#documentation (module)


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# < definition scip-python python snapshot-util 0.1 `src.bar.baz`/__init__:
#documentation (module) src.bar.baz

def baz_function():
# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.baz`/baz_function().
# documentation ```python
# > def baz_function(): # -> None:
# > ```
pass

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# < definition scip-python python snapshot-util 0.1 `src.bar.mod`/__init__:
#documentation (module) src.bar.mod

def mod_function():
# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.mod`/mod_function().
# documentation ```python
# > def mod_function(): # -> None:
# > ```
pass

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# < definition scip-python python snapshot-util 0.1 `src.bar.toc`/__init__:
#documentation (module) src.bar.toc

def toc_function():
# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.toc`/toc_function().
# documentation ```python
# > def toc_function(): # -> None:
# > ```
pass

20 changes: 20 additions & 0 deletions packages/pyright-scip/snapshots/output/nested_import/src/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# < definition scip-python python snapshot-util 0.1 `src.foo`/__init__:
#documentation (module) src.foo

from .bar.baz import baz_function
# ^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/__init__:
# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/baz_function().
from bar.mod import mod_function
# ^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/__init__:
# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/mod_function().
from src.bar.toc import toc_function
# ^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/__init__:
# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/toc_function().

baz_function()
#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/baz_function().
mod_function()
#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/mod_function().
toc_function()
#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/toc_function().

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#documentation (module) src.importer

from foo.bar import InitClass
# ^^^^^^^ reference snapshot-util 0.1 `foo.bar`/__init__:
# ^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/__init__:
# ^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/InitClass#
from foo.bar.baz.mod import SuchNestedMuchWow, AnotherNestedMuchWow
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `foo.bar.baz.mod`/__init__:
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/__init__:
# ^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/SuchNestedMuchWow#
# ^^^^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/AnotherNestedMuchWow#

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#documentation (module) src.long_importer

import foo.bar.baz.mod
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `foo.bar.baz.mod`/__init__:
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/__init__:

print(foo.bar.baz.mod.SuchNestedMuchWow)
#^^^^ reference python-stdlib 3.11 builtins/print().
Expand Down
37 changes: 27 additions & 10 deletions packages/pyright-scip/src/treeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class TreeVisitor extends ParseTreeWalker {
[]
);

this.cwd = path.resolve(process.cwd());
this.cwd = path.resolve(process.cwd()).toLowerCase();

this._docstringWriter = new TypeStubExtendedWriter(this.config.sourceFile, this.evaluator);
}
Expand Down Expand Up @@ -498,13 +498,19 @@ export class TreeVisitor extends ParseTreeWalker {
// ^^^^^^^ node.module (create new reference)
// ^ node.alias (create new local definition)
override visitImportAs(node: ImportAsNode): boolean {
const moduleName = _formatModuleName(node.module);
let moduleName = _formatModuleName(node.module);
const importInfo = getImportInfo(node.module);
if (
importInfo &&
importInfo.resolvedPaths[0] &&
path.resolve(importInfo.resolvedPaths[0]).startsWith(this.cwd)
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] &&
path
.resolve(importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1])
.toLowerCase()
.startsWith(this.cwd)
) {
moduleName = this.program._getImportNameForFile(
importInfo!.resolvedPaths[importInfo.resolvedPaths.length - 1]
);
const symbol = Symbols.makeModuleInit(this.projectPackage, moduleName);
this.pushNewOccurrence(node.module, symbol);
} else {
Expand Down Expand Up @@ -1063,10 +1069,21 @@ export class TreeVisitor extends ParseTreeWalker {
pythonPackage = this.stdlibPackage;
}

return Symbols.makeModuleInit(
pythonPackage,
node.nameParts.map((namePart) => namePart.value).join('.')
);
const importInfo = getImportInfo(node);

let moduleName = node.nameParts.map((namePart) => namePart.value).join('.');

if (
importInfo &&
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] &&
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1].toLowerCase().startsWith(this.cwd)
) {
moduleName = this.program._getImportNameForFile(
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1]
);
}

return Symbols.makeModuleInit(pythonPackage, moduleName);
}
case ParseNodeType.MemberAccess: {
softAssert(false, 'Not supposed to get a member access');
Expand Down Expand Up @@ -1396,7 +1413,7 @@ export class TreeVisitor extends ParseTreeWalker {
const nodeFilePath = path.resolve(nodeFileInfo.filePath);

// TODO: Should use files from the package to determine this -- should be able to do that quite easily.
if (nodeFilePath.startsWith(this.cwd)) {
if (nodeFilePath.toLowerCase().startsWith(this.cwd.toLowerCase())) {
return this.projectPackage;
}

Expand Down Expand Up @@ -1616,7 +1633,7 @@ export class TreeVisitor extends ParseTreeWalker {
if (declPath && declPath.length !== 0) {
const p = path.resolve(declPath);

if (p.startsWith(this.cwd)) {
if (p.toLowerCase().startsWith(this.cwd)) {
return this.projectPackage;
}
}
Expand Down