Skip to content

Commit

Permalink
fix,refac:utils: Update @no_redecorate
Browse files Browse the repository at this point in the history
- Fix: `NameError` when the object has already been decorated.
- Change: Set marker attribute value to `Ellipsis` instead of `True`.
- Change: Use `hasattr()` instead of `getattr()`.
- Change: Append "wrapped_" to marker attribute names.
  • Loading branch information
AnonymouX47 committed Sep 26, 2024
1 parent a697555 commit 9f016d8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/term_image/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,22 @@ def no_redecorate(decor: Callable[P, T]) -> Callable[P, T]:
Args:
decor: The decorator to be wrapped.
Also used to mark decorators for auto documentation.
"""
if getattr(decor, "_no_redecorate_", False):
if hasattr(decor, "_no_redecorate_wrapped_"):
return decor

@wraps(decor)
def no_redecorate_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
if not getattr(args[0], f"_{decor.__name__}_", False):
obj: Any = args[0]
if not hasattr(obj, f"_{decor.__name__}_wrapped_"):
obj = decor(*args, **kwargs)
setattr(obj, f"_{decor.__name__}_", True)
return obj
setattr(obj, f"_{decor.__name__}_wrapped_", ...)

return obj # type: ignore[no-any-return]

setattr(no_redecorate_wrapper, "_no_redecorate_", True)
setattr(no_redecorate_wrapper, "_no_redecorate_wrapped_", ...)

return no_redecorate_wrapper

Expand Down

0 comments on commit 9f016d8

Please sign in to comment.