A comprehensive dataclass instance creation library that enables bidirectional conversion between dictionaries and dataclass instances.
npx @tessl/cli install tessl/pypi-dataclass-factory@1.1.0A comprehensive Python library that provides bidirectional conversion between dictionaries and dataclass instances. It fills the gap left by Python's standard asdict() method by enabling conversion from dictionaries back to dataclass objects with flexible parsing capabilities, custom type handling, and naming policy support.
pip install dataclass_factoryfrom dataclass_factory import ParserFactory, SerializerFactory, NameStyleFor simple use cases, you can use the convenience function:
from dataclass_factory import parseFor legacy compatibility with dataclasses.asdict():
from dataclass_factory import dict_factoryfrom dataclasses import dataclass
from dataclass_factory import ParserFactory, SerializerFactory
@dataclass
class User:
name: str
age: int
email: str = "unknown@example.com"
# Create parser and serializer factories
parser_factory = ParserFactory()
serializer_factory = SerializerFactory()
# Convert dict to dataclass
user_data = {
"name": "John Doe",
"age": 30
}
user = parser_factory.get_parser(User)(user_data)
# Result: User(name="John Doe", age=30, email="unknown@example.com")
# Convert dataclass back to dict
user_dict = serializer_factory.get_serializer(User)(user)
# Result: {"name": "John Doe", "age": 30, "email": "unknown@example.com"}The library uses a factory pattern with caching for optimal performance:
Convert dictionaries and other data structures into dataclass instances with support for complex nested types, Optional fields, Union types, collections, and custom parsing logic.
class ParserFactory:
def __init__(
self,
trim_trailing_underscore: bool = True,
debug_path: bool = False,
type_factories: Dict[Type, Parser] = None,
name_styles: Dict[Type, NameStyle] = None,
): ...
def get_parser(self, cls: ClassVar) -> Parser: ...
def parse(
data,
cls,
trim_trailing_underscore: bool = True,
type_factories: Dict[Any, Callable] = None,
): ...Convert dataclass instances back to dictionaries with performance optimization (up to 10x faster than standard asdict), custom serializers, and naming policy support.
class SerializerFactory:
def __init__(
self,
trim_trailing_underscore: bool = True,
debug_path: bool = False,
type_serializers: Dict[Type, Serializer] = None,
name_styles: Dict[Type, NameStyle] = None,
): ...
def get_serializer(self, cls: Any) -> Serializer: ...Convert between different field naming conventions when parsing and serializing dataclasses, supporting snake_case, camelCase, kebab-case, and CamelCase transformations.
class NameStyle(Enum):
snake = "snake_case"
kebab = "kebab-case"
camel_lower = "camelCaseLower"
camel = "CamelCase"
def convert_name(
name: str,
trim_trailing_underscore: bool = True,
naming_policy: NameStyle = None
) -> str: ...from typing import Callable, Any, Dict, Type
# Note: These type aliases are used internally but not officially exported
Parser = Callable[[Any], Any]
Serializer = Callable[[Any], Any]The library supports parsing and serialization for: