kwarg_has_default#

deeptrack.utils.kwarg_has_default(function: Callable[[...], Any], argument: str) bool#

Check whether a specific argument of a function has a default value.

Parameters#

function: Callable[…, Any]

The function to inspect.

argument: str

Name of the argument to check.

Returns#

bool

True if the specified argument has a default value.

Examples#

from deeptrack.utils import kwarg_has_default

Check default values for positional and keyword-only arguments:

>>> def f(a, b=2, c=3):
...     pass
>>> kwarg_has_default(f, "a")
False
>>> kwarg_has_default(f, "b")
True
>>> kwarg_has_default(f, "c")
True

Missing argument:

>>> kwarg_has_default(f, "not_present")
False

Keyword-only arguments without defaults:

>>> def g(*, flag):
...     pass
>>> kwarg_has_default(g, "flag")
False

Method example:

>>> class MyClass:
...     def method(self, x, y=42):
...         pass
>>> kwarg_has_default(MyClass.method, "self")
False
>>> kwarg_has_default(MyClass.method, "x")
False
>>> kwarg_has_default(MyClass.method, "y")
True