Description
Please give your opinion on this. I opened it as a feature request as it is something I would very well like to see in pycord. Below are my thoughts and ideas, which I am pretty sure have some issues themselves. I would be happy to contribute on working on this. I think it may be a good step towards v3 or whatever it is called.
Summary
Migrate internal models from TypedDict
to Pydantic to improve type safety, runtime validation, and developer experience.
What is the feature request for?
The core library
The Problem
Currently, the internal models/types primarily use TypedDict
for type annotations. While TypedDict
provides static typing benefits, it has several limitations:
- No runtime validation capabilities - impossible to validate actual payloads against their type definitions
- No runtime type checking -
isinstance()
checks aren't possible on typed dicts as they are just dicts at runtime. - Type definitions can easily become outdated as they aren't enforced at runtime
- No automatic type casting or data transformation (custom validators and serializers)
- Limited developer experience (e.g., no runtime field validation)
The Ideal Solution
Gradually migrate our internal model definitions to Pydantic, starting with API requests/responses and gateway models. This would provide:
- Improved type safety throughout the library
- Validation of API/Gateway requests/responses
- Betterer developer experience with proper IDE support
- More maintainable codebase
- Clearer separation between API models and internal representations
- It's rust :p
Here is a rough idea of how I would do it and some ideas, to be discussed if this fr is moved forward in the first place:
-
Create a new
models
submodule with the following structure:models/base/
: Core model definitions matching Discord's API objectsmodels/api/
: Request/response models for APImodels/gateway/
: Gateway-related models
-
Add an optional
Model
parameter toHTTPClient.request
and other places where applicable to automagically cast to a pydantic model:def request[T: BaseModel](self, ..., model: type[T] | None = None) -> T:
This allows slow and gradual adoption, you can start by just implementing the new ones then migrate the old ones.
-
Migrate existing
TypedDict
definitions to Pydantic models, add new features to support this change where applicable. Enforce all new model implementations to use Pydantic models.
Additional Context
https://discord.com/channels/881207955029110855/881224361015672863/1302431199670833223
https://discord.com/channels/881207955029110855/881735314987708456/1299306561432326165