DEV Community

Serhat Teker
Serhat Teker

Posted on Originally published at tech.serhatteker.com on

Type Hinting for Dictionaries in Python

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:
    ...
🔴 Hot News: serhatteker/type hinting for dictionaries in python ek - ประเด็นร้อน 🔴 Hot News: serhatteker/type hinting for dictionaries in python ek - ประเด็นร้อน

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:
    ...

🔴 Hot News: serhatteker/type hinting for dictionaries in python ek - ประเด็นร้อน 🔴 Hot News: serhatteker/type hinting for dictionaries in python ek - ประเด็นร้อน

For more info: Dict

All done!

Top comments (0)