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

auto-imports: `Self` type is imported from `typing` module, not `typing_extensions` in Python 3.9

Open sarisia opened this issue 2 years ago • 5 comments

Auto-import suggests importing Self type (PEP 673) from typing module and not from typing_extensions module in Python 3.9.

typing.Self is available from Python 3.11 so Pylance should suggest typing_extensions.Self.

Looked into typeshed and seems they are typed correctly:

https://github.com/python/typeshed/blob/4379a6a509396a58b85ac9f888c3eec5aac38039/stdlib/typing.pyi#L107-L122

https://github.com/python/typeshed/blob/4379a6a509396a58b85ac9f888c3eec5aac38039/stdlib/typing_extensions.pyi#L45

Environment data

  • Language Server version: Pylance v2022.12.20
  • OS and version: devcontainer
    • Host: VSCode 1.74.2 Windows 11 10.0.22621
    • devcontainer.json:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
	"name": "Python 3",
	// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
	"image": "mcr.microsoft.com/devcontainers/python:0-3.9",

	// Features to add to the dev container. More info: https://containers.dev/features.
	// "features": {},

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],

	// Use 'postCreateCommand' to run commands after the container is created.
	// "postCreateCommand": "pip3 install --user -r requirements.txt",

	// Configure tool-specific properties.
	"customizations": {
		"vscode": {
			"settings": {
				"python.analysis.logLevel": "Trace"
			}
		}
	}

	// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
	// "remoteUser": "root"
}
  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.16

Code Snippet

Self

Repro Steps

  1. install typing_extensions: pip install typing-extensions

  2. In Python file, type Self and it suggests importing from wrong module

    image

  3. Accepting suggest results in wrong import

    image

Expected behavior

Auto-imports suggests importing typing_extensions.Self.

Actual behavior

It suggests importing typing.Self.

Logs

collapse...
[Info  - 3:38:12 PM] (909) Pylance language server 2022.12.20 (pyright 621d886b) starting
[Info  - 3:38:12 PM] (909) Server root directory: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist
[Info  - 3:38:12 PM] (909) Starting service instance "repro"
[Info  - 3:38:12 PM] (909) Notebook support: LSP
[Info  - 3:38:12 PM] (909) Interactive window support: LSP
[Info  - 3:38:12 PM] (909) No configuration file found.
[Info  - 3:38:12 PM] (909) No pyproject.toml file found.
[Info  - 3:38:12 PM] (909) Setting pythonPath for service "repro": "/usr/local/bin/python"
[Warn  - 3:38:12 PM] (909) stubPath /workspaces/repro/typings is not a valid directory.
[Info  - 3:38:12 PM] (909) Assuming Python version 3.9
[Info  - 3:38:12 PM] (909) Assuming Python platform Linux
[Info  - 3:38:12 PM] (909) Search paths for /workspaces/repro
[Info  - 3:38:12 PM] (909)   /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib
[Info  - 3:38:12 PM] (909)   /workspaces/repro
[Info  - 3:38:12 PM] (909)   /workspaces/repro/typings
[Info  - 3:38:12 PM] (909)   /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stubs/...
[Info  - 3:38:12 PM] (909)   /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/bundled/stubs
[Info  - 3:38:12 PM] (909)   /usr/local/lib/python3.9
[Info  - 3:38:12 PM] (909)   /usr/local/lib/python3.9/lib-dynload
[Info  - 3:38:12 PM] (909)   /home/vscode/.local/lib/python3.9/site-packages
[Info  - 3:38:12 PM] (909)   /usr/local/lib/python3.9/site-packages
[Info  - 3:38:12 PM] (909) Adding fs watcher for library directories:
 /usr/local/lib/python3.9
/usr/local/lib/python3.9/lib-dynload
/home/vscode/.local/lib/python3.9/site-packages
/usr/local/lib/python3.9/site-packages
[Info  - 3:38:12 PM] (909) Adding fs watcher for directories:
 /workspaces/repro
[Info  - 3:38:12 PM] (909) Searching for source files
[Info  - 3:38:12 PM] (909) Found 1 source file
[Info  - 3:38:12 PM] (909) Background analysis(1) root directory: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist
[Info  - 3:38:12 PM] (909) Background analysis(1) started
(909) Background analysis message: setConfigOptions
(909) Background analysis message: setImportResolver
(909) Background analysis message: ensurePartialStubPackages
(909) Background analysis message: setTrackedFiles
(909) Background analysis message: markAllFilesDirty
(909) Background analysis message: analyze
(909) [BG(1)] analyzing: /workspaces/repro/main.py ...
(909) [BG(1)]   parsing: /workspaces/repro/main.py [fs read 0ms] (11ms)
(909) [BG(1)]   parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 2ms] (88ms)
(909) [BG(1)]   binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/builtins.pyi (37ms)
(909) [BG(1)]   binding: /workspaces/repro/main.py (0ms)
(909) [BG(1)]   checking: /workspaces/repro/main.py (3ms)
(909) [BG(1)] analyzing: /workspaces/repro/main.py (141ms)
(909) [IDX(FG)] index libraries /workspaces/repro (index) ...
(909) [IDX(FG)]   read stdlib indices (53ms)
(909) [IDX(FG)] index libraries /workspaces/repro (index) [succeed] (56ms)
(909) Background analysis message: setFileOpened
(909) [FG] parsing: /workspaces/repro/main.py (9ms)
(909) [FG] parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 2ms] (92ms)
(909) [FG] binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/builtins.pyi (35ms)
(909) [FG] binding: /workspaces/repro/main.py (0ms)
(909) Background analysis message: getSemanticTokens full
(909) [BG(1)] getSemanticTokens full at /workspaces/repro/main.py (1ms)
(909) Background inlayHint message
(909) Background analysis message: analyze
(909) Background analysis message: getSemanticTokens range
(909) [BG(1)] getSemanticTokens range 0:0 - 0:0 at /workspaces/repro/main.py (0ms)
(909) Background analysis message: setFileClosed
(909) Background analysis message: analyze
(909) Background analysis message: setFileOpened
(909) Background analysis message: getSemanticTokens full
(909) [BG(1)] getSemanticTokens full at /workspaces/repro/main.py (0ms)
(909) Background inlayHint message
(909) Background analysis message: analyze
(909) Background analysis message: setFileOpened
(909) Background analysis message: markFilesDirty
(909) [FG] completion at /workspaces/repro/main.py:0:1 ...
(909) [FG]   parsing: /workspaces/repro/main.py (1ms)
(909) [FG]   binding: /workspaces/repro/main.py (1ms)
(909) [FG] completion at /workspaces/repro/main.py:0:1 [found 1255 items] (50ms)
(909) Background analysis message: setFileOpened
(909) Background analysis message: markFilesDirty
(909) Background analysis message: setFileOpened
(909) Background analysis message: markFilesDirty
(909) [FG] completion at /workspaces/repro/main.py:0:3 ...
(909) [FG]   parsing: /workspaces/repro/main.py (0ms)
(909) [FG]   binding: /workspaces/repro/main.py (0ms)
(909) [FG] completion at /workspaces/repro/main.py:0:3 [found 258 items] (21ms)
(909) Background analysis message: setFileOpened
(909) Background analysis message: markFilesDirty
(909) Background analysis message: getSemanticTokens delta
(909) [BG(1)] getSemanticTokens delta previousResultId:1671896556903 at /workspaces/repro/main.py ...
(909) [BG(1)]   parsing: /workspaces/repro/main.py (1ms)
(909) [BG(1)]   binding: /workspaces/repro/main.py (1ms)
(909) [FG] completion at /workspaces/repro/main.py:0:4 ...
(909) [FG]   parsing: /workspaces/repro/main.py (0ms)
(909) [FG]   binding: /workspaces/repro/main.py (0ms)
(909) [FG] completion at /workspaces/repro/main.py:0:4 [found 15 items] (18ms)
(909) [BG(1)]   parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing.pyi [fs read 3ms] (73ms)
(909) [BG(1)]   binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing.pyi (14ms)
(909) [BG(1)]   parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 2ms] (9ms)
(909) [BG(1)]   binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing_extensions.pyi (5ms)
(909) [BG(1)]   parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (12ms)
(909) [BG(1)]   binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (3ms)
(909) [BG(1)]   parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (1ms)
(909) [BG(1)]   binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/abc.pyi (1ms)
(909) [BG(1)] getSemanticTokens delta previousResultId:1671896556903 at /workspaces/repro/main.py (150ms)
(909) Background analysis message: analyze
(909) [BG(1)] analyzing: /workspaces/repro/main.py ...
(909) [BG(1)]   checking: /workspaces/repro/main.py (1ms)
(909) [BG(1)] analyzing: /workspaces/repro/main.py (1ms)
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: resumeAnalysis
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background inlayHint message
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: setFileOpened
(909) Background analysis message: markFilesDirty
(909) [FG] parsing: /workspaces/repro/main.py (0ms)
(909) [FG] binding: /workspaces/repro/main.py (0ms)
(909) Background analysis message: analyze
(909) [BG(1)] analyzing: /workspaces/repro/main.py ...
(909) [BG(1)]   parsing: /workspaces/repro/main.py (0ms)
(909) [BG(1)]   binding: /workspaces/repro/main.py (0ms)
(909) [BG(1)]   checking: /workspaces/repro/main.py ...
(909) [BG(1)]     parsing: /usr/local/lib/python3.9/typing.py [fs read 0ms] (99ms)
(909) [BG(1)]     binding: /usr/local/lib/python3.9/typing.py (18ms)
(909) [BG(1)]   checking: /workspaces/repro/main.py (136ms)
(909) [BG(1)] analyzing: /workspaces/repro/main.py (136ms)
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getSemanticTokens delta
(909) [BG(1)] getSemanticTokens delta previousResultId:1671897017043 at /workspaces/repro/main.py (0ms)
(909) Background analysis message: resumeAnalysis
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background inlayHint message
(909) [FG] parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing.pyi [fs read 3ms] (55ms)
(909) [FG] binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing.pyi (13ms)
(909) [FG] parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 2ms] (7ms)
(909) [FG] binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/typing_extensions.pyi (2ms)
(909) [FG] parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (11ms)
(909) [FG] binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (2ms)
(909) [FG] parsing: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (2ms)
(909) [FG] binding: /home/vscode/.vscode-server/extensions/ms-python.vscode-pylance-2022.12.20/dist/typeshed-fallback/stdlib/abc.pyi (0ms)
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange
(909) Background analysis message: getDiagnosticsForRange

sarisia avatar Dec 24 '22 15:12 sarisia

as a workaround try adding 'typing_extensions.py' to Extra Paths. image

bschnurr avatar Dec 29 '22 21:12 bschnurr

https://github.com/microsoft/pylance-release/issues/3785#issuecomment-1367592268 as a workaround try adding 'typing_extensions.py' to Extra Paths. image

Sadly this workaround seems not working:

"python.analysis.extraPaths": [
	".venv/lib/python3.9/site-packages/typing_extensions.py"
]

image

Pylance language server 2023.1.20 (pyright b78a9e4b)

sarisia avatar Jan 16 '23 08:01 sarisia

an that is the correct environment you have selected?

bschnurr avatar Mar 13 '23 18:03 bschnurr

Yes, confirmed this is the correct environment. We use Poetry to create in-project .venv (virtualenvs.in-project).

image

Pylance language server 2023.3.20 (pyright 93a4ef87)

sarisia avatar Mar 14 '23 01:03 sarisia

I have the same issue with lots of the backports in typing_extensions e.g. Self, reveal_type, ParamSpec etc.

The odd thing is Pylance correctly gives no warning when I import them from typing_extensions manually and correctly does warn when they are attempted to be imported from typing on an older version of Python. Nonetheless the auto-complete still only ever suggests importing from typing and not typing_extensions.

I also tried the suggested work-around using "Extra Paths" but it didn't seem to work for me either.

j-hil-nwm avatar Feb 07 '24 16:02 j-hil-nwm

See https://github.com/microsoft/pylance-release/issues/5196#issuecomment-1960365597 for the potential issues that need addressing in order to implement the fix.

I also opened a pyright issue to ask about the first issue in the linked comment. https://github.com/microsoft/pyright/issues/7318

StellaHuang95 avatar Feb 22 '24 22:02 StellaHuang95

could be dup of https://github.com/microsoft/pylance-release/issues/4993

heejaechang avatar Jul 08 '24 18:07 heejaechang

closing duplicate

bschnurr avatar Aug 06 '24 23:08 bschnurr