pip_shims.utils

Shared utility functions which are not specific to any particular module.

Functions

add_mixin_to_class(basecls, mixins) Given a class, adds the provided mixin classes as base classes and gives a new class
apply_alias(imported, target, *aliases) Given a target with attributes, point non-existant aliases at the first existing one
call_function_with_correct_args(fn, …) Determines which arguments from provided_kwargs to call fn and calls it.
ensure_function(parent, funcname, func) 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
fallback_is_artifact(self)
fallback_is_file_url(link)
fallback_is_vcs(self)
get_allowed_args(fn_or_class) Given a callable or a class, returns the arguments and default kwargs passed in.
get_method_args(target_method) Returns the arguments for a callable.
has_property(target, name)
make_classmethod(fn)
make_method(fn)
memoize(obj)
nullcontext(*args, **kwargs)
parse_version(version)
resolve_possible_shim(target)
set_default_kwargs(basecls, method, *args, …)
split_package(module[, subimport]) Used to determine what target to import.
suppress_setattr(obj, attr, value[, filter_none]) Set an attribute, suppressing any exceptions and skipping the attempt on failure.

Classes

BaseClassMethod(func_base, name, *args, **kwargs)
BaseMethod(func_base, name, *args, **kwargs)
class pip_shims.utils.BaseClassMethod(func_base, name, *args, **kwargs)[source]

Bases: collections.abc.Callable

class pip_shims.utils.BaseMethod(func_base, name, *args, **kwargs)[source]

Bases: collections.abc.Callable

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.fallback_is_artifact(self)[source]
pip_shims.utils.fallback_is_file_url(link)[source]
pip_shims.utils.fallback_is_vcs(self)[source]
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.has_property(target, name)[source]
pip_shims.utils.make_classmethod(fn)[source]
pip_shims.utils.make_method(fn)[source]
pip_shims.utils.memoize(obj)[source]
pip_shims.utils.nullcontext(*args, **kwargs)[source]
pip_shims.utils.parse_version(version)[source]
pip_shims.utils.resolve_possible_shim(target)[source]
pip_shims.utils.set_default_kwargs(basecls, method, *args, **default_kwargs)[source]
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:
  • module (str) – A package to import from
  • subimport (Optional[str]) – A class, function, or subpackage to import
Returns:

A 2-tuple of the corresponding import package and sub-import path

Return type:

Tuple[str, str]

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:
  • obj (Any) – Object to set the attribute on
  • attr (str) – The attribute name to set
  • value (Any) – The value to set the attribute to
  • filter_none (bool) – [description], defaults to False
Returns:

Nothing

Return type:

None

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)>)>)>