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

fix: Parse deprecation message from expression arguments #5

Merged
merged 5 commits into from
Dec 10, 2024
Merged
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
12 changes: 9 additions & 3 deletions src/griffe_warnings_deprecated/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from __future__ import annotations

import ast
from typing import Any

from griffe import Class, Docstring, DocstringSectionAdmonition, Extension, Function, get_logger
from griffe import Class, Docstring, DocstringSectionAdmonition, ExprCall, Extension, Function, get_logger

logger = get_logger(__name__)
self_namespace = "griffe_warnings_deprecated"
Expand All @@ -15,8 +16,13 @@

def _deprecated(obj: Class | Function) -> str | None:
for decorator in obj.decorators:
if decorator.callable_path in _decorators:
return str(decorator.value).split("(", 1)[1].rstrip(")").rsplit(",", 1)[0].lstrip("f")[1:-1]
if decorator.callable_path in _decorators and isinstance(decorator.value, ExprCall):
first_arg = decorator.value.arguments[0]
try:
return ast.literal_eval(first_arg) # type: ignore[arg-type]
except ValueError:
logger.debug("%s is not a static string", str(first_arg))
return None
return None


Expand Down
25 changes: 21 additions & 4 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import logging
from textwrap import dedent

import pytest
Expand Down Expand Up @@ -37,10 +38,6 @@ def hello(): ...
def hello(): ...
""",
"""
@warnings.deprecated(f"message", category=DeprecationWarning)
def hello(): ...
""",
"""
@warnings.deprecated("message", category=DeprecationWarning)
def hello():
'''Summary.'''
Expand Down Expand Up @@ -85,3 +82,23 @@ def test_extension(code: str) -> None:
assert adm.title == "Deprecated"
assert adm.value.kind == "danger"
assert adm.value.contents == "message"


def test_extension_fstring(caplog: pytest.LogCaptureFixture) -> None:
"""Test the extension with an f-string as the deprecation message."""
code = dedent(
"""
import warnings
@warnings.deprecated(f"message")
def hello(): ...
""",
)
with (
caplog.at_level(logging.DEBUG),
temporary_visited_module(code, extensions=load_extensions(WarningsDeprecatedExtension)) as module,
):
adm = module["hello"].docstring

# Expect no deprecation message in the docstring.
assert adm is None
assert "f'message' is not a static string" in caplog.records[0].message
Loading