basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

import suggestions don't work for explicit re-exports

Open jorenham opened this issue 1 year ago • 7 comments

image_2024-10-14_05-32-23

This screenshot is from scipy-stubs, specifically in scipy-stubs/sparse/csgraph/_validation.pyi (the relevant code isn't pushed yet, but will be soon).

So here the basedpyright suggested to import sparray from scipy.sparse._base, which I accepted because it was the only available option. But in the screenshot you can see that it wasn't able to find spmatrix, even though it also is available in (at least) scipy.sparse._base.

Within scipy.sparse._base.__all__, only sparray is explicitly exported, and spmatrix is implicitly exported through from {} import spmatrix as spmatrix (blame scipy).

But the strange thing is that both spmatrix and sparray are both explicitly exported at the package level, i.e. through scipy.sparse.__init__.__all__. However, in both cases, the basedpyright plugin wasn't able to suggest this 🤔.

jorenham avatar Oct 14 '24 03:10 jorenham

the relevant code: https://github.com/jorenham/scipy-stubs/blob/d24baa3be2a311fdf85ca1be13eb5ed9420202ba/scipy-stubs/sparse/csgraph/_validation.pyi

jorenham avatar Oct 14 '24 03:10 jorenham

looks like the only difference between sparray and spmatrix is that spmatrix is an explicit re-export (from ._matrix import spmatrix as spmatrix), maybe there's an issue with import suggestions with re-exports?

does the same thing happen with completions?

DetachHead avatar Oct 14 '24 04:10 DetachHead

maybe there's an issue with import suggestions with re-exports

Yea, I suppose that makes sense. Especially because sparray is actually defined in scipy.sparse._base, whereas spmatrix isn't.

does the same thing happen with completions?

Yea I believe so, but it's hard to tell because I'm also using codeium as an AI autocomplete assistant thingy

jorenham avatar Oct 14 '24 04:10 jorenham

looks like a setting for this was added in the latest pylance release: https://code.visualstudio.com/updates/v1_95#_improved-import-suggestions

One of Pylance's powerful features is its ability to provide auto-import suggestions. By default, Pylance offers the import suggestion from where the symbol is defined, but you might want it to import it from a file where the symbol is imported (i.e. aliased). With the new python.analysis.includeAliasesFromUserFiles setting, you can now control whether Pylance includes alias symbols from user files in its auto-import suggestions or in the add import Quick Fix.

Note that enabling this setting can negatively impact performance, especially in large codebases, as Pylance may need to index more symbols and monitor more files for changes, which can increase resource usage.

DetachHead avatar Oct 30 '24 01:10 DetachHead

I believe that's more applicable to situations where you did import numbers as ir in some file that's closed, so that if you type Rational in some other file in a different continent, then pylance will autoimport import numbers as ir, and then autocompletes Rational as ir.Rational

jorenham avatar Oct 30 '24 01:10 jorenham

But I didn't test it out yet (I was too busy shouting at my screen because the typeshed still didn't annotate the numbers stdlib), so don't take my word for it 🤷🏻

jorenham avatar Oct 30 '24 01:10 jorenham

includeAliasesFromUserFiles also works for plain re-exports, at least when an alias is used, which marks it as public. So it would help with:

from ._matrix import spmatrix as spmatrix

akvadrako avatar Apr 23 '25 09:04 akvadrako

includeAliasesFromUserFiles

I’m not seeing this option under basedpyright.analysis.. how do i enable it?

the-vampiire avatar Aug 18 '25 20:08 the-vampiire

He's talking about pylance, that option doesn't exist yet in basedpyright

DetachHead avatar Aug 18 '25 22:08 DetachHead