Skip to content

Commit

Permalink
Fix isEmpty check for partially resolved namespace packages (#6749)
Browse files Browse the repository at this point in the history
* Fix

* Unit test
  • Loading branch information
debonte authored Dec 15, 2023
1 parent 4457f36 commit bc72b3c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ export class ImportResolver {
// skip the typings import and continue searching.
if (
typingsImport.isNamespacePackage &&
!typingsImport.resolvedUris[typingsImport.resolvedUris.length - 1]
typingsImport.resolvedUris[typingsImport.resolvedUris.length - 1].isEmpty()
) {
if (this._isNamespacePackageResolved(moduleDescriptor, typingsImport.implicitImports)) {
return typingsImport;
Expand Down
32 changes: 32 additions & 0 deletions packages/pyright-internal/src/tests/importResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,38 @@ describe('Import tests that can run with or without a true venv', () => {
);
});

test('py.typed namespace package plus stubs', () => {
const typingFolder = combinePaths(normalizeSlashes('/'), 'typing');
const files = [
{
path: combinePaths(typingFolder, 'myLib/core', 'foo.pyi'),
content: 'def test(): pass',
},
{
path: combinePaths(libraryRoot, 'myLib', 'py.typed'),
content: '',
},
{
path: combinePaths(libraryRoot, 'myLib', '__init__.py'),
content: 'def test(): pass',
},
{
path: combinePaths(libraryRoot, 'myLib', '__init__.pyi'),
content: 'def test(): pass',
},
];

const importResult = getImportResult(files, ['myLib'], (c) => (c.stubPath = Uri.file(typingFolder)));
assert(importResult.isImportFound);
assert(importResult.isStubFile);
assert.strictEqual(
1,
importResult.resolvedUris.filter(
(f) => !f.isEmpty() && f.getFilePath() === combinePaths(libraryRoot, 'myLib', '__init__.pyi')
).length
);
});

test('stub in typing folder over partial stub package', () => {
const typingFolder = combinePaths(normalizeSlashes('/'), 'typing');
const files = [
Expand Down

0 comments on commit bc72b3c

Please sign in to comment.