Skip to content

Custom lazy magics do not show help (?) until they are imported #15383

Description

@krassowski

Prior to 9.17.0 it did not work at all. Since 9.17.0 it sometimes works on the second attempt (depending on the order of registration):

import sys, types
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.magic import Magics, cell_magic, line_magic, magics_class


@magics_class
class LineHalf(Magics):
    @line_magic
    def dual(self, line):
        """The line half of %dual."""


@magics_class
class CellHalf(Magics):
    @cell_magic
    def dual(self, line, cell):
        """The cell half of %%dual."""


sys.modules["provider"] = provider = types.ModuleType("provider")
provider.LineHalf, provider.CellHalf = LineHalf, CellHalf

ip = InteractiveShell()
ip.magics_manager.register_lazy("dual", "provider:LineHalf", "line")
ip.magics_manager.register_lazy("dual", "provider:CellHalf", "cell")

ip.run_cell("%%dual?")   # does not work "Object `%%dual` not found."
ip.run_cell("%%dual?")   # works, shows "Docstring: The cell half of %%dual."

If we swap registration order neither works:

import sys, types
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.magic import Magics, cell_magic, line_magic, magics_class


@magics_class
class LineHalf(Magics):
    @line_magic
    def dual(self, line):
        """The line half of %dual."""


@magics_class
class CellHalf(Magics):
    @cell_magic
    def dual(self, line, cell):
        """The cell half of %%dual."""


sys.modules["provider"] = provider = types.ModuleType("provider")
provider.LineHalf, provider.CellHalf = LineHalf, CellHalf

ip = InteractiveShell()
ip.magics_manager.register_lazy("dual", "provider:CellHalf", "cell")
ip.magics_manager.register_lazy("dual", "provider:LineHalf", "line")

ip.run_cell("%%dual?")   # does not work "Object `%%dual` not found."
ip.run_cell("%%dual?")   #  does not work "Object `%%dual` not found."

Also, there are issues when overwriting built-in dual magic with say only a line magic:

import sys, types
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.magic import Magics, line_magic, magics_class


@magics_class
class MyMagics(Magics):
    @line_magic
    def time(self, line):
        """My own %time."""


sys.modules["provider"] = provider = types.ModuleType("provider")
provider.MyMagics = MyMagics

ip = InteractiveShell()
ip.magics_manager.register_lazy("time", "provider:MyMagics")

ip.run_cell("time?")            # my own %time
ip.run_cell("%%time?")          # expected: IPython's %%time docs
ip.run_cell("%%time\npass")     # expected: a wall time report

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions