You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm exploring capturing in a Protocol some of the readable interface of a Mapping where values have different types for keys (like a lightweight Mapping version of TypedDict. I expected the snippet below to work but it doesn't:
I expected the assignment above to type check properly, given that the dictionary object used satisfies the constraints given in the MyMap protocol.
Actual Behavior
map.py:11: error: Incompatible types in assignment (expression has type "Dict[Literal['a'], int]", variable has type "MyMap")
map.py:11: note: Following member(s) of "Dict[Literal['a'], int]" have conflicts:
map.py:11: note: Expected:
map.py:11: note: @overload
map.py:11: note: def __getitem__(self, Literal['a']) -> int
map.py:11: note: @overload
map.py:11: note: def __getitem__(self, Literal['b']) -> str
map.py:11: note: Got:
map.py:11: note: def __getitem__(self, Literal['a']) -> int
map.py:11: error: Dict entry 1 has incompatible type "Literal['b']": "str"; expected "Literal['a']": "int"
Found 2 errors in 1 file (checked 1 source file)
Even if my expected behaviour is incorrect, there's something wrong with this error message, where it says that expression has type "Dict[Literal['a'], int]" (which clearly isn't the type of the expression)
Your Environment
Mypy version used: 0.982 (compiled: yes)
Mypy command-line flags: just the filename
Python version used: 3.10.8
The text was updated successfully, but these errors were encountered:
I'm aware of that thread (I wrote this code after looking at it trying to explore alternative implementations); still I think the issue goes beyond the original problem
The signature of dict.__getitem__ doesn't match the protocol in your code sample, so mypy correct reports a type compatibility error. I think mypy is doing the right thing here. Recommend closing.
I'm exploring capturing in a Protocol some of the readable interface of a Mapping where values have different types for keys (like a lightweight
Mapping
version ofTypedDict
. I expected the snippet below to work but it doesn't:To Reproduce
Expected Behavior
I expected the assignment above to type check properly, given that the dictionary object used satisfies the constraints given in the
MyMap
protocol.Actual Behavior
Even if my expected behaviour is incorrect, there's something wrong with this error message, where it says that
expression has type "Dict[Literal['a'], int]"
(which clearly isn't the type of the expression)Your Environment
The text was updated successfully, but these errors were encountered: