basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

Language server does't offer autocomplete for functions in modules or packages that haven't been opened or loaded

Open dekomote opened this issue 1 year ago • 22 comments

This happens in vscode. The diagnostic mode is set to "workspace"

"basedpyright.analysis.diagnosticMode": "workspace"

I have an example project with 2 packages and 2 modules inside.

image

In pack1/mod1.py I have a function

def some_function(arg: str):
    ...

If I open mod2.py, write "some_" and try to get autocomplete one of two things happen:

  1. If mod1 has been opened, or a module that references mod1 or any file in pack1 has been opened, I will get an autocomplete.
image
  1. If only mod2.py has been opened since opening the editor, I don't get anything.
image

If I now open pack1/mod1.py, go back to mod2.py and try the same thing, I get an autocomplete.

I have the same setup for sublime and there it works as it should.

image

Same thing happens with pyright. Hoever, with pylance, the problem doesn't exist.

dekomote avatar Aug 05 '24 16:08 dekomote

thanks for reporting. i've noticed similar behaviour but haven't been able to narrow down anything specific. interesting that it only happens in vscode though

DetachHead avatar Aug 05 '24 23:08 DetachHead

i reproduced the same issue in sublime text, so i don't think it's specific to vscode

DetachHead avatar Aug 10 '24 00:08 DetachHead

Have you enabled workspace instead of open files only?

dekomote avatar Aug 10 '24 08:08 dekomote

oh wow, i thought diagnosticMode was "workspace" by default. i should change that (#559)

now that i've set mine to "workspace", i can't seem to reproduce the issue in vscode or sublime text:

image

how exactly are you configuring diagnosticMode in vscode? is it in your workspace or user settings? maybe it's being overwritten or something? also which version of basedpyright are you using?

DetachHead avatar Aug 11 '24 00:08 DetachHead

I use basedpyright.analysis.diagnosticMode, set to workspace.

I use v1.15.2

I tried it again, it doesn't work in vscode. Make sure you haven't opened any files from pack1 prior to opening mod2, even if they are closed now. Once they are read, it works.

dekomote avatar Aug 11 '24 17:08 dekomote

weird, i can reproduce the issue on a different machine. will investigate further, thanks

notes to self:

  • prob need to revert #560 before the next release, its way too slow on a large project
  • perhaps the default can be set back to `"workspace" once #427 is done?
  • is this an issue in pylance?

DetachHead avatar Aug 12 '24 01:08 DetachHead

is this an issue in pylance?

No. In pylance, it works, even if you set the diagnostic mode to open files only. I think this is what separates pylance from pyright

dekomote avatar Aug 12 '24 05:08 dekomote

ok i think i can reliably reproduce the issue now

No. In pylance, it works, even if you set the diagnostic mode to open files only. I think this is what separates pylance from pyright

it doesn't seem to work for me in pylance at all. instead i get a quick fix to search for additional imports which i think is a recent change. it does end up showing it but i don't know why they'd add an extra step and make it more annoying:

image

image

issues with import suggestions are always so hard to debug, because the behavior seems to always change randomly.

DetachHead avatar Aug 12 '24 12:08 DetachHead

You probably have "python.languageServer": "None" set in .vscode/settings.json automatically, so pylance doesn't work. I can reliably get autocomplete with pylance.

dekomote avatar Aug 12 '24 12:08 dekomote

nah i have a separate isolated pylance profile with python.languageServer set to "Default"

DetachHead avatar Aug 12 '24 12:08 DetachHead

The auto-complete via code actions behavior is quite unpredictable for me. It seems to work for most of the stdlib modules like os, itetools , subprocess but not for a few ones like logging and unittest(there could be more but I just happened to notice these two).

And for third party modules, it didn't work for me at all, I did try setting extraPaths to site-packages path, and changing the diagnosticMode to workspace but none of these options worked.

Although this is only via code actions, if I start typing, the entries do show up in the auto-completion popup menu.

For reference I am using Neovim with native lsp.

rbhanot4739 avatar Sep 21 '24 09:09 rbhanot4739

Confirm the same issue on VSCode and vim for me.

Example file: test.py

class Test(BaseModel):
    id: str
    test: str

t = Test(
    id="Hey",
    test="yo",
)

Startup: https://pastebin.com/RVYHKwDE Code Action Issue: https://pastebin.com/RcWuYcTf

npip99 avatar Sep 22 '24 23:09 npip99

Actually I've also ran into this issue recently and didn't figure out what's wrong with it. Sometimes a file would properly get saved into autocomplete but an adjacent file in that folder would not. Maybe it's because of this issue...

Wizzerinus avatar Jan 02 '25 11:01 Wizzerinus

same issue

zmm2tysu avatar Jun 26 '25 08:06 zmm2tysu

same in vim, zed, vs code

Runderoy avatar Aug 21 '25 17:08 Runderoy

Same with neovim. Strange thing, because Sqlalchemy it supports, but not FastAPI, Django, DRF, Flask.

alexander-chemeza avatar Sep 11 '25 09:09 alexander-chemeza

I've been poking and prodding the code for the past two days. Initially, all the files are analyzed and the symbols are correctly mapped and processed, but somehow, the symbol table gets reset after that.

I'll investigate more.

dekomote avatar Sep 12 '25 15:09 dekomote

Did you ever make any progress on figuring this one out? I'm using the Cursor fork of basedpyright and getting very similar behavior, so I'd love to see this get fixed.

coredumperror avatar Oct 02 '25 20:10 coredumperror

Wasted a ton of time on this issue, and found nothing. All the files in the workspace are getting processed when you open the project correctly, just not offered for autocomplete for some reason I can't understand.

dekomote avatar Oct 02 '25 20:10 dekomote

Makes me think @DetachHead (or someone similarly deeply familiar with how basedpyright works) is going to have to dig into this, then.

coredumperror avatar Oct 02 '25 20:10 coredumperror

No, I just updated my configuration to the latest version of lspconfig. I figured out that it is looking for some dynamic libraries like fastapi if you insert some its imports in workspace. For example if I write in workspace from FastAPI import FastAPI and from fastapi.security import ... it is looking in these modules.> Did you ever make any progress on figuring this one out? I'm using the Cursor fork of basedpyright and getting very similar behavior, so I'd love to see this get fixed.

alexander-chemeza avatar Oct 03 '25 04:10 alexander-chemeza

I'm having the same issue. Gosh this is a little troublesome.

vhbui02 avatar Oct 29 '25 17:10 vhbui02