MCPcopy Index your code
hub / github.com/ipython/ipython / MagicAlias

Class MagicAlias

IPython/core/magic.py:780–827  ·  view source on GitHub ↗

An alias to another magic function. An alias is determined by its magic name and magic kind. Lookup is done at call time, so if the underlying magic changes the alias will call the new function. Use the :meth:`MagicsManager.register_alias` method or the `%alias_magic` magic fun

Source from the content-addressed store, hash-verified

778 return opts, args
779
780class MagicAlias:
781 """An alias to another magic function.
782
783 An alias is determined by its magic name and magic kind. Lookup
784 is done at call time, so if the underlying magic changes the alias
785 will call the new function.
786
787 Use the :meth:`MagicsManager.register_alias` method or the
788 `%alias_magic` magic function to create and register a new alias.
789 """
790
791 def __init__(
792 self,
793 shell: InteractiveShell,
794 magic_name: str,
795 magic_kind: _MagicKind,
796 magic_params: str | None = None,
797 ) -> None:
798 self.shell = shell
799 self.magic_name = magic_name
800 self.magic_params = magic_params
801 self.magic_kind = magic_kind
802
803 self.pretty_target = "%s%s" % (magic_escapes[self.magic_kind], self.magic_name)
804 self.__doc__ = "Alias for `%s`." % self.pretty_target
805
806 self._in_call = False
807
808 def __call__(self, *args: Any, **kwargs: Any) -> Any:
809 """Call the magic alias."""
810 fn = self.shell.find_magic(self.magic_name, self.magic_kind) # type: ignore[no-untyped-call]
811 if fn is None:
812 raise UsageError("Magic `%s` not found." % self.pretty_target)
813
814 # Protect against infinite recursion.
815 if self._in_call:
816 raise UsageError(
817 "Infinite recursion detected; magic aliases cannot call themselves."
818 )
819 self._in_call = True
820 try:
821 if self.magic_params:
822 args_list = list(args)
823 args_list[0] = self.magic_params + " " + args[0]
824 args = tuple(args_list)
825 return fn(*args, **kwargs)
826 finally:
827 self._in_call = False

Callers 1

register_aliasMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…