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

reportTypedDictNotRequiredAccess seems not works correctly. #1926

Closed
AiroPi opened this issue Oct 10, 2021 · 4 comments
Closed

reportTypedDictNotRequiredAccess seems not works correctly. #1926

AiroPi opened this issue Oct 10, 2021 · 4 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@AiroPi
Copy link

AiroPi commented Oct 10, 2021

Environment data

PyLance : v2021.10.1-pre.1

  • Language Server version: v2021.10.1-pre.1
  • OS and version: Windows 11 Professionnel Education version 21H2
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.10.0

Expected behaviour

PyLance should know that we defined the key when creating the object, and nowhere after declaration we deleted any key.
Also, even if we add a check if the key exist there is an avert.

Actual behaviour

PyLance trigger the reportTypedDictNotRequiredAccess error even if it should not.

Logs

[Info  - 16:50:45] Pylance language server 2021.10.1-pre.1 (pyright d5878534) starting
[Info  - 16:50:45] Server root directory: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist
[Info  - 16:50:45] No configuration file found.
[Info  - 16:50:45] No pyproject.toml file found.
[Info  - 16:50:45] Setting pythonPath for service "test": "C:\Users\pierr\AppData\Local\Programs\Python\Python310\python.exe"
[Warn  - 16:50:45] stubPath c:\Users\pierr\Documents\programmation\git\test\typings is not a valid directory.
[Info  - 16:50:45] Assuming Python version 3.10
[Info  - 16:50:45] Assuming Python platform Windows
Search paths for c:\Users\pierr\Documents\programmation\git\test
  c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib
  c:\Users\pierr\Documents\programmation\git\test
  c:\Users\pierr\Documents\programmation\git\test\typings
  c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\...
  c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\bundled\stubs
  c:\Users\pierr\AppData\Local\Programs\Python\Python310\DLLs
  c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib
  c:\Users\pierr\AppData\Local\Programs\Python\Python310
  c:\Users\pierr\AppData\Roaming\Python\Python310\site-packages
  c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages
[Info  - 16:50:46] Searching for source files
[Info  - 16:50:47] Found 1 source file
[FG] parsing: c:\Users\pierr\Documents\programmation\git\test\test.py (129ms)
[FG] parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 68ms] (580ms)
[FG] binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\builtins.pyi (163ms)
[FG] binding: c:\Users\pierr\Documents\programmation\git\test\test.py (1ms)
[Info  - 16:50:47] Background analysis(1) root directory: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist
[Info  - 16:50:47] Background analysis(1) started
Background analysis message: setConfigOptions
Background analysis message: setImportResolver
Background analysis message: ensurePartialStubPackages
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: setFileOpened
Background analysis message: getDiagnosticsForRange
Background analysis message: analyze
[BG(1)] analyzing: c:\Users\pierr\Documents\programmation\git\test\test.py ...
[BG(1)]   parsing: c:\Users\pierr\Documents\programmation\git\test\test.py (211ms)
[BG(1)]   parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 42ms] (532ms)
[Info  - 16:50:48] Indexer background runner(2) root directory: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist
[Info  - 16:50:48] Indexing(2) started
[BG(1)]   binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\builtins.pyi (134ms)
[BG(1)]   binding: c:\Users\pierr\Documents\programmation\git\test\test.py (1ms)
[BG(1)]   checking: c:\Users\pierr\Documents\programmation\git\test\test.py ...
[BG(1)]     parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\typing.pyi [fs read 30ms] (222ms)
[BG(1)]     binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\typing.pyi (65ms)
[BG(1)]     parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 44ms] (63ms)
[BG(1)]     binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (3ms)
[BG(1)]     parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\types.pyi [fs read 30ms] (98ms)
[BG(1)]     binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\types.pyi (38ms)
[BG(1)]     parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 41ms] (78ms)
[BG(1)]     binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi (7ms)
[BG(1)]     parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\abc.pyi [fs read 19ms] (21ms)
[BG(1)]     binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\abc.pyi (1ms)
[BG(1)]   checking: c:\Users\pierr\Documents\programmation\git\test\test.py (809ms)
[BG(1)] analyzing: c:\Users\pierr\Documents\programmation\git\test\test.py (1702ms)
Background analysis message: getSemanticTokens full
[BG(1)] getSemanticTokens full at c:\Users\pierr\Documents\programmation\git\test\test.py (86ms)
Background analysis message: getDiagnosticsForRange
Background analysis message: getSemanticTokens range
[BG(1)] getSemanticTokens range 0:0 - 17:0 at c:\Users\pierr\Documents\programmation\git\test\test.py (4ms)
Background analysis message: resumeAnalysis
[BG(1)] indexing: c:\Users\pierr\Documents\programmation\git\test\test.py [found 2] (1ms)
Indexing Done: c:\Users\pierr\Documents\programmation\git\test\test.py
[IDX(2)] index libraries c:\Users\pierr\Documents\programmation\git\test ...
[IDX(2)]   read stdlib indices (1082ms)
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
[IDX(2)]   scan packages [found 18 modules over 1 exec env] (2063ms)
[Info  - 16:50:52] [IDX(2)] Long operation: scan packages (2063ms)
[IDX(2)]   index execution environment c:\Users\pierr\Documents\programmation\git\test ...
[IDX(2)]     parsing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\xml\__init__.py [fs read 0ms] (65ms)
[IDX(2)]     parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 10ms] (224ms)
[IDX(2)]     binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\builtins.pyi (35ms)
[IDX(2)]     binding: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\xml\__init__.py (0ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\xml\__init__.py [found 0] (1ms)
[IDX(2)]     parsing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pip\__init__.py [fs read 3ms] (4ms)
[IDX(2)]     binding: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pip\__init__.py (4ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pip\__init__.py ...
[IDX(2)]       parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\typing.pyi [fs read 1ms] (69ms)
[IDX(2)]       binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\typing.pyi (6ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pip\__init__.py [found 4] (79ms)
[IDX(2)]     parsing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pkg_resources\__init__.py [fs read 1ms] (193ms)
[IDX(2)]     binding: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pkg_resources\__init__.py (8ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pkg_resources\__init__.py ...
[IDX(2)]       parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\pkgutil.pyi [fs read 27ms] (57ms)
[IDX(2)]       binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stdlib\pkgutil.pyi (2ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\pkg_resources\__init__.py [found 71] (63ms)
[IDX(2)]     parsing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\setuptools\__init__.py [fs read 0ms] (37ms)
[IDX(2)]     binding: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\setuptools\__init__.py (3ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\setuptools\__init__.py ...
[IDX(2)]       parsing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\setuptools\_deprecation_warning.py [fs read 4ms] (9ms)
[IDX(2)]       binding: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\setuptools\_deprecation_warning.py (0ms)
[IDX(2)]       parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\setuptools\setuptools\extension.pyi [fs read 25ms] (26ms)
[IDX(2)]       binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\setuptools\setuptools\extension.pyi (0ms)
[IDX(2)]       parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\setuptools\setuptools\dist.pyi [fs read 43ms] (48ms)
[IDX(2)]       binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\setuptools\setuptools\dist.pyi (1ms)
[IDX(2)]       parsing: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\setuptools\setuptools\depends.pyi [fs read 20ms] (24ms)
[IDX(2)]       binding: c:\Users\pierr\.vscode-insiders\extensions\ms-python.vscode-pylance-2021.10.1-pre.1\dist\typeshed-fallback\stubs\setuptools\setuptools\depends.pyi (0ms)
[IDX(2)]     indexing: c:\Users\pierr\AppData\Local\Programs\Python\Python310\Lib\site-packages\setuptools\__init__.py [found 8] (109ms)
[IDX(2)]   index execution environment c:\Users\pierr\Documents\programmation\git\test [found 8812 in 421 files] (1152ms)
[IDX(2)] index libraries c:\Users\pierr\Documents\programmation\git\test [found 8812 in 1 exec envs] (4300ms)
[Info  - 16:50:53] [IDX(2)] Long operation: index libraries c:\Users\pierr\Documents\programmation\git\test (4300ms)
[Info  - 16:50:53] Indexer done(2). indexed 421 files

Code Snippet / Additional information

from typing import TypedDict


class MyDict(TypedDict, total=False):
    key1: int
    key2: str


my_dict: MyDict = {
    'key1': 1,
    'key2': "test"
}

print(my_dict['key1'])  # Here there is an "error"

if 'key1' in my_dict.keys():
    print(my_dict['key1'])  # Here there is an "error"
@yvvt0379
Copy link

To see logs, you should:

  1. add this line "python.analysis.logLevel": "Trace" to your settings.json file.
  2. go to Output, click the drop-down box, and select Pylance Language Server.

@erictraut
Copy link
Contributor

The type MyDict in your example indicates that the keys are not required. In general, a type checker must assume that a not-required key is potentially not present and accessing it through an index operation will result in a runtime error.

Pyright (the type checker that underlies pylance) will "narrow" the type and (internally mark the corresponding key as no longer "not-required") based on conditional checks, like this:

if "key1" in my_dict:
    print(my_dict["key1"])

Note that you don't need to fetch the .keys() explicitly, and pyright's narrowing logic doesn't know about that pattern.

Pyright doesn't currently narrow the TypedDict type on assignment. I think that's a good enhancement request. I've created a tracking feature request in the pyright repo.

In the meantime, you can use the pattern above. Another solution is to enclose the usage in a try block. Or you can suppress the reportTypedDictNotRequiredAccess diagnostic temporarily within that file by adding the following comment somewhere:

# pyright: reportTypedDictNotRequiredAccess=false

Or you can disable that diagnostic rule throughout your entire project using pylance settings to set the diagnostic severity to "none".

@erictraut
Copy link
Contributor

I've added support for TypedDict narrowing on assignment. This support will be in the next release of pylance.

@erictraut erictraut added enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Oct 11, 2021
@bschnurr
Copy link
Member

This issue has been fixed in version 2021.10.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2021101-14-october-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants