Esempio n. 1
0
    def wrapper(config_cls):
        # Add __annotations__ for dataclass
        config_cls.__annotations__ = {
            field_name: parameters[field_name].annotation
            for field_name in whitelist
        }
        # Set default values
        for field_name in whitelist:
            default = parameters[field_name].default
            if default != Parameter.empty:
                setattr(config_cls, field_name, default)

        # Add hashing to support hashing list and dict
        config_cls.__hash__ = param_hash

        # Add non-recursive asdict(). dataclasses.asdict() is recursive
        def asdict(self):
            return {
                field.name: getattr(self, field.name)
                for field in fields(self)
            }

        config_cls.asdict = asdict

        return dataclass(frozen=True)(config_cls)
Esempio n. 2
0
        def wrapper(union):
            cls.REGISTRY_FROZEN = True

            def make_union_instance(inst, instance_class=None):
                inst_class = instance_class or type(inst)
                key = getattr(inst_class, "__registry_name__",
                              inst_class.__name__)
                return union(**{key: inst})

            union.make_union_instance = make_union_instance

            if not INTERNAL_TAGGED_UNION:
                # OSS TaggedUnion
                union.__annotations__ = {
                    name: Optional[t]
                    for name, t in cls.REGISTRY.items()
                }
                for name in cls.REGISTRY:
                    setattr(union, name, None)
                return dataclass(frozen=True)(union)
            else:
                # FBL TaggedUnion
                union.__annotations__ = {
                    name: t
                    for name, t in cls.REGISTRY.items()
                }
                return union
Esempio n. 3
0
def wrap_oss_with_dataclass(union):
    if not IS_FB_ENVIRONMENT:
        # OSS TaggedUnion
        return dataclass(frozen=True)(union)
    else:
        # FBL TaggedUnion
        return union