[GH-ISSUE #3758] [feature]: use python'sTypedDict in codegen #1308

Open
opened 2026-03-16 19:46:32 +03:00 by kerem · 0 comments
Owner

Originally created by @KraXen72 on GitHub (Jan 17, 2024).
Original GitHub issue: https://github.com/hoppscotch/hoppscotch/issues/3758

Is there an existing issue for this?

  • I have searched the existing issues

Summary

Python now has a better way to define type definitions than the one that's currently used - see https://peps.python.org/pep-0589/

Why should this be worked on?

In my opinion it's better to utilize python's typing system, rather than generate whole classes.

Some TypedDict code:

from typing import TypedDict

class Movie(TypedDict):
    name: str
    year: int

movie: Movie = {'name': 'Blade Runner',
                'year': 1982}

This will be type-checked in VSCode with these settings:

"python.languageServer": "Pylance",
"python.analysis.typeCheckingMode": "basic", // "strict" works too

Sometimes, api's have keys for json that cannot be defined this way, due to them not being valid identifiers (like when the key contains a hyphen). In that case, according to the spec, the function syntax must be used:

MBArtist = TypedDict("MBArtist", { 
	"id": str,
	"name": str, 
	"sort-name": str, 
})

MBArtistCredit = TypedDict("MBArtistCredit", { 
	"name": str, 
	"sort-name": str, 
	"artist": MBArtist
})

MBRecording = TypedDict("MBRecording", {
	"id": str,
	"title": str,
	"artist-credit": list[MBArtistCredit],
	"releases": list[dict[str, str]]
})

TypedDict is supported from python version 3.8, which is pretty widely available now.

Originally created by @KraXen72 on GitHub (Jan 17, 2024). Original GitHub issue: https://github.com/hoppscotch/hoppscotch/issues/3758 ### Is there an existing issue for this? - [X] I have searched the existing issues ### Summary Python now has a better way to define type definitions than the one that's currently used - see https://peps.python.org/pep-0589/ ### Why should this be worked on? In my opinion it's better to utilize python's typing system, rather than generate whole classes. Some TypedDict code: ```py from typing import TypedDict class Movie(TypedDict): name: str year: int movie: Movie = {'name': 'Blade Runner', 'year': 1982} ``` This will be type-checked in VSCode with these settings: ```jsonc "python.languageServer": "Pylance", "python.analysis.typeCheckingMode": "basic", // "strict" works too ``` Sometimes, api's have keys for json that cannot be defined this way, due to them not being valid identifiers (like when the key contains a hyphen). In that case, according to the spec, the function syntax must be used: ```py MBArtist = TypedDict("MBArtist", { "id": str, "name": str, "sort-name": str, }) MBArtistCredit = TypedDict("MBArtistCredit", { "name": str, "sort-name": str, "artist": MBArtist }) MBRecording = TypedDict("MBRecording", { "id": str, "title": str, "artist-credit": list[MBArtistCredit], "releases": list[dict[str, str]] }) ``` TypedDict is supported from python version 3.8, which is pretty widely available now.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#1308
No description provided.