55
66import ast
77import inspect
8- import sys
98import textwrap
109import typing as T
1110from types import ModuleType
1211
1312from .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
2715def 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
3220def 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