diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a5a50d..053bdae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docstring_parser/attrdoc.py b/docstring_parser/attrdoc.py index 170bbd1..de84a85 100644 --- a/docstring_parser/attrdoc.py +++ b/docstring_parser/attrdoc.py @@ -5,28 +5,16 @@ 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]: @@ -34,7 +22,7 @@ def ast_unparse(node: ast.AST) -> T.Optional[str]: 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 @@ -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) )