Ejemplo n.º 1
0
def test_ipy_display_hook__no_special_repr_methods():
    console = Console(file=io.StringIO(), force_jupyter=True)

    class Thing:
        def __repr__(self) -> str:
            return "hello"

    console.begin_capture()
    _ipy_display_hook(Thing(), console=console)

    # No IPython special repr methods, so printed by Rich
    assert console.end_capture() == "hello\n"
Ejemplo n.º 2
0
def test_ipy_display_hook__repr_html():
    console = Console(file=io.StringIO(), force_jupyter=True)

    class Thing:
        def _repr_html_(self):
            return "hello"

    console.begin_capture()
    _ipy_display_hook(Thing(), console=console)

    # Rendering delegated to notebook because _repr_html_ method exists
    assert console.end_capture() == ""
Ejemplo n.º 3
0
def test_ipy_display_hook__classes_to_not_render():
    console = Console(file=io.StringIO(), force_jupyter=True)
    console.begin_capture()

    class Thing:
        def __repr__(self) -> str:
            return "hello"

    class_fully_qualified_name = f"{__name__}.{Thing.__qualname__}"
    with patch("rich.pretty.JUPYTER_CLASSES_TO_NOT_RENDER",
               {class_fully_qualified_name}):
        _ipy_display_hook(Thing(), console=console)
    assert console.end_capture() == ""
Ejemplo n.º 4
0
def test_ipy_display_hook__multiple_special_reprs():
    """
    The case where there are multiple IPython special _repr_*_
    methods on the object, and one of them returns None but another
    one does not.
    """
    console = Console(file=io.StringIO(), force_jupyter=True)

    class Thing:
        def _repr_latex_(self):
            return None

        def _repr_html_(self):
            return "hello"

    console.begin_capture()
    _ipy_display_hook(Thing(), console=console)

    assert console.end_capture() == ""
Ejemplo n.º 5
0
def test_ipy_display_hook__special_repr_raises_exception():
    """
    When an IPython special repr method raises an exception,
    we treat it as if it doesn't exist and look for the next.
    """
    console = Console(file=io.StringIO(), force_jupyter=True)

    class Thing:
        def _repr_markdown_(self):
            raise Exception()

        def _repr_latex_(self):
            return None

        def _repr_html_(self):
            return "hello"

    console.begin_capture()
    _ipy_display_hook(Thing(), console=console)

    assert console.end_capture() == ""
Ejemplo n.º 6
0
def test_ipy_display_hook__console_renderables_on_newline():
    console = Console(file=io.StringIO(), force_jupyter=True)
    console.begin_capture()
    _ipy_display_hook(Text("hello"), console=console)
    assert console.end_capture() == "\nhello\n"