diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-08-26 07:13:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-26 07:13:19 (GMT) |
commit | c3ea41e9bf100a5396b851488c3efe208e5e2179 (patch) | |
tree | 5e30febe4e132ed33968e96ca81aeb043e33387a /Lib/ast.py | |
parent | 44cd86bbdddb1f7b05deba2c1986a1e98f992429 (diff) | |
download | cpython-c3ea41e9bf100a5396b851488c3efe208e5e2179.zip cpython-c3ea41e9bf100a5396b851488c3efe208e5e2179.tar.gz cpython-c3ea41e9bf100a5396b851488c3efe208e5e2179.tar.bz2 |
bpo-36917: Add default implementation of ast.NodeVisitor.visit_Constant(). (GH-15490)
It emits a deprecation warning and calls corresponding method
visit_Num(), visit_Str(), etc.
Diffstat (limited to 'Lib/ast.py')
-rw-r--r-- | Lib/ast.py | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -360,6 +360,27 @@ class NodeVisitor(object): elif isinstance(value, AST): self.visit(value) + def visit_Constant(self, node): + value = node.value + type_name = _const_node_type_names.get(type(value)) + if type_name is None: + for cls, name in _const_node_type_names.items(): + if isinstance(value, cls): + type_name = name + break + if type_name is not None: + method = 'visit_' + type_name + try: + visitor = getattr(self, method) + except AttributeError: + pass + else: + import warnings + warnings.warn(f"{method} is deprecated; add visit_Constant", + DeprecationWarning, 2) + return visitor(node) + return self.generic_visit(node) + class NodeTransformer(NodeVisitor): """ @@ -487,3 +508,13 @@ _const_types = { _const_types_not = { Num: (bool,), } +_const_node_type_names = { + bool: 'NameConstant', # should be before int + type(None): 'NameConstant', + int: 'Num', + float: 'Num', + complex: 'Num', + str: 'Str', + bytes: 'Bytes', + type(...): 'Ellipsis', +} |