Skip to content

kshramt/type_casting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

type_casting

Table of Contents
import dataclasses
import typing

import type_casting


@dataclasses.dataclass
class AdamConf:
    name: typing.Literal["Adam"]
    learning_rate: float


@dataclasses.dataclass
class SGDConf:
    name: typing.Literal["SGD"]
    learning_rate: float


@dataclasses.dataclass
class Conf:
    n_units: int
    optimizer: typing.Union[AdamConf, SGDConf]


conf_dict = dict(n_units=100, optimizer=dict(name="Adam", learning_rate=1e-2))
type_casting.override(conf_dict, ['optimizer.name="SGD"'])

conf = type_casting.cast(
    Conf,
    conf_dict,
)
assert conf == Conf(
    n_units=100, optimizer=SGDConf(name="SGD", learning_rate=1e-2)
)

assert type_casting.cast(
    type_casting.Call[str],
    dict(
        fn="__main__.Conf",
        kwargs=dict(
            n_units=100,
            optimizer=dict(name="SGD", learning_rate=1e-2),
        )
    ),
) == Conf(
    n_units=100, optimizer=SGDConf(name="SGD", learning_rate=1e-2)
)

Release

python3 -m venv venv
venv/bin/pip install -e .[dev]
venv/bin/python3 -m build --wheel
venv/bin/twine upload dist/type_casting-xxx.whl

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages