Skip to content

func

func_dataclass(func, method_name='__call__', cls_name=None, bases=())

Wraps a function into a new dataclass type with a single method whose positional arguments (other than self) are equivalent to that of the given function, and whose kwargs are dataclass parameters.

Parameters:

Name Type Description Default
func Callable[[Any], Any]

Function to convert to a dataclass

required
method_name str

Name of method that will call the function

'__call__'
cls_name Optional[str]

Name of the new type (if None, converts the function's name from snake case to camel case)

None
bases Bases

Base classes for the new type

()

Returns:

Type Description
type

A new type inheriting from bases with the name cls_name and a single method method_name

Source code in fancy_dataclass/func.py
def func_dataclass(func: Callable[[Any], Any], method_name: str = '__call__', cls_name: Optional[str] = None, bases: Bases = ()) -> type:
    """Wraps a function into a new dataclass type with a single method whose positional arguments (other than self) are equivalent to that of the given function, and whose kwargs are dataclass parameters.

    Args:
        func: Function to convert to a dataclass
        method_name: Name of method that will call the function
        cls_name: Name of the new type (if `None`, converts the function's name from snake case to camel case)
        bases: Base classes for the new type

    Returns:
        A new type inheriting from `bases` with the name `cls_name` and a single method `method_name`"""
    bases = tuple(bases) if isinstance(bases, Sequence) else (bases,)
    return _func_dataclass(func, method_name=method_name, cls_name=cls_name, bases=bases)