MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _resolve_func_self

Method _resolve_func_self

cmd2/cmd2.py:5916–5966  ·  view source on GitHub ↗

Attempt to resolve a candidate instance to pass as 'self'. Used for an unbound class method that was used when defining command's argparse object. Since we restrict registration to only a single CommandSet instance of each type, using type is a reasonably safe way to resolv

(
        self,
        cmd_support_func: Callable[..., Any],
        cmd_self: CmdOrSet | None,
    )

Source from the content-addressed store, hash-verified

5914 self._cmdfinalization_hooks.append(func)
5915
5916 def _resolve_func_self(
5917 self,
5918 cmd_support_func: Callable[..., Any],
5919 cmd_self: CmdOrSet | None,
5920 ) -> object | None:
5921 """Attempt to resolve a candidate instance to pass as 'self'.
5922
5923 Used for an unbound class method that was used when defining command's argparse object.
5924
5925 Since we restrict registration to only a single CommandSet
5926 instance of each type, using type is a reasonably safe way to resolve the correct object instance.
5927
5928 :param cmd_support_func: command support function. This could be a completer or namespace provider
5929 :param cmd_self: The `self` associated with the command or subcommand
5930 """
5931 # figure out what class the command support function was defined in
5932 func_class: type[Any] | None = get_defining_class(cmd_support_func)
5933
5934 # Was there a defining class identified? If so, is it a sub-class of CommandSet?
5935 if func_class is not None and issubclass(func_class, CommandSet):
5936 # Since the support function is provided as an unbound function, we need to locate the instance
5937 # of the CommandSet to pass in as `self` to emulate a bound method call.
5938 # We're searching for candidates that match the support function's defining class type in this order:
5939 # 1. Is the command's CommandSet a sub-class of the support function's class?
5940 # 2. Do any of the registered CommandSets in the Cmd2 application exactly match the type?
5941 # 3. Is there a registered CommandSet that is is the only matching subclass?
5942
5943 func_self: CmdOrSet | None
5944
5945 # check if the command's CommandSet is a sub-class of the support function's defining class
5946 if isinstance(cmd_self, func_class):
5947 # Case 1: Command's CommandSet is a sub-class of the support function's CommandSet
5948 func_self = cmd_self
5949 else:
5950 # Search all registered CommandSets
5951 func_self = None
5952 candidate_sets: list[CommandSet[Any]] = []
5953 for installed_cmd_set in self._installed_command_sets:
5954 if type(installed_cmd_set) == func_class: # noqa: E721
5955 # Case 2: CommandSet is an exact type match for the function's CommandSet
5956 func_self = installed_cmd_set
5957 break
5958
5959 # Add candidate for Case 3:
5960 if isinstance(installed_cmd_set, func_class):
5961 candidate_sets.append(installed_cmd_set)
5962 if func_self is None and len(candidate_sets) == 1:
5963 # Case 3: There exists exactly 1 CommandSet that is a sub-class match of the function's CommandSet
5964 func_self = candidate_sets[0]
5965 return func_self
5966 return self

Callers 2

cmd_wrapperFunction · 0.80

Calls 2

get_defining_classFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected