Ü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 IntWrapper:
value: int
@dataclass
class Data(Base):
version: IntWrapper
command: str
def as_typed_dict(self) -> AsTypedDict[Self]:
return as_typed_dict(self)
data = Data(version=IntWrapper(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("❌")
Metadata
Release files for ubertyped 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ubertyped-0.1.2.tar.gz | 5.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ubertyped-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / ubertyped-0.1.2.tar.gz
| Download URL | ubertyped-0.1.2.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a50e8e317764e926b530f2c16fb2335e8b92c519b90744f7258e831e1ab5fec8
|
|
BLAKE2b-256 checksum How to use checksums |
0f48180c1e064ec52519ecba0072b20daa8bcb3d4f6686993472e9bab52a2a6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.0 Darwin/23.1.0
|
Release files / ubertyped-0.1.2-py3-none-any.whl
| Download URL | ubertyped-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea32a652e971498c83cafd3b7e09c16c675d5a72e6ad30c1b1f4b7ded13badaa
|
|
BLAKE2b-256 checksum How to use checksums |
92ae799ddeb9497b1a390568ba32900032615732634624aaac26282031a696b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.0 Darwin/23.1.0
|
Security for ubertyped 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.