Convert dataclass to TypedDict for fun and type safety 🪄
Project description
Übertyped
Convert dataclass to TypedDict for fun and type safety 🪄
uber- /ˈuːbə/ to a great or extreme degree
Requirements
python>=3.11mypy>=1.0.1
Installation
pip install ubertyped
Mypy plugin
Add ubertyped.mypy_plugin to the list of plugins in your mypy config file
(for example pyproject.toml)
[tool.mypy]
python_version = "3.11"
plugins = ["ubertyped.mypy_plugin"]
Features
- ✅
AsTypedDict[T]generic type convertingdataclasses toTypeDicts - ✅
as_typed_dictutility function wrappingdataclasses.asdict - ✅ Support for usage with
TypeVars - ✅ Nested dataclasses
- ✅ Compatibility with other typecheckers such as
PylanceandPyright - ✅ Zero dependencies
Usage
from dataclasses import asdict, dataclass
from typing import Self, reveal_type
from ubertyped import AsTypedDict, as_typed_dict
@dataclass
class Base:
base: bool
@dataclass
class IntWraper:
value: int
@dataclass
class Data(Base):
version: IntWraper
command: str
def as_typed_dict(self) -> AsTypedDict[Self]:
return as_typed_dict(self)
data = Data(version=IntWraper(1), command="c", base=False)
# 🎉 Type-safe conversion!
td = as_typed_dict(data)
reveal_type(td)
# Revealed type is "TypedDict({'version': TypedDict({'value': builtins.int}), 'command': builtins.str, 'base': builtins.bool})"
# 🎉 Works with nested dataclasses too!
reveal_type(td["version"]["value"])
# Revealed type is "builtins.int"
# 🎉 Binding `Self` in methods is resolved correctly!
reveal_type(data.as_typed_dict)
# Revealed type is "def () -> TypedDict({'version': TypedDict({'value': builtins.int}), 'command': builtins.str, 'base': builtins.bool})"
# 🎉 In runtime, `as_typed_dict` is just a wrapper around `asdict`!
if asdict(data) == data.as_typed_dict():
print("✅")
else:
print("❌")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ubertyped-0.1.0.tar.gz
(5.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ubertyped-0.1.0.tar.gz.
File metadata
- Download URL: ubertyped-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.0 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a5a0379cc68910a5b75096b9bdaa8ad6035ef7151caa1d2926db7a5a92c4411
|
|
| MD5 |
1a9c57d7c7608e0c3bd87ae325163631
|
|
| BLAKE2b-256 |
7fe96d1b58367a85141e4ba0b8cdfdbcbbd5e960ea76c8174cc20f47697fe2ea
|
File details
Details for the file ubertyped-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ubertyped-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.0 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52fdbac42e4712137d769fb7cb5e5c558035813388cd8122216bec9a50becaa8
|
|
| MD5 |
447609450a4c8fbc6316342eb24cbafd
|
|
| BLAKE2b-256 |
038c1d34bde92197cf0829acf2f569b01a8da0e454bf7c31959306c755a33219
|