Skip to content

Commit 2d3b178

Browse files
committed
More changes related to AST deprecations and removal of code for Python <= 3.7
1 parent b64ad17 commit 2d3b178

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- General: Drop support for Python 3.6 and 3.7 (thanks to @LecrisUT)
55
- General: Officially support Python 3.13
66
- Google: Fix multi-line parameter definitions (thanks to @coolbeevip)
7+
- Attrdoc: Remove use of deprecated ast classes (thanks to @fedepell)
8+
- Attrdoc: Remove code related to Python <= 3.7
79

810
# 0.16 (2024-03-15)
911

docstring_parser/attrdoc.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,24 @@
55

66
import ast
77
import inspect
8-
import sys
98
import textwrap
109
import typing as T
1110
from types import ModuleType
1211

1312
from .common import Docstring, DocstringParam
1413

15-
ast_constant_attr = {ast.Constant: "value"}
16-
17-
if sys.version_info[:2] <= (3, 7):
18-
ast_constant_attr.update(
19-
{
20-
ast.NameConstant: "value",
21-
ast.Num: "n",
22-
ast.Str: "s",
23-
}
24-
)
25-
2614

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

3119

3220
def ast_unparse(node: ast.AST) -> T.Optional[str]:
3321
"""Convert the AST node to source code as a string."""
3422
if hasattr(ast, "unparse"):
3523
return ast.unparse(node)
3624
# Support simple cases in Python < 3.9
37-
if isinstance(node, (ast.Constant)):
25+
if isinstance(node, ast.Constant):
3826
return str(ast_get_constant_value(node))
3927
if isinstance(node, ast.Name):
4028
return node.id
@@ -45,7 +33,7 @@ def ast_is_literal_str(node: ast.AST) -> bool:
4533
"""Return True if the given node is a literal string."""
4634
return (
4735
isinstance(node, ast.Expr)
48-
and isinstance(node.value, (ast.Constant))
36+
and isinstance(node.value, ast.Constant)
4937
and isinstance(ast_get_constant_value(node.value), str)
5038
)
5139

0 commit comments

Comments
 (0)