Skip to content

Commit bc72b3c

Browse files
authored
Fix isEmpty check for partially resolved namespace packages (#6749)
* Fix * Unit test
1 parent 4457f36 commit bc72b3c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/pyright-internal/src/analyzer/importResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ export class ImportResolver {
15271527
// skip the typings import and continue searching.
15281528
if (
15291529
typingsImport.isNamespacePackage &&
1530-
!typingsImport.resolvedUris[typingsImport.resolvedUris.length - 1]
1530+
typingsImport.resolvedUris[typingsImport.resolvedUris.length - 1].isEmpty()
15311531
) {
15321532
if (this._isNamespacePackageResolved(moduleDescriptor, typingsImport.implicitImports)) {
15331533
return typingsImport;

packages/pyright-internal/src/tests/importResolver.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,38 @@ describe('Import tests that can run with or without a true venv', () => {
361361
);
362362
});
363363

364+
test('py.typed namespace package plus stubs', () => {
365+
const typingFolder = combinePaths(normalizeSlashes('/'), 'typing');
366+
const files = [
367+
{
368+
path: combinePaths(typingFolder, 'myLib/core', 'foo.pyi'),
369+
content: 'def test(): pass',
370+
},
371+
{
372+
path: combinePaths(libraryRoot, 'myLib', 'py.typed'),
373+
content: '',
374+
},
375+
{
376+
path: combinePaths(libraryRoot, 'myLib', '__init__.py'),
377+
content: 'def test(): pass',
378+
},
379+
{
380+
path: combinePaths(libraryRoot, 'myLib', '__init__.pyi'),
381+
content: 'def test(): pass',
382+
},
383+
];
384+
385+
const importResult = getImportResult(files, ['myLib'], (c) => (c.stubPath = Uri.file(typingFolder)));
386+
assert(importResult.isImportFound);
387+
assert(importResult.isStubFile);
388+
assert.strictEqual(
389+
1,
390+
importResult.resolvedUris.filter(
391+
(f) => !f.isEmpty() && f.getFilePath() === combinePaths(libraryRoot, 'myLib', '__init__.pyi')
392+
).length
393+
);
394+
});
395+
364396
test('stub in typing folder over partial stub package', () => {
365397
const typingFolder = combinePaths(normalizeSlashes('/'), 'typing');
366398
const files = [

0 commit comments

Comments
 (0)