Getting Arguments

starstar.signature(f: Callable, required=True) Signature[source]

Get a function signature.

Faster than inspect.signature (after the first call) because it is cached using the standard f.__signature__ attribute.

starstar.get_args(func, match=(), ignore=())[source]

Get argument parameters matching a specific type.

Parameters:
  • func (callable) – The function to inspect.

  • match (int or set) – Argument types to filter for.

  • ignore (int or set) – Argument types to filter out.

Returns:

a list containing the matching parameter objects.

Return type:

(list)

def func(x, y=3, *a, z=4, **kw):
    pass

# get all arguments
args = starstar.get_args(func)
assert all(isinstance(p, inspect.Parameter) for p in args)
assert [p.name for p in args] == ['x', 'y', 'a', 'z', 'kw']

# get positional arguments
args = starstar.get_args(func, starstar.POS)
assert [p.name for p in args] == ['x', 'y']

# get arguments excluding *a, **kw
args = starstar.get_args(func, ignore=starstar.VAR)
assert [p.name for p in args] == ['x', 'y', 'z']

# get keyword arguments
args = starstar.get_args(func, ignore=starstar.KW)
assert [p.name for p in args] == ['x', 'y', 'z']

# get keyword only arguments
args = starstar.get_args(func, ignore=starstar.KW_ONLY)
assert [p.name for p in args] == ['z']
starstar.as_args_kwargs(func, kw)[source]

Separate out positional and keyword arguments using a function’s signature.

Sometimes functions have position only arguments, but you still want to be able to configure them in a single dictionary. In order to pass them to a function you need to separate them out first. This will separate out all arguments that can be passed as positional arguments.

Parameters:
  • func (callable) – The function that the arguments will be passed to.

  • values (dict) – The parameter values you want to pass to the function. NOTE: This value is not modified.

Returns:

A tuple containing
  • a (list): The positional arguments we could pull out.

  • kw (dict): The keyword args that are left over.

Return type:

(tuple)

def func_a(a, b, c, *, d=0):
    return a, b, c, d

# split out the args and kwargs
a, kw = starstar.as_args_kwargs(func_a, {'a': 1, 'b': 2, 'c': 3, 'd': 4})
assert a == [1, 2, 3]
assert kw == {'d': 4}

#
assert func_a(*a, **kw) == (1, 2, 3, 4)

starstar also provides shortcuts for parameter types in inspect.Parameter

Single Argument Types (int):

def func(POS_ONLY, /, POS_KW, *VAR_POS, KW_ONLY, **VAR_KW):
    ...

Name

Description

starstar.POS_ONLY

Positional Only

starstar.POS_KW

Positional or Keyword

starstar.VAR_POS

Variable Positional (*)

starstar.KW_ONLY

Keyword Only

starstar.VAR_KW

Variable Keyword (**)

Combination Argument Types (set):

Name

Description

starstar.ALL

= {POS_ONLY, KW_ONLY, POS_KW, VAR_POS, VAR_KW}

starstar.POS

= {POS_ONLY, POS_KW}

starstar.KW

= {POS_KW, KW_ONLY}

starstar.VAR

= {VAR_POS, VAR_KW}