Skip to content

GH1226 Add Styler.map to the stubs #1228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pandas-stubs/io/formats/style.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Styler(StylerRenderer):
formatter: ExtFormatter | None = ...,
) -> None: ...
def concat(self, other: Styler) -> Styler: ...
def map(
self, func: Callable, subset: Subset | None = ..., **kwargs: Any
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self, func: Callable, subset: Subset | None = ..., **kwargs: Any
self, func: Callable[[Scalar], str], subset: Subset | None = ..., **kwargs: dict[str, Any]

docs indicate what the callable should look like, and kwargs has to be a dict

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is a little tricky since the example on the docs mention that you can pass kwargs to the function so the signature would be something like, Callable[[Scalar, kwargs], str | None].
I will type it in as Callable[[Scalar], str | None] since the docs itself mention the return type None and then we can see if users complain. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is a little tricky since the example on the docs mention that you can pass kwargs to the function so the signature would be something like, Callable[[Scalar, kwargs], str | None]. I will type it in as Callable[[Scalar], str | None] since the docs itself mention the return type None and then we can see if users complain. What do you think?

Can we do this with an overload? If kwargs is not specified, then we use a single argument in the Callable. If kwargs is specified as a dict, then a different Callable signature is needed.

Copy link

@Molkree Molkree May 21, 2025

Choose a reason for hiding this comment

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

@Dr-Irv

kwargs has to be a dict

As far as I know kwargs are annotated with the type of the values so if it can be only ints, then it's kwargs: int, if it can be anything then it's kwargs: Any

I am pretty sure we don't expect only dictionaries to be passed in kwargs here so Any seems correct

https://peps.python.org/pep-0484/#arbitrary-argument-lists-and-default-argument-values

Copy link
Member Author

Choose a reason for hiding this comment

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

The example in the docs passes a str and not a dict so indeed we may be a bit lax.

) -> Styler: ...
def set_tooltips(
self,
ttips: DataFrame,
Expand Down
10 changes: 9 additions & 1 deletion tests/test_styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

DF = DataFrame({"a": [1, 2, 3], "b": [3.14, 2.72, 1.61]})


PWD = pathlib.Path(os.path.split(os.path.abspath(__file__))[0])

if TYPE_CHECKING:
Expand Down Expand Up @@ -233,3 +232,12 @@ def test_styler_columns_and_index() -> None:
styler = DF.style
check(assert_type(styler.columns, Index), Index)
check(assert_type(styler.index, Index), Index)


def test_styler_map() -> None:
"""Test type returned with Styler.map GH1226."""
df = DataFrame(data={"col1": [1, -2], "col2": [-3, 4]})
check(
assert_type(df.style.map(lambda v: "color: red;" if v < 0 else None), Styler),
Styler,
)