Skip to content
Merged
Changes from 2 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
28 changes: 27 additions & 1 deletion src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class SageArgSpecVisitor(ast.NodeVisitor):
sage: sorted(v.items(), key=lambda x: str(x[0]))
[(37.0, 'temp'), ('a', ('e', 2, [None, ({False: True}, 'pi')]))]
sage: v = ast.parse("jc = ['veni', 'vidi', 'vici']").body[0]; v
<...ast.Assign object at ...>
...Assign...
sage: attrs = [x for x in dir(v) if not x.startswith('__')]
sage: '_attributes' in attrs and '_fields' in attrs and 'col_offset' in attrs
True
Expand Down Expand Up @@ -820,6 +820,32 @@ def visit_UnaryOp(self, node):
if op == 'USub':
return -self.visit(node.operand)

def visit_Constant(self, node):
"""
Visit a Python AST :class:`ast.Constant` node.

Starting from Python 3.8, constants like numbers, strings, booleans,
None, and ellipsis are all represented as Constant nodes instead of
separate node types (Num, Str, NameConstant, etc.).

INPUT:

- ``node`` -- the node instance to visit

OUTPUT: the constant value the ``node`` represents

EXAMPLES::

sage: import ast, sage.misc.sageinspect as sms
sage: visitor = sms.SageArgSpecVisitor()
sage: vis = lambda x: visitor.visit_Constant(ast.parse(x).body[0].value)
sage: [vis(n) for n in ['123', '0', '3.14', '"hello"', 'True', 'False', 'None']]
[123, 0, 3.14, 'hello', True, False, None]
sage: [type(vis(n)) for n in ['123', '0', '3.14', '"hello"', 'True', 'False', 'None']]
[<class 'int'>, <class 'int'>, <class 'float'>, <class 'str'>, <class 'bool'>, <class 'bool'>, <class 'NoneType'>]
"""
return node.value


def _grep_first_pair_of_parentheses(s):
r"""
Expand Down
Loading