Skip to content

Commit 21d5832

Browse files
committed
refactor: Simplify AST imports, stop using deprecated code from ast
Issue #179: #179
1 parent 48a7162 commit 21d5832

6 files changed

Lines changed: 279 additions & 434 deletions

File tree

src/griffe/agents/nodes/_all.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22

33
from __future__ import annotations
44

5-
from ast import AST
6-
from ast import Assign as NodeAssign
7-
from ast import AugAssign as NodeAugAssign
8-
from ast import BinOp as NodeBinOp
9-
from ast import Constant as NodeConstant
10-
from ast import List as NodeList
11-
from ast import Name as NodeName
12-
from ast import Set as NodeSet
13-
from ast import Starred as NodeStarred
14-
from ast import Tuple as NodeTuple
5+
import ast
156
from contextlib import suppress
167
from functools import partial
178
from typing import TYPE_CHECKING, Any, Callable
@@ -27,47 +18,47 @@
2718
logger = get_logger(__name__)
2819

2920

30-
def _extract_constant(node: NodeConstant, parent: Module) -> list[str | Name]:
21+
def _extract_constant(node: ast.Constant, parent: Module) -> list[str | Name]:
3122
return [node.value]
3223

3324

34-
def _extract_name(node: NodeName, parent: Module) -> list[str | Name]:
25+
def _extract_name(node: ast.Name, parent: Module) -> list[str | Name]:
3526
return [Name(node.id, partial(parent.resolve, node.id))]
3627

3728

38-
def _extract_starred(node: NodeStarred, parent: Module) -> list[str | Name]:
29+
def _extract_starred(node: ast.Starred, parent: Module) -> list[str | Name]:
3930
return _extract(node.value, parent)
4031

4132

42-
def _extract_sequence(node: NodeList | NodeSet | NodeTuple, parent: Module) -> list[str | Name]:
33+
def _extract_sequence(node: ast.List | ast.Set | ast.Tuple, parent: Module) -> list[str | Name]:
4334
sequence = []
4435
for elt in node.elts:
4536
sequence.extend(_extract(elt, parent))
4637
return sequence
4738

4839

49-
def _extract_binop(node: NodeBinOp, parent: Module) -> list[str | Name]:
40+
def _extract_binop(node: ast.BinOp, parent: Module) -> list[str | Name]:
5041
left = _extract(node.left, parent)
5142
right = _extract(node.right, parent)
5243
return left + right
5344

5445

5546
_node_map: dict[type, Callable[[Any, Module], list[str | Name]]] = {
56-
NodeConstant: _extract_constant,
57-
NodeName: _extract_name,
58-
NodeStarred: _extract_starred,
59-
NodeList: _extract_sequence,
60-
NodeSet: _extract_sequence,
61-
NodeTuple: _extract_sequence,
62-
NodeBinOp: _extract_binop,
47+
ast.Constant: _extract_constant,
48+
ast.Name: _extract_name,
49+
ast.Starred: _extract_starred,
50+
ast.List: _extract_sequence,
51+
ast.Set: _extract_sequence,
52+
ast.Tuple: _extract_sequence,
53+
ast.BinOp: _extract_binop,
6354
}
6455

6556

66-
def _extract(node: AST, parent: Module) -> list[str | Name]:
57+
def _extract(node: ast.AST, parent: Module) -> list[str | Name]:
6758
return _node_map[type(node)](node, parent)
6859

6960

70-
def get__all__(node: NodeAssign | NodeAugAssign, parent: Module) -> list[str | Name]:
61+
def get__all__(node: ast.Assign | ast.AugAssign, parent: Module) -> list[str | Name]:
7162
"""Get the values declared in `__all__`.
7263
7364
Parameters:
@@ -83,7 +74,7 @@ def get__all__(node: NodeAssign | NodeAugAssign, parent: Module) -> list[str | N
8374

8475

8576
def safe_get__all__(
86-
node: NodeAssign | NodeAugAssign,
77+
node: ast.Assign | ast.AugAssign,
8778
parent: Module,
8879
log_level: LogLevel = LogLevel.debug, # TODO: set to error when we handle more things
8980
) -> list[str | Name]:

src/griffe/agents/nodes/_docstrings.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
from __future__ import annotations
44

5-
from ast import AST
6-
from ast import Constant as NodeConstant
7-
from ast import Expr as NodeExpr
8-
from ast import Str as NodeStr
5+
import ast
96

107
from griffe.logger import get_logger
118

129
logger = get_logger(__name__)
1310

1411

1512
def get_docstring(
16-
node: AST,
13+
node: ast.AST,
1714
*,
1815
strict: bool = False,
1916
) -> tuple[str | None, int | None, int | None]:
@@ -27,17 +24,14 @@ def get_docstring(
2724
A tuple with the value and line numbers of the docstring.
2825
"""
2926
# TODO: possible optimization using a type map
30-
if isinstance(node, NodeExpr):
27+
if isinstance(node, ast.Expr):
3128
doc = node.value
32-
elif node.body and isinstance(node.body[0], NodeExpr) and not strict: # type: ignore[attr-defined]
29+
elif node.body and isinstance(node.body[0], ast.Expr) and not strict: # type: ignore[attr-defined]
3330
doc = node.body[0].value # type: ignore[attr-defined]
3431
else:
3532
return None, None, None
36-
if isinstance(doc, NodeConstant) and isinstance(doc.value, str):
33+
if isinstance(doc, ast.Constant) and isinstance(doc.value, str):
3734
return doc.value, doc.lineno, doc.end_lineno
38-
if isinstance(doc, NodeStr):
39-
lineno = doc.lineno
40-
return doc.s, lineno, doc.end_lineno
4135
return None, None, None
4236

4337

0 commit comments

Comments
 (0)