Skip to content

Commit 86f8cb5

Browse files
committed
Fix sageinspect for Python 3.14: Add visit_Constant method to SageArgSpecVisitor
In Python 3.14, the AST representation was fully unified to use ast.Constant nodes for all literal values (numbers, strings, booleans, None). Previously, Python 3.8-3.13 still generated legacy node types (ast.Num, ast.Str, ast.NameConstant) for backward compatibility. The SageArgSpecVisitor class had visit methods for the legacy node types but was missing visit_Constant(), causing it to return None for all constant default argument values when parsing function signatures on Python 3.14. This fix adds the visit_Constant() method to properly handle the unified AST representation, allowing correct extraction of default argument values like base=0 in Integer.__init__(self, x=None, base=0). Fixes: #<issue_number> (if applicable)
1 parent 4e2319b commit 86f8cb5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/sage/misc/sageinspect.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,32 @@ def visit_UnaryOp(self, node):
820820
if op == 'USub':
821821
return -self.visit(node.operand)
822822

823+
def visit_Constant(self, node):
824+
"""
825+
Visit a Python AST :class:`ast.Constant` node.
826+
827+
Starting from Python 3.8, constants like numbers, strings, booleans,
828+
None, and ellipsis are all represented as Constant nodes instead of
829+
separate node types (Num, Str, NameConstant, etc.).
830+
831+
INPUT:
832+
833+
- ``node`` -- the node instance to visit
834+
835+
OUTPUT: the constant value the ``node`` represents
836+
837+
EXAMPLES::
838+
839+
sage: import ast, sage.misc.sageinspect as sms
840+
sage: visitor = sms.SageArgSpecVisitor()
841+
sage: vis = lambda x: visitor.visit_Constant(ast.parse(x).body[0].value)
842+
sage: [vis(n) for n in ['123', '0', '3.14', '"hello"', 'True', 'False', 'None']]
843+
[123, 0, 3.14, 'hello', True, False, None]
844+
sage: [type(vis(n)) for n in ['123', '0', '3.14', '"hello"', 'True', 'False', 'None']]
845+
[<class 'int'>, <class 'int'>, <class 'float'>, <class 'str'>, <class 'bool'>, <class 'bool'>, <class 'NoneType'>]
846+
"""
847+
return node.value
848+
823849

824850
def _grep_first_pair_of_parentheses(s):
825851
r"""

0 commit comments

Comments
 (0)