Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto import not working #6350

Closed
olegkorshunov opened this issue Sep 4, 2024 · 9 comments
Closed

auto import not working #6350

olegkorshunov opened this issue Sep 4, 2024 · 9 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@olegkorshunov
Copy link

Environment data

  • Pylance version: v2024.8.1
  • OS and version: WSL
  • Python version (& distribution if applicable, e.g. Anaconda): poetry 3.11.8

image
image

settings.json

{
    "terminal.integrated.defaultProfile.windows": "Git Bash",
    "terminal.integrated.persistentSessionReviveProcess": "never",
    "terminal.integrated.enablePersistentSessions": false,
    "files.eol": "\n",
    "[python]": {
        "diffEditor.ignoreTrimWhitespace": false,
        "editor.formatOnType": true,
        "editor.wordBasedSuggestions": "off",
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        }
    },
    "python.analysis.packageIndexDepths": [
        {
            "name": "sqlalchemy",
            "depth": 3,
            "includeAllSymbols": true,
        },
    ],
    "python.analysis.indexing": true,
    "python.analysis.autoImportCompletions": true,
    "python.analysis.autoSearchPaths": true,
    "python.analysis.importFormat": "absolute",
    "python.analysis.inlayHints.variableTypes": false,
    "python.analysis.inlayHints.functionReturnTypes": false,
    "python.testing.autoTestDiscoverOnSaveEnabled": true,
    "workbench.colorTheme": "Default High Contrast",
    "search.exclude": {
        ".idea": true,
        // ".venv": true,
        "poetry.lock": true
    },
    "editor.rulers": [
        120
    ],
    // formatter
    "pylint.args": [
        "--max-line-length=120",
        "--disable=C0114, C0115, C0116, W0105, C0200, E1101, E1121, C0103, W0621, W1203",
    ],
    "mypy-type-checker.args": [
        "--allow_redefinition"
    ],
    "black-formatter.importStrategy": "fromEnvironment",
    "black-formatter.args": [
        "--line-length=120"
    ],
    "notebook.formatOnCellExecution": true,
    // "flake8.importStrategy": "fromEnvironment",
    // "flake8.args": [
    //     "--config-toml=pyproject.toml"
    // ],
    "python.terminal.activateEnvInCurrentTerminal": true,
    "remote.autoForwardPortsSource": "hybrid",
    "workbench.editor.editorActionsLocation": "titleBar",
    "isort.importStrategy": "fromEnvironment",
    "isort.args": [
        "--profile",
        "black"
    ],
    "editor.defaultFormatter": "ms-python.black-formatter",
    "update.mode": "none",
    "extensions.autoUpdate": false,
    "editor.minimap.enabled": false,
    "explorer.compactFolders": false,
    "python.analysis.diagnosticMode": "workspace",
    "python.analysis.inlayHints.pytestParameters": true,
    "notebook.output.textLineLimit": 100,
    "files.autoSave": "onFocusChange",
    "debugpy.debugJustMyCode": false,
    "editor.formatOnSave": true,
    "notebook.formatOnSave.enabled": true,
    "[markdown]": {
        "diffEditor.ignoreTrimWhitespace": true
    },
    "jupyter.askForKernelRestart": false,
    "jupyter.debugJustMyCode": false,
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "editor.stickyScroll.enabled": false,
    "files.insertFinalNewline": true,
}
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Sep 4, 2024
@olegkorshunov olegkorshunov changed the title not work auto-import auto import not working Sep 4, 2024
@heejaechang
Copy link
Contributor

what symbol are you looking for?

@rchiodo
Copy link
Contributor

rchiodo commented Sep 5, 2024

The variable in the other module - uuid_pk. It's not working because of this code here:

https://github.com/microsoft/pyright/blob/49235d38ba73200c014b1ae5b2617f1d67321352/packages/pyright-internal/src/languageService/autoImporter.ts#L467

Which looks like we only include 'CONSTANTS' as imports for variables.

@heejaechang
Copy link
Contributor

heejaechang commented Sep 5, 2024

this is by design then. auto import only suggests variables in __all__ unless it is from stub file, otherwise, you will get zillions of variables such as a, foo and etc.

@rchiodo
Copy link
Contributor

rchiodo commented Sep 5, 2024

But why do we not include other variables? I couldn't find anything that indicates these are considered 'private'. Private is only for variables with _ or __.

@heejaechang
Copy link
Contributor

heejaechang commented Sep 5, 2024

we already tried to include locals and back off since it includes too much stuff. python doesn't have accessibility mechanism and nobody uses _ for variables to indicates it is private at the top level. see any of our repro steps user puts in the issue. none of them are supposed to be public but none of them uses _.

our escape patch is __all__ for regular python file. if the variable is intended to be used in other module, it should be explicitly added to __all__ otherwise, we won't expose it to add/auto import

it is different for stub file. we assume anything in the stub file is supposed to be public. unlike regular python file, there won't be any temporary variable defined at module level.

@heejaechang
Copy link
Contributor

one thing we have we didn't have before is new search import code action and add import ordering with MRU, so, one thing we could do is exposing variables in search code action and add import with lowest priority?

I believe auto import should be left as it is.

@heejaechang
Copy link
Contributor

@olegkorshunov that said, where is this uuid_pk defined? it would help for us to understand the usage case?

@olegkorshunov
Copy link
Author

olegkorshunov commented Sep 5, 2024

@olegkorshunov that said, where is this uuid_pk defined? it would help for us to understand the usage case?

here my project structure
app/db/custom_types.py: uuid_pk
I trying it to auto import in wallet.py

- app
    - __init__.py
    - db
       - __init__.py
       - custom_types.py.py
       - wallet.py
       ...
     ...
...

@olegkorshunov
Copy link
Author

olegkorshunov commented Sep 5, 2024

The variable in the other module - uuid_pk. It's not working because of this code here:

https://github.com/microsoft/pyright/blob/49235d38ba73200c014b1ae5b2617f1d67321352/packages/pyright-internal/src/languageService/autoImporter.ts#L467

Which looks like we only include 'CONSTANTS' as imports for variables.

yes thanks for the answer, if I do it in uppercase then auto import works. Slightly different behavior for me, I'm used to it always working in Pycharm, but in VScode used other alogrithm for auto import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

4 participants