Skip to content

Fix issue where double decorator does not parse function name #320

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions ultraplot/axes/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5266,7 +5266,6 @@ def streamplot(self, x, y, u, v, c, **kwargs):
return m

@inputs._parse_triangulation_with_preprocess("x", "y", "z", keywords=["triangles"])
@inputs._preprocess_or_redirect("x", "y", "z")
@docstring._concatenate_inherited
@docstring._snippet_manager
def tricontour(self, *args, **kwargs):
Expand Down Expand Up @@ -5308,7 +5307,6 @@ def tricontour(self, *args, **kwargs):
return m

@inputs._parse_triangulation_with_preprocess("x", "y", "z", keywords=["triangles"])
@inputs._preprocess_or_redirect("x", "y", "z")
@docstring._concatenate_inherited
@docstring._snippet_manager
def tricontourf(self, *args, **kwargs):
Expand Down
16 changes: 11 additions & 5 deletions ultraplot/internals/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,22 @@ def _parse_triangulation_with_preprocess(*keys, keywords=None, allow_extra=True)
"""

def _decorator(func):
@_preprocess_or_redirect(*keys, keywords=keywords, allow_extra=allow_extra)
def wrapper(self, *args, **kwargs):
# Parse triangulation inputs after preprocessing
def triangulation_wrapper(self, *args, **kwargs):
triangulation, z, remaining_args, updated_kwargs = (
_parse_triangulation_inputs(*args, **kwargs)
)
# Call the original function with parsed inputs
return func(self, triangulation, z, *remaining_args, **updated_kwargs)

return wrapper
# Manually set the name to the original function's name
triangulation_wrapper.__name__ = func.__name__

final_wrapper = _preprocess_or_redirect(
*keys, keywords=keywords, allow_extra=allow_extra
)(triangulation_wrapper)

# Finally make sure all other metadata is correct
functools.update_wrapper(final_wrapper, func)
return final_wrapper

return _decorator

Expand Down
17 changes: 17 additions & 0 deletions ultraplot/tests/test_inputs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ultraplot as uplt, pytest, numpy as np
from unittest.mock import Mock


@pytest.mark.parametrize(
Expand All @@ -16,3 +17,19 @@ def test_to_numpy_array(data, dtype):
"""
arr = uplt.internals.inputs._to_numpy_array(data)
assert arr.dtype == dtype, f"Expected dtype {dtype}, got {arr.dtype}"


def test_name_preserved_and_args_processed():
"""
Check if the name is preserved across nested decoration
for tri-related functions
"""
parser_mock = Mock(return_value=("triang", "zval", None, None))

def tripcolor(self, tri, z, extra=None, kw=None):
return "ok"

decorated = uplt.internals.inputs._parse_triangulation_with_preprocess()(tripcolor)

# Test that the decorator preserves the function name
assert decorated.__name__ == "tripcolor"