My inspect module

alex_ber
2 min readMay 30, 2024

--

I’ve revisited my inspect module and added 2 more function. I didn’t write about it earlier, so I will cover all functions.

The source code you can found here. It is available as part of my AlexBerUtils s project.

You can install AlexBerUtils from PyPi:

python -m pip install -U alex-ber-utils

See here for more details explanation on how to install.

def issetdescriptor(object: Any) -> bool

This function check if object is set descriptor. The object should not be a class, method, or function, and type of the object (__class__) should has “__set__”.

def ismethod(object: Any) -> bool:

This function check if object is method. It is done my checking whether object.ismethod() or whether object is bound function.

def has_method(cls: Type, methodName: str) -> bool:

First it check if there is methodName attribute on cls. If yes, inspect.isroutine() is called to do the job.

def update_function_defaults(func: Callable, new_defaults: Optional[Dict[str, Any]] = None,
remove_defaults: Optional[List[str]] = None, is_forced: bool = False)

It merges args and kwargs, taking into account default values from func.

func is the function which default params we want effectively to update.

It is done by creating wrapper function, that changes arguments passed to func a moment before the call happens.

There are 2 different modes that this function operates on. If is_forced is False (default), only func’s params that do have default value will be updated. If True, all func’s params will be updated that are mentioned by name in new_defaults and remove_defaults.

It iterates over inspect.signature(func) and checks:

in default mode:

  • If param’s has default parameters and is found in new_defaults, new param will be generated with default value from new_defaults.
  • If param’s has default parameters and is found in remove_defaults, new param will be generated with no default.

in forced mode:

  • If param’s is found in new_defaults, new param will be generated with default value from new_defaults (regardless whether param has default value or not).
  • If param’s is found in remove_defaults, new param will be generated with no default (regardless whether param has default value or not).

--

--