Skip to content
3 changes: 2 additions & 1 deletion pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

"""
import os
from typing import Callable
import warnings

import pandas._config.config as cf
Expand Down Expand Up @@ -879,7 +880,7 @@ def register_converter_cb(key):
"format.formatter",
None,
styler_formatter,
validator=is_instance_factory([type(None), dict, callable, str]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm why doesn't this work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x = '{:.2f}'

    def inner(x) -> None:
>       if not isinstance(x, _type):
E       TypeError: isinstance() arg 2 must be a type or tuple of types
x = <function <lambda> at 0x1251dd160>

    def inner(x) -> None:
>       if not isinstance(x, _type):
E       TypeError: isinstance() arg 2 must be a type or tuple of types

Also the signature of of the is_instance_factory struggles with callable:

>>> isinstance(lambda x: x, callable)
False
>>> isinstance(lambda x: x, type(callable))
False
>>> callable(lambda x: x)
True

Copy link
Contributor Author

@attack68 attack68 Sep 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fixed: callable != Callable !!!

validator=is_instance_factory([type(None), dict, Callable, str]),
)

cf.register_option("html.mathjax", True, styler_mathjax, validator=is_bool)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/io/formats/style/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,28 @@ def test_precision_zero(df):
assert ctx["body"][1][2]["display_value"] == "-1"


@pytest.mark.parametrize(
"formatter, exp",
[
(lambda x: f"{x:.3f}", "9.000"),
("{:.2f}", "9.00"),
({0: "{:.1f}"}, "9.0"),
(None, "9"),
],
)
def test_formatter_options_validator(formatter, exp):
df = DataFrame([[9]])
with option_context("styler.format.formatter", formatter):
assert f" {exp} " in df.style.to_latex()


def test_formatter_options_raises():
msg = "Value must be an instance of"
with pytest.raises(ValueError, match=msg):
with option_context("styler.format.formatter", ["bad", "type"]):
DataFrame().style.to_latex()


def test_1level_multiindex():
# GH 43383
midx = MultiIndex.from_product([[1, 2]], names=[""])
Expand Down