pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Pylance should auto-import from files marked with `_` too

Open zmievsa opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

In my library, Cadwyn, I use _ prefixes for files to make sure that their interfaces are private to my users. However, as a result, I do not get auto-imports for them too -- even when I have Cadwyn open in a directory.

To reproduce:

  1. Open vscode in any dir
  2. Create two files: file1.py and _file2.py
  3. Create a variable MY_VAR = 83 in _file2.py
  4. Write MY_VAR in file1.py and try to get auto-import

Describe the solution you’d like Private modules should also be available for autocompletion when they are a part of your project. Otherwise it doesn't make sense: if marking a file as private removes my ability to import from it in my own project, then this file is only usable as a script. Is that the convention in python community? I don't think so. FastAPI does _compat.py file too and imports from it everywhere within the project.

Interestingly, it works as expected with private packages! So it's only an issue in private modules.

zmievsa avatar Dec 20 '23 11:12 zmievsa

@rchiodo, my impression from reading the following issues is that this should work. Is this a bug, or does the user need to do something differently to get the expected behavior? Or am I missing something?

  • https://github.com/microsoft/pylance-release/issues/4510
  • https://github.com/microsoft/pylance-release/discussions/4518
  • https://github.com/microsoft/pylance-release/issues/4528

debonte avatar Jan 30 '24 05:01 debonte

Sounds like a bug, but we'd need to repro it I guess. AFAIK, this should be working. It should allow importing private modules unless there's a py.typed file.

rchiodo avatar Jan 30 '24 17:01 rchiodo

Want to add that I am getting the same problems here, also for functions in a private python file.

MartinBernstorff avatar Jun 12 '24 11:06 MartinBernstorff

Sounds like a bug, but we'd need to repro it I guess. AFAIK, this should be working. It should allow importing private modules unless there's a py.typed file.

@rchiodo hmm... Thing is: I am developing a library so there is a py.typed file. But I still want to be able to import private modules, at least while I'm working on the library itself. Here's the library: https://github.com/zmievsa/cadwyn

(sorry for the long absense)

zmievsa avatar Jun 12 '24 13:06 zmievsa

Thanks I can reproduce the problem. I'll look into it:

image

rchiodo avatar Jun 12 '24 18:06 rchiodo

Turns out we explicitly remove any file starting with _ from the list of auto imports. I believe we can workaround this and check if a file is part of the workspace or not.

rchiodo avatar Jun 19 '24 23:06 rchiodo

That'd be fantastic! Currently not indicating privacy of my modules, would love to remedy that 👍

MartinBernstorff avatar Jun 20 '24 07:06 MartinBernstorff

This issue has been fixed in prerelease version 2024.6.101, which we've just released. You can find the changelog here: CHANGELOG.md

rchiodo avatar Jun 20 '24 21:06 rchiodo