Python list of Literal type check error (same for dict with Literal keys) in PyCharm

Viewed 1303

I'd like to use advantages of Literal type hints in Python, but it seems that Literal is not recognized correctly as dict keys or in lists:

from typing import Literal, Dict, List

T = Literal['foo', 'bar']
L = List[T]
D = Dict[T, int]

q: T = 'foo'  # ok
l: L = ['foo', 'bar']  # Expected type 'List[Literal['foo', 'bar']]', got 'List[str]' instead 
d: D = {'foo': 1}  # Expected type 'Dict[Literal['foo', 'bar'], int]', got 'Dict[str, int]' instead

How do use Literal type correctly in list or as dict keys?

0 Answers
Related