Home / Function/ required_args() — anthropic-sdk-python Function Reference

required_args() — anthropic-sdk-python Function Reference

Architecture documentation for the required_args() function in _utils.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  c21a9edf_5371_2d8f_04bd_5600300ac276["required_args()"]
  875202ec_3744_577d_9ec4_ed9fbc6aaf41["_utils.py"]
  c21a9edf_5371_2d8f_04bd_5600300ac276 -->|defined in| 875202ec_3744_577d_9ec4_ed9fbc6aaf41
  7490d04c_c7f7_7106_1f97_c167a8b2c517["human_join()"]
  c21a9edf_5371_2d8f_04bd_5600300ac276 -->|calls| 7490d04c_c7f7_7106_1f97_c167a8b2c517
  3b4a3e6a_1a3d_24c5_eef7_8cd5887991cc["quote()"]
  c21a9edf_5371_2d8f_04bd_5600300ac276 -->|calls| 3b4a3e6a_1a3d_24c5_eef7_8cd5887991cc
  style c21a9edf_5371_2d8f_04bd_5600300ac276 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_utils/_utils.py lines 214–286

def required_args(*variants: Sequence[str]) -> Callable[[CallableT], CallableT]:
    """Decorator to enforce a given set of arguments or variants of arguments are passed to the decorated function.

    Useful for enforcing runtime validation of overloaded functions.

    Example usage:
    ```py
    @overload
    def foo(*, a: str) -> str: ...


    @overload
    def foo(*, b: bool) -> str: ...


    # This enforces the same constraints that a static type checker would
    # i.e. that either a or b must be passed to the function
    @required_args(["a"], ["b"])
    def foo(*, a: str | None = None, b: bool | None = None) -> str: ...
    ```
    """

    def inner(func: CallableT) -> CallableT:
        params = inspect.signature(func).parameters
        positional = [
            name
            for name, param in params.items()
            if param.kind
            in {
                param.POSITIONAL_ONLY,
                param.POSITIONAL_OR_KEYWORD,
            }
        ]

        @functools.wraps(func)
        def wrapper(*args: object, **kwargs: object) -> object:
            given_params: set[str] = set()
            for i, _ in enumerate(args):
                try:
                    given_params.add(positional[i])
                except IndexError:
                    raise TypeError(
                        f"{func.__name__}() takes {len(positional)} argument(s) but {len(args)} were given"
                    ) from None

            for key in kwargs.keys():
                given_params.add(key)

            for variant in variants:
                matches = all((param in given_params for param in variant))
                if matches:
                    break
            else:  # no break
                if len(variants) > 1:
                    variations = human_join(
                        ["(" + human_join([quote(arg) for arg in variant], final="and") + ")" for variant in variants]
                    )
                    msg = f"Missing required arguments; Expected either {variations} arguments to be given"
                else:
                    assert len(variants) > 0

                    # TODO: this error message is not deterministic
                    missing = list(set(variants[0]) - given_params)
                    if len(missing) > 1:
                        msg = f"Missing required arguments: {human_join([quote(arg) for arg in missing])}"
                    else:
                        msg = f"Missing required argument: {quote(missing[0])}"
                raise TypeError(msg)
            return func(*args, **kwargs)

        return wrapper  # type: ignore

    return inner

Subdomains

Frequently Asked Questions

What does required_args() do?
required_args() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_utils/_utils.py.
Where is required_args() defined?
required_args() is defined in src/anthropic/_utils/_utils.py at line 214.
What does required_args() call?
required_args() calls 2 function(s): human_join, quote.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free