Beispiel #1
0
 def __init_subclass__(cls: Type[T], **kwargs):
     builder = CodeBuilder(cls)
     exc = None
     try:
         builder.add_from_dict()
     except Exception as e:
         exc = e
     try:
         builder.add_to_dict()
     except Exception as e:
         exc = e
     if exc:
         raise exc
Beispiel #2
0
def build_mashumaro(cls: TypeVar) -> bool:

    try:
        # don't build it twice...
        # support inherit class
        return getattr(cls, f"__is_mashu_built_{cls.__name__}__")

    except AttributeError:

        builder = CodeBuilder(cls)
        builder.add_from_dict()
        builder.add_to_dict()

        # done
        setattr(cls, f"__is_mashu_built_{cls.__name__}__", True)

    return True
Beispiel #3
0
"""
tests/test-fwd-decl.py

No inheritance
forward declare classes
"""
from dataclasses import dataclass

import mashuhelpa  # patches CodeBuilder on import
from mashumaro.serializer.base.metaprogramming import CodeBuilder


@dataclass
class Member:
    """Provides a member for MainClass."""

    count: int


@dataclass
class MainClass:
    """Contains a previously declared member class."""

    member: Member


builder = CodeBuilder(MainClass)
builder.add_from_dict()
Beispiel #4
0
 def __init_subclass__(cls, **kwargs):
     builder = CodeBuilder(cls)
     builder.add_from_dict()
     builder.add_to_dict()
Beispiel #5
0
def test_get_unknown_declared_hook():
    builder = CodeBuilder(object)
    assert builder.get_declared_hook("unknown_name") is None