microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 770 forks source link

False positive reportShadowedImports with PEP562 package-level getattr #4304

Open bdarnell opened 1 year ago

bdarnell commented 1 year ago

Environment data

Code Snippet

The issue occurs for some but not all stdlib imports within the github.com/tornadoweb/tornado repo. The cause appears to be this __getattr__ hook in tornado/init.py

https://github.com/tornadoweb/tornado/blob/dc56f548fb7edfc6697b100cbbfc422fd9784913/tornado/__init__.py#L63-L67

One import that gives this error is the "import sys" in tornado/test/runtests.py.

Repro Steps

Clone github.com/tornadoweb/tornado, open it in vscode (with python plugins installed with default settings). Open tornado/test/runtests.py and observe the warning reported on the "import sys" line:

"/Users/bdarnell/src/tornado/tornado/__init__.py" is overriding the stdlib module "sys" Pylance reportShadowedImports

Expected behavior

No warning should be emitted.

Actual behavior

A warning is emitted as quoted above.

This is a false positive for multiple reasons:

  1. The getattr hook raises ImportError for unknown names, so it should not actually shadow anything. (but fair enough, you can't determine that statically)
  2. Absolute imports have been the default since Python 3.0. The getattr hook in tornado/init.py is only used for imports that specify tornado.* or relative imports within the tornado package, not this unqualified "import sys"
  3. The warning is occurring in subpackages of tornado, and even when absolute imports were not the default I do not believe there was any name resolution rule that would put this in scope

Logs

(11786) [FG] parsing: /Users/bdarnell/src/tornado/tornado/test/runtests.py (16ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (26ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/builtins.pyi (10ms)
(11786) [FG] binding: /Users/bdarnell/src/tornado/tornado/test/runtests.py (2ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (8ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/typing.pyi (3ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (1ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/typing_extensions.pyi (1ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (4ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/types.pyi [fs read 0ms] (2ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/types.pyi (2ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (0ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/abc.pyi (0ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/textwrap.pyi [fs read 1ms] (1ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/textwrap.pyi (0ms)
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/unittest/__init__.pyi [fs read 1ms] (3ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/unittest/__init__.pyi ...
(11786) [FG]   parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/unittest/async_case.pyi [fs read 0ms] (1ms)
(11786) [FG]   binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/unittest/async_case.pyi (0ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/unittest/__init__.pyi (1ms)
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/sys.pyi [fs read 1ms] (6ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/sys.pyi (2ms)
(11786) [FG] parsing: /Users/bdarnell/src/tornado/tornado/__init__.py (2ms)
(11786) [FG] binding: /Users/bdarnell/src/tornado/tornado/__init__.py (0ms)
(11786) [FG] parsing: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/logging/__init__.pyi [fs read 0ms] (3ms)
(11786) [FG] binding: /Users/bdarnell/.vscode/extensions/ms-python.vscode-pylance-2023.4.40/dist/typeshed-fallback/stdlib/logging/__init__.pyi (1ms)
(11786) [FG] parsing: /var/folders/vg/ff5x6_0x0ds6xnct4sm8q_mh0000gn/T/pyright-11786-sYza27XIScSe/builtins-11786-9Oxiz20yaPGe-.py [fs read 0ms] (17ms)
(11786) [FG] binding: /var/folders/vg/ff5x6_0x0ds6xnct4sm8q_mh0000gn/T/pyright-11786-sYza27XIScSe/builtins-11786-9Oxiz20yaPGe-.py (19ms)
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: setFileClosed
(11786) Background analysis message: analyze
(11786) Background analysis message: setFileOpened
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getDiagnosticsForRange
(11786) Background analysis message: getSemanticTokens full
(11786) [BG(1)] getSemanticTokens full at /Users/bdarnell/src/tornado/tornado/test/runtests.py (7ms)
bdarnell commented 2 months ago

For the record, this is still occurring with language server version 2027.7.1 (Pylance language server 2024.7.1 (pyright version 1.1.369, commit 1168be71) starting). However, it's moved around a bit. I no longer see it on the import sys line in tornado/test/runtests.py. I still see it in other places, including the import of binascii in httpclient_test.py.

I'm not sure whether this is a useful clue or not, but binascii is imported four times in this codebase (auth.py, web.py, httpclient_test.py, and web_test.py), and three of those times are affected by this bug (the one in web.py is not).

heejaechang commented 1 month ago

let me take a look

heejaechang commented 1 month ago

I think I found the issue. thank you for the reporting!