Skip to content

Commit 0ad4dd1

Browse files
committed
Uses resolved imports for module names
1 parent e45b593 commit 0ad4dd1

File tree

13 files changed

+97
-13
lines changed

13 files changed

+97
-13
lines changed

packages/pyright-scip/snapshots/input/nested_import/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def baz_function():
2+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def mod_function():
2+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def toc_function():
2+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .bar.baz import baz_function
2+
from bar.mod import mod_function
3+
from src.bar.toc import toc_function
4+
5+
baz_function()
6+
mod_function()
7+
toc_function()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# < definition scip-python python snapshot-util 0.1 /__init__:
2+
#documentation (module)
3+
4+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# < definition scip-python python snapshot-util 0.1 `src.bar.baz`/__init__:
2+
#documentation (module) src.bar.baz
3+
4+
def baz_function():
5+
# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.baz`/baz_function().
6+
# documentation ```python
7+
# > def baz_function(): # -> None:
8+
# > ```
9+
pass
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# < definition scip-python python snapshot-util 0.1 `src.bar.mod`/__init__:
2+
#documentation (module) src.bar.mod
3+
4+
def mod_function():
5+
# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.mod`/mod_function().
6+
# documentation ```python
7+
# > def mod_function(): # -> None:
8+
# > ```
9+
pass
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# < definition scip-python python snapshot-util 0.1 `src.bar.toc`/__init__:
2+
#documentation (module) src.bar.toc
3+
4+
def toc_function():
5+
# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.toc`/toc_function().
6+
# documentation ```python
7+
# > def toc_function(): # -> None:
8+
# > ```
9+
pass
10+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# < definition scip-python python snapshot-util 0.1 `src.foo`/__init__:
2+
#documentation (module) src.foo
3+
4+
from .bar.baz import baz_function
5+
# ^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/__init__:
6+
# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/baz_function().
7+
from bar.mod import mod_function
8+
# ^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/__init__:
9+
# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/mod_function().
10+
from src.bar.toc import toc_function
11+
# ^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/__init__:
12+
# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/toc_function().
13+
14+
baz_function()
15+
#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/baz_function().
16+
mod_function()
17+
#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/mod_function().
18+
toc_function()
19+
#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/toc_function().
20+

packages/pyright-scip/snapshots/output/nested_items/src/importer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#documentation (module) src.importer
33

44
from foo.bar import InitClass
5-
# ^^^^^^^ reference snapshot-util 0.1 `foo.bar`/__init__:
5+
# ^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/__init__:
66
# ^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/InitClass#
77
from foo.bar.baz.mod import SuchNestedMuchWow, AnotherNestedMuchWow
8-
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `foo.bar.baz.mod`/__init__:
8+
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/__init__:
99
# ^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/SuchNestedMuchWow#
1010
# ^^^^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/AnotherNestedMuchWow#
1111

packages/pyright-scip/snapshots/output/nested_items/src/long_importer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#documentation (module) src.long_importer
33

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

77
print(foo.bar.baz.mod.SuchNestedMuchWow)
88
#^^^^ reference python-stdlib 3.11 builtins/print().

packages/pyright-scip/src/treeVisitor.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export class TreeVisitor extends ParseTreeWalker {
202202
[]
203203
);
204204

205-
this.cwd = path.resolve(process.cwd());
205+
this.cwd = path.resolve(process.cwd()).toLowerCase();
206206

207207
this._docstringWriter = new TypeStubExtendedWriter(this.config.sourceFile, this.evaluator);
208208
}
@@ -498,13 +498,19 @@ export class TreeVisitor extends ParseTreeWalker {
498498
// ^^^^^^^ node.module (create new reference)
499499
// ^ node.alias (create new local definition)
500500
override visitImportAs(node: ImportAsNode): boolean {
501-
const moduleName = _formatModuleName(node.module);
501+
let moduleName = _formatModuleName(node.module);
502502
const importInfo = getImportInfo(node.module);
503503
if (
504504
importInfo &&
505-
importInfo.resolvedPaths[0] &&
506-
path.resolve(importInfo.resolvedPaths[0]).startsWith(this.cwd)
505+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] &&
506+
path
507+
.resolve(importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1])
508+
.toLowerCase()
509+
.startsWith(this.cwd)
507510
) {
511+
moduleName = this.program._getImportNameForFile(
512+
importInfo!.resolvedPaths[importInfo.resolvedPaths.length - 1]
513+
);
508514
const symbol = Symbols.makeModuleInit(this.projectPackage, moduleName);
509515
this.pushNewOccurrence(node.module, symbol);
510516
} else {
@@ -1063,10 +1069,21 @@ export class TreeVisitor extends ParseTreeWalker {
10631069
pythonPackage = this.stdlibPackage;
10641070
}
10651071

1066-
return Symbols.makeModuleInit(
1067-
pythonPackage,
1068-
node.nameParts.map((namePart) => namePart.value).join('.')
1069-
);
1072+
const importInfo = getImportInfo(node);
1073+
1074+
let moduleName = node.nameParts.map((namePart) => namePart.value).join('.');
1075+
1076+
if (
1077+
importInfo &&
1078+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] &&
1079+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1].toLowerCase().startsWith(this.cwd)
1080+
) {
1081+
moduleName = this.program._getImportNameForFile(
1082+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1]
1083+
);
1084+
}
1085+
1086+
return Symbols.makeModuleInit(pythonPackage, moduleName);
10701087
}
10711088
case ParseNodeType.MemberAccess: {
10721089
softAssert(false, 'Not supposed to get a member access');
@@ -1396,7 +1413,7 @@ export class TreeVisitor extends ParseTreeWalker {
13961413
const nodeFilePath = path.resolve(nodeFileInfo.filePath);
13971414

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

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

1619-
if (p.startsWith(this.cwd)) {
1636+
if (p.toLowerCase().startsWith(this.cwd)) {
16201637
return this.projectPackage;
16211638
}
16221639
}

0 commit comments

Comments
 (0)