w: dict[int, int] = {1:2, 2:3, 69: 420}
produces the error message in the title. Why won't specific type hints work for my dictionaries and lists?
Asked
Active
Viewed 1,941 times
1

Karl Knechtel
- 62,466
- 11
- 102
- 153

cjm
- 451
- 4
- 20
-
@jonrsharpe Yes. Why didn't DDG put that as the top result when I searched for the error? – cjm Feb 06 '21 at 00:05
1 Answers
1
As of python3.8, you need to import their respective classes from the typing
module.
from typing import Dict, List, Set
w: Dict[int, int] = {1:2, 2:3, 69:420}
Note the capital D in Dict
.

cjm
- 451
- 4
- 20
-
This is well known and very possibly covered in other questions. There's no need for a self-answered question. – Pranav Hosangadi Feb 05 '21 at 23:58
-
For example, the topic is comprehensively covered here: https://stackoverflow.com/questions/37087457/difference-between-defining-typing-dict-and-dict – Pranav Hosangadi Feb 05 '21 at 23:59
-
The question you linked did not appear in the Google results for the error nor in the suggested duplicates box when typing the question. – cjm Feb 06 '21 at 00:02