Skip to content

Commit 1fc0cb2

Browse files
authored
Merge pull request #3 from codemuse-app/fix-133
Uses resolved imports for module names
2 parents ffc6f3e + 0ad4dd1 commit 1fc0cb2

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
@@ -1,10 +1,10 @@
11
# < definition scip-python python snapshot-util 0.1 `src.importer`/__init__:
22

33
from foo.bar import InitClass
4-
# ^^^^^^^ reference snapshot-util 0.1 `foo.bar`/__init__:
4+
# ^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/__init__:
55
# ^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/InitClass#
66
from foo.bar.baz.mod import SuchNestedMuchWow, AnotherNestedMuchWow
7-
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `foo.bar.baz.mod`/__init__:
7+
# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/__init__:
88
# ^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/SuchNestedMuchWow#
99
# ^^^^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/AnotherNestedMuchWow#
1010

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
@@ -1,7 +1,7 @@
11
# < definition scip-python python snapshot-util 0.1 `src.long_importer`/__init__:
22

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

66
print(foo.bar.baz.mod.SuchNestedMuchWow)
77
#^^^^ 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 {
@@ -1076,10 +1082,21 @@ export class TreeVisitor extends ParseTreeWalker {
10761082
pythonPackage = this.stdlibPackage;
10771083
}
10781084

1079-
return Symbols.makeModuleInit(
1080-
pythonPackage,
1081-
node.nameParts.map((namePart) => namePart.value).join('.')
1082-
);
1085+
const importInfo = getImportInfo(node);
1086+
1087+
let moduleName = node.nameParts.map((namePart) => namePart.value).join('.');
1088+
1089+
if (
1090+
importInfo &&
1091+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] &&
1092+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1].toLowerCase().startsWith(this.cwd)
1093+
) {
1094+
moduleName = this.program._getImportNameForFile(
1095+
importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1]
1096+
);
1097+
}
1098+
1099+
return Symbols.makeModuleInit(pythonPackage, moduleName);
10831100
}
10841101
case ParseNodeType.MemberAccess: {
10851102
softAssert(false, 'Not supposed to get a member access');
@@ -1416,7 +1433,7 @@ export class TreeVisitor extends ParseTreeWalker {
14161433
const nodeFilePath = path.resolve(nodeFileInfo.filePath);
14171434

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

@@ -1636,7 +1653,7 @@ export class TreeVisitor extends ParseTreeWalker {
16361653
if (declPath && declPath.length !== 0) {
16371654
const p = path.resolve(declPath);
16381655

1639-
if (p.startsWith(this.cwd)) {
1656+
if (p.toLowerCase().startsWith(this.cwd)) {
16401657
return this.projectPackage;
16411658
}
16421659
}

0 commit comments

Comments
 (0)