Skip to content

collection.ChainMap rejects non-MutableMapping types #2097

Closed
@yehorb

Description

@yehorb

Pylance does not allow ChainMap maps to have the Mapping type. The MutableMapping type if required. I want to use ChainMap for lookup only, but I am forced to cast Mapping to dict (which has measurable performance consequences) just to satisfy type checks. It should be possible to use the Mapping type in ChainMap, there are no docs or requirements that say that it is incorrect.

I do understand that making ChainMap from Mapping elements and then assigning to it can lead to Runtime errors. But reporting an error if you try to do a natural thing - make a ChainMap from a bunch of Mapping elements - is not great either. I believe there are more people than use ChainMap for lookups than for assignments.

from collections import ChainMap
from typing import Any, Mapping


def build_mapping(this: Mapping[str, Any], that: Mapping[str, Any]) -> Mapping[str, Any]:
    return ChainMap(this, that)

Environment data

  • Language Server version: Pylance language server 2021.11.2 (pyright b27108d5) starting.
  • OS and version: Windows 10 latest version.
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.8, in conda environment.

Expected behaviour

No error.

Actual behaviour

Argument of type "Mapping[str, Any]" cannot be assigned to parameter "maps" of type "MutableMapping[_KT@ChainMap, _VT@ChainMap]" in function "__init__"
  "Mapping[str, Any]" is incompatible with "MutableMapping[_KT@ChainMap, _VT@ChainMap]"PylancereportGeneralTypeIssues
(parameter) this: Mapping[str, Any]

Logs

[Info  - 15:04:59] Pylance language server 2021.11.2 (pyright b27108d5) starting
[Info  - 15:04:59] Server root directory: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist
[Info  - 15:04:59] No configuration file found.
[Info  - 15:04:59] No pyproject.toml file found.
[Info  - 15:04:59] Setting pythonPath for service "mutable-mapping": "~\miniconda3\envs\mutable-mapping\python.exe"
[Warn  - 15:04:59] stubPath ~\projects\mutable-mapping\typings is not a valid directory.
[Info  - 15:04:59] Assuming Python version 3.8
[Info  - 15:04:59] Assuming Python platform Windows
Search paths for ~\projects\mutable-mapping
  ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib
  ~\projects\mutable-mapping
  ~\projects\mutable-mapping\typings
  ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stubs\...
  ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\bundled\stubs
  ~\miniconda3\envs\mutable-mapping\DLLs
  ~\miniconda3\envs\mutable-mapping\Lib
  ~\miniconda3\envs\mutable-mapping
  ~\miniconda3\envs\mutable-mapping\Lib\site-packages
  ~\projects\mutable-mapping\ccpa_deletion\python\psycopg2-utils
  ~\miniconda3\envs\mutable-mapping\Lib\site-packages\win32
  ~\miniconda3\envs\mutable-mapping\Lib\site-packages\win32\lib
  ~\miniconda3\envs\mutable-mapping\Lib\site-packages\pythonwin
[Info  - 15:05:00] Searching for source files
[Info  - 15:05:00] Found 1 source file
[FG] parsing: ~\projects\mutable-mapping\main.py (31ms)
[FG] parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 4ms] (138ms)
[FG] binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\builtins.pyi (79ms)
[FG] binding: ~\projects\mutable-mapping\main.py (1ms)
[Info  - 15:05:00] Background analysis(1) root directory: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist
[Info  - 15:05:00] 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: analyze
IntelliCode model ~\.vscode\extensions\visualstudioexptteam.vscodeintellicode-1.2.14\cache\E61945A9A512ED5E1A3EE3F1A2365B88F8FE_E4E9EADA96734F01970E616FAB2FAC19
[BG(1)] analyzing: ~\projects\mutable-mapping\main.py ...
[BG(1)]   parsing: ~\projects\mutable-mapping\main.py (43ms)
[BG(1)]   parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 3ms] (89ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\builtins.pyi (45ms)
[BG(1)]   binding: ~\projects\mutable-mapping\main.py (0ms)
[BG(1)]   checking: ~\projects\mutable-mapping\main.py (4ms)
[BG(1)] analyzing: ~\projects\mutable-mapping\main.py (184ms)
Background analysis message: getSemanticTokens full
[BG(1)] getSemanticTokens full at ~\projects\mutable-mapping\main.py ...
[BG(1)]   parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\typing.pyi [fs read 2ms] (59ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\typing.pyi (8ms)
[BG(1)]   parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 1ms] (10ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (1ms)
[BG(1)]   parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 0ms] (3ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\typing_extensions.pyi (2ms)
[BG(1)]   parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\abc.pyi [fs read 1ms] (3ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\abc.pyi (0ms)
[BG(1)]   parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\collections\__init__.pyi [fs read 1ms] (12ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\collections\__init__.pyi ...
[BG(1)]     parsing: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\_collections_abc.pyi [fs read 1ms] (1ms)
[BG(1)]     binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\_collections_abc.pyi (1ms)
[BG(1)]   binding: ~\.vscode\extensions\ms-python.vscode-pylance-2021.11.2\dist\typeshed-fallback\stdlib\collections\__init__.pyi (6ms)
[BG(1)] getSemanticTokens full at ~\projects\mutable-mapping\main.py (132ms)
Background analysis message: getSemanticTokens range
[BG(1)] getSemanticTokens range 0:0 - 6:0 at ~\projects\mutable-mapping\main.py (2ms)
Background analysis message: markFilesDirty
Background analysis message: analyze
[BG(1)] analyzing: ~\projects\mutable-mapping\main.py ...
[BG(1)]   checking: ~\projects\mutable-mapping\main.py (9ms)
[BG(1)] analyzing: ~\projects\mutable-mapping\main.py (9ms)
Background analysis message: resumeAnalysis
Loading ONNX runtime...
Loaded ONNX runtime. Creating IntelliCode session...
Created IntelliCode session.
Initialize deeplearning succeeded

Code Snippet / Additional information

from collections import ChainMap
from typing import Any, Mapping


def build_mapping(this: Mapping[str, Any], that: Mapping[str, Any]) -> Mapping[str, Any]:
    return ChainMap(this, that)

Metadata

Metadata

Assignees

No one assigned

    Labels

    typestubIssue relating to our bundled type stubswaiting for upstreamWaiting for upstream to release a fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions