-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ability to create alias method names
- Loading branch information
1 parent
8f7b030
commit 3a18ee6
Showing
7 changed files
with
157 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import functools | ||
from typing import Any, Callable, Dict, Optional, Set | ||
import warnings | ||
|
||
|
||
class FunctionWrapper: | ||
"""Function wrapper""" | ||
|
||
def __init__(self, func: Callable[..., Any]) -> None: | ||
self.func = func | ||
self._aliases: Set[str] = set() | ||
|
||
|
||
class alias(object): | ||
"""Add an alias to a function""" | ||
|
||
def __init__(self, *aliases: str, deprecated_version: str = None) -> None: | ||
"""Constructor | ||
Args: | ||
deprecated_version (str, optional): Version number that deprecation will happen. Defaults to None. | ||
""" | ||
self.aliases: Set[str] = set(aliases) | ||
self.deprecated_version: Optional[str] = deprecated_version | ||
|
||
def __call__(self, f: Callable[..., Any]) -> FunctionWrapper: | ||
"""call""" | ||
wrapped_func = FunctionWrapper(f) | ||
wrapped_func._aliases = self.aliases | ||
|
||
@functools.wraps(f) | ||
def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
"""Alias wrapper""" | ||
if self.deprecated_version: | ||
aliases_str = ", ".join(self.aliases) | ||
msg = f"{aliases_str} is deprecated and will be removed in version {self.deprecated_version}. Use {f.__name__} instead." | ||
warnings.warn(msg, DeprecationWarning) | ||
return f(*args, **kwargs) | ||
|
||
wrapped_func.func = wrapper # Assign wrapper directly to func attribute | ||
return wrapped_func | ||
|
||
|
||
def aliased(aliased_class: Any) -> Any: | ||
"""Class has aliases""" | ||
original_methods: Dict[str, Any] = aliased_class.__dict__.copy() | ||
for name, method in original_methods.items(): | ||
if isinstance(method, FunctionWrapper) and hasattr(method, "_aliases"): | ||
for alias in method._aliases: | ||
setattr(aliased_class, alias, method.func) | ||
|
||
# Also replace the original method with the wrapped function | ||
setattr(aliased_class, name, method.func) | ||
|
||
return aliased_class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pyarr" | ||
version = "5.1.2" | ||
version = "5.2.0" | ||
description = "Synchronous Sonarr, Radarr, Lidarr and Readarr API's for Python" | ||
authors = ["Steven Marks <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters