Function Argument Wrapper for Enhanced Handling

1.0.1 · active · verified Mon Apr 13

func-args is a lightweight Python library (current version 1.0.1) designed for creating wrapper functions with enhanced argument handling. It addresses common challenges encountered with third-party APIs that have suboptimal interface designs, allowing developers to explicitly mark parameters as required or optional using special sentinel values (REQ and OPT). The library is actively maintained with a stable release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a function using the `@delegator` decorator and `REQ`/`OPT` sentinel values. `REQ` marks a parameter as mandatory, ensuring a `TypeError` is raised if it's not provided. `OPT` acts as a placeholder for optional parameters, allowing you to differentiate between an explicitly passed `None` and an omitted optional argument.

from func_args import REQ, OPT, delegator

@delegator
def my_function(required_param: REQ, optional_param: OPT = None):
    """An example function using REQ and OPT for argument handling."""
    print(f"Required: {required_param}")
    if optional_param is not OPT:
        print(f"Optional: {optional_param}")
    else:
        print("Optional parameter was not provided.")
    return {"required": required_param, "optional": optional_param}

# Example 1: Providing both required and optional
result1 = my_function(required_param="hello", optional_param="world")
print(f"Result 1: {result1}")

# Example 2: Providing only required, optional defaults to OPT sentinel
result2 = my_function(required_param=123)
print(f"Result 2: {result2}")

# Example 3: Missing a required argument (will raise TypeError)
try:
    my_function()
except TypeError as e:
    print(f"Error: {e}")

view raw JSON →