[note] Enhancing Code Clarity with TypedDict

The reason why we meed to use TypedDict

In general, we use dataclasses and Pydantic to store data. However, using dictionaries to store complex data is often an easier approach. Nevertheless, without predefined fields, it becomes difficult for later developers to maintain the code effectively. Using TypedDict allows the IDE to provide developers with suggestions, enhancing their development efficiency. But, TypedDict have some limitations; it cannot enforce strict type checking.

1
2
3
4
5
6
7
8
from typing import TypedDict
import datetime


class News(TypedDict):
title: str
create_date: datetime.datetime
content: str

image

Reference

[note] Enhancing Code Clarity with TypedDict

https://hsiangjenli.github.io/blog/note_typedict/

Author

Hsiang-Jen Li

Posted on

2024-09-07

Updated on

2024-12-18

Licensed under