Skip to content
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

Add an optional arg deadline to dev.deprecated to raise warning after deadline #631

Merged
merged 22 commits into from
Mar 23, 2024
Merged
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
55 changes: 45 additions & 10 deletions monty/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import os
import sys
import subprocess
import warnings
from datetime import datetime
from typing import Callable, Optional, Type
Expand All @@ -28,7 +29,7 @@ def deprecated(
message (str): A warning message to be displayed.
deadline (Optional[tuple[int, int, int]]): Optional deadline for removal
of the old function/class, in format (yyyy, MM, dd). A CI warning would
be raised after this date.
be raised after this date if is running in code owner' repo.
category (Warning): Choose the category of the warning to issue. Defaults
to FutureWarning. Another choice can be DeprecationWarning. Note that
FutureWarning is meant for end users and is always shown unless silenced.
Expand All @@ -42,8 +43,47 @@ def deprecated(

def _convert_date(date: tuple[int, int, int]) -> datetime:
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
"""Convert date as int tuple (yyyy, MM, dd) to datetime type."""

return datetime(*date)

def deadline_warning() -> None:
"""Raise CI warning after removal deadline in code owner's repo."""

def _is_in_owner_repo() -> bool:
"""Check if is running in code owner's repo.
Only generate reliable check when `git` is installed and remote name
is "origin".
"""

try:
# Get current running repo
result = subprocess.run(
["git", "config", "--get", "remote.origin.url"],
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
stdout=subprocess.PIPE,
)
owner_repo = (
result.stdout.decode("utf-8")
.strip()
.lstrip("[email protected]:")
.rstrip(".git")
)

return owner_repo == os.getenv("GITHUB_REPOSITORY")

except (subprocess.CalledProcessError, FileNotFoundError):
return False

# Only raise warning in code owner's repo CI
if (
_deadline is not None
and os.getenv("CI")
and datetime.now() > _deadline
and _is_in_owner_repo()
):
raise DeprecationWarning(
"This function should have been removed on {deadline:%Y-%m-%d}."
)

def craft_message(
old: Callable,
replacement: Callable,
Expand Down Expand Up @@ -76,16 +116,11 @@ def wrapped(*args, **kwargs):

return wrapped

# Raise a CI warning after removal deadline
if deadline is None:
_deadline = None
# Convert deadline to datetime
_deadline = _convert_date(deadline) if deadline is not None else None

else:
_deadline = _convert_date(deadline)
if os.getenv("CI") and datetime.now() > _deadline:
raise DeprecationWarning(
"This function should have been removed on {deadline:%Y-%m-%d}."
)
# Raise a CI warning after removal deadline
deadline_warning()

return deprecated_decorator

Expand Down