Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- General: Drop support for Python 3.6 and 3.7 (thanks to @LecrisUT)
- General: Officially support Python 3.13
- Google: Fix multi-line parameter definitions (thanks to @coolbeevip)
- Attrdoc: Remove use of deprecated ast classes (thanks to @fedepell)
- Attrdoc: Remove code related to Python <= 3.7

# 0.16 (2024-03-15)

Expand Down
18 changes: 3 additions & 15 deletions docstring_parser/attrdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,24 @@

import ast
import inspect
import sys
import textwrap
import typing as T
from types import ModuleType

from .common import Docstring, DocstringParam

ast_constant_attr = {ast.Constant: "value"}

if sys.version_info[:2] <= (3, 7):
ast_constant_attr.update(
{
ast.NameConstant: "value",
ast.Num: "n",
ast.Str: "s",
}
)


def ast_get_constant_value(node: ast.AST) -> T.Any:
"""Return the constant's value if the given node is a constant."""
return getattr(node, ast_constant_attr[node.__class__])
return getattr(node, "value")


def ast_unparse(node: ast.AST) -> T.Optional[str]:
"""Convert the AST node to source code as a string."""
if hasattr(ast, "unparse"):
return ast.unparse(node)
# Support simple cases in Python < 3.9
if isinstance(node, (ast.Constant)):
if isinstance(node, ast.Constant):
return str(ast_get_constant_value(node))
if isinstance(node, ast.Name):
return node.id
Expand All @@ -45,7 +33,7 @@ def ast_is_literal_str(node: ast.AST) -> bool:
"""Return True if the given node is a literal string."""
return (
isinstance(node, ast.Expr)
and isinstance(node.value, (ast.Constant))
and isinstance(node.value, ast.Constant)
and isinstance(ast_get_constant_value(node.value), str)
)

Expand Down
Loading