-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #676 from DanielYang59/deprecation-msg
Extend `dev.deprecated` to decorate classes and improve message
- Loading branch information
Showing
2 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from __future__ import annotations | ||
|
||
import functools | ||
import inspect | ||
import logging | ||
import os | ||
import subprocess | ||
|
@@ -64,9 +65,9 @@ def _is_in_owner_repo() -> bool: | |
owner_repo = ( | ||
result.stdout.decode("utf-8") | ||
.strip() | ||
.lstrip("https://github.com/") # https clone | ||
.lstrip("[email protected]:") # ssh clone | ||
.rstrip(".git") # ssh clone | ||
.lstrip("https://github.com/") # HTTPS clone | ||
.lstrip("[email protected]:") # SSH clone | ||
.rstrip(".git") # SSH clone | ||
) | ||
|
||
return owner_repo == os.getenv("GITHUB_REPOSITORY") | ||
|
@@ -103,27 +104,53 @@ def craft_message( | |
r = replacement.__func__ | ||
else: | ||
r = replacement | ||
msg += f"; use {r.__name__} in {r.__module__} instead." | ||
|
||
if deadline is None: | ||
msg += "; use " # for better formatting | ||
else: | ||
msg += "Use " | ||
msg += f"{r.__name__} in {r.__module__} instead." | ||
|
||
if message: | ||
msg += "\n" + message | ||
return msg | ||
|
||
def deprecated_decorator(old: Callable) -> Callable: | ||
def deprecated_function_decorator(old: Callable) -> Callable: | ||
def wrapped(*args, **kwargs): | ||
msg = craft_message(old, replacement, message, _deadline) | ||
warnings.warn(msg, category=category, stacklevel=2) | ||
return old(*args, **kwargs) | ||
|
||
return wrapped | ||
|
||
def deprecated_class_decorator(cls: Type) -> Type: | ||
original_init = cls.__init__ | ||
|
||
def new_init(self, *args, **kwargs): | ||
msg = craft_message(cls, replacement, message, _deadline) | ||
warnings.warn(msg, category=category, stacklevel=2) | ||
original_init(self, *args, **kwargs) | ||
|
||
cls.__init__ = new_init | ||
return cls | ||
|
||
# Convert deadline to datetime type | ||
_deadline = datetime(*deadline) if deadline is not None else None | ||
|
||
# Raise CI warning after removal deadline | ||
raise_deadline_warning() | ||
|
||
return deprecated_decorator | ||
def decorator(target: Callable) -> Callable: | ||
if inspect.isfunction(target): | ||
return deprecated_function_decorator(target) | ||
elif inspect.isclass(target): | ||
return deprecated_class_decorator(target) | ||
else: | ||
raise TypeError( | ||
"The @deprecated decorator can only be applied to classes or functions" | ||
) | ||
|
||
return decorator | ||
|
||
|
||
class requires: | ||
|
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