pip_shims.utils¶
Shared utility functions which are not specific to any particular module.
-
class
pip_shims.utils.
BaseClassMethod
(func_base, name, *args, **kwargs)[source]¶ Bases:
collections.abc.Callable
-
_abc_impl
= <_abc_data object>¶
-
-
class
pip_shims.utils.
BaseMethod
(func_base, name, *args, **kwargs)[source]¶ Bases:
collections.abc.Callable
-
_abc_impl
= <_abc_data object>¶
-
-
pip_shims.utils.
add_mixin_to_class
(basecls, mixins)[source]¶ Given a class, adds the provided mixin classes as base classes and gives a new class
Parameters: - basecls (Type) – An initial class to generate a new class from
- mixins (List[Type]) – A list of mixins to add as base classes
Returns: A new class with the provided mixins as base classes
Return type: Type[basecls, *mixins]
-
pip_shims.utils.
apply_alias
(imported, target, *aliases)[source]¶ Given a target with attributes, point non-existant aliases at the first existing one
Parameters: - Type] imported (Union[ModuleType,) – A Module or Class base
- target (Any) – The target which is a member of imported and will have aliases
- aliases (str) – A list of aliases, the first found attribute will be the basis for all non-existant names which will be created as pointers
Returns: The original target
Return type: Any
-
pip_shims.utils.
call_function_with_correct_args
(fn, **provided_kwargs)[source]¶ Determines which arguments from provided_kwargs to call fn and calls it.
Consumes a list of allowed arguments (e.g. from
getargs()
) and uses it to determine which of the arguments in the provided kwargs should be passed through to the given callable.Parameters: - fn (Callable) – A callable which has some dynamic arguments
- allowed_args (List[str]) – A list of allowed arguments which can be passed to the supplied function
Returns: The result of calling the function
Return type: Any
-
pip_shims.utils.
ensure_function
(parent, funcname, func)[source]¶ Given a module, a function name, and a function object, attaches the given function to the module and ensures it is named properly according to the provided argument
Parameters: - parent (Any) – The parent to attack the function to
- funcname (str) – The name to give the function
- func (Callable) – The function to rename and attach to parent
Returns: The function with its name, qualname, etc set to mirror parent
Return type: Callable
-
pip_shims.utils.
filter_allowed_args
(fn, **provided_kwargs)[source]¶ Given a function and a kwarg mapping, return only those kwargs used in the function.
Parameters: - fn (Callable) – A function to inspect
- Any] kwargs (Dict[str,) – A mapping of kwargs to filter
Returns: A new, filtered kwarg mapping
Return type: Tuple[List[Any], Dict[str, Any]]
-
pip_shims.utils.
get_allowed_args
(fn_or_class)[source]¶ Given a callable or a class, returns the arguments and default kwargs passed in.
Parameters: Type] fn_or_class (Union[Callable,) – A function, method or class to inspect. Returns: A 2-tuple with a list of arguments and a dictionary of keywords mapped to default values. Return type: Tuple[List[str], Dict[str, Any]]
-
pip_shims.utils.
get_method_args
(target_method)[source]¶ Returns the arguments for a callable.
Parameters: target_method (Callable) – A callable to retrieve arguments for Returns: A 2-tuple of the original callable and its resulting arguments Return type: Tuple[Callable, Optional[inspect.Arguments]]
-
pip_shims.utils.
split_package
(module, subimport=None)[source]¶ Used to determine what target to import.
Either splits off the final segment or uses the provided sub-import to return a 2-tuple of the import path and the target module or sub-path.
Parameters: Returns: A 2-tuple of the corresponding import package and sub-import path
Return type: Example: >>> from pip_shims.utils import split_package >>> split_package("pip._internal.req.req_install", subimport="InstallRequirement") ("pip._internal.req.req_install", "InstallRequirement") >>> split_package("pip._internal.cli.base_command") ("pip._internal.cli", "base_command")
-
pip_shims.utils.
suppress_setattr
(obj, attr, value, filter_none=False)[source]¶ Set an attribute, suppressing any exceptions and skipping the attempt on failure.
Parameters: Returns: Nothing
Return type: Example: >>> class MyClass(object): ... def __init__(self, name): ... self.name = name ... self.parent = None ... def __repr__(self): ... return "<{0!r} instance (name={1!r}, parent={2!r})>".format( ... self.__class__.__name__, self.name, self.parent ... ) ... def __str__(self): ... return self.name >>> me = MyClass("Dan") >>> dad = MyClass("John") >>> grandfather = MyClass("Joe") >>> suppress_setattr(dad, "parent", grandfather) >>> dad <'MyClass' instance (name='John', parent=<'MyClass' instance (name='Joe', parent=None )>)> >>> suppress_setattr(me, "parent", dad) >>> me <'MyClass' instance (name='Dan', parent=<'MyClass' instance (name='John', parent=<'My Class' instance (name='Joe', parent=None)>)>)> >>> suppress_setattr(me, "grandparent", grandfather) >>> me <'MyClass' instance (name='Dan', parent=<'MyClass' instance (name='John', parent=<'My Class' instance (name='Joe', parent=None)>)>)>