Skip to main content

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.11
  • mypy>=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 converting dataclasses to TypeDicts
  • as_typed_dict utility function wrapping dataclasses.asdict
  • ✅ Support for usage with TypeVars
  • ✅ Nested dataclasses
  • ✅ Compatibility with other typecheckers such as Pylance and Pyright
  • ✅ 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.0.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ubertyped-0.0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file ubertyped-0.0.3.tar.gz.

File metadata

  • Download URL: ubertyped-0.0.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.12.0 Darwin/23.1.0

File hashes

Hashes for ubertyped-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b32ed83efdfef3c2f4a3be8f75871c61a41a434fa17c3545f52db39af775c116
MD5 3a71f1f211ad53e228878cf8cbc3f322
BLAKE2b-256 dde4b3e862dd5fe0d70695bbb33c60f3b6d20607ae125dd9bde3b9f853c166a8

See more details on using hashes here.

File details

Details for the file ubertyped-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: ubertyped-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 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

Hashes for ubertyped-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d784d0e7cf8112c42ebd2d9f8d930993cd6fe7e82e792cab05d11edeec5c5ca3
MD5 5a75c1e58f50f2dbb564a273ed408ca7
BLAKE2b-256 c715892f27386c87f3c03bd41481bb17846e0a260fd6102775656bf62827f470

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page