DEV Community

loading...

Type Hinting for Dictionaries in Python

Serhat Teker
Uomo Universale | Software Engineer | Entrepreneur | builds systems | py:js:go |
Originally published at tech.serhatteker.com on ・1 min read

We can use generic version of dict: Dict, to declare the dictionary type in
python:

from typing import Dict


def get_request(self) -> Dict[str, str]:
    ...


def validate(request: Dict[str, str]) -> bool:
    ...
Enter fullscreen mode Exit fullscreen mode

However this is deprecated since version 3.9. We can use directly dict:



def get_request(self) -> dict[str, str]:
    ...


def validate(request: dict[str, str]) -> bool:
    ...

Enter fullscreen mode Exit fullscreen mode

For more info: Dict

All done!

Discussion (0)