|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +########################################################################### |
| 3 | +# Copyright (c), The AiiDA team. All rights reserved. # |
| 4 | +# This file is part of the AiiDA code. # |
| 5 | +# # |
| 6 | +# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # |
| 7 | +# For further information on the license, see the LICENSE.txt file # |
| 8 | +# For further information please visit http://www.aiida.net # |
| 9 | +########################################################################### |
| 10 | +"""Transformers for upgrading AiiDA methods.""" |
| 11 | +import libcst as cst |
| 12 | +from libcst import matchers |
| 13 | + |
| 14 | + |
| 15 | +class DictListNoKeywordTransformer(cst.CSTTransformer): |
| 16 | + """Remove ``dict`` and ``list`` keywords from constructors of ``Dict`` and ``List`` nodes.""" |
| 17 | + |
| 18 | + dict_constructor = matchers.Name("Dict") |
| 19 | + list_constructor = matchers.Name("List") |
| 20 | + |
| 21 | + dict_keyword = matchers.Name("dict") |
| 22 | + list_keyword = matchers.Name("list") |
| 23 | + |
| 24 | + def leave_Call(self, original_node: cst.Call, updated_node: cst.Call) -> cst.Call: |
| 25 | + |
| 26 | + if matchers.matches( |
| 27 | + original_node.func, self.dict_constructor | self.list_constructor |
| 28 | + ): |
| 29 | + |
| 30 | + # Empty `Dict` or `List` constructor |
| 31 | + if len(original_node.args) == 0: |
| 32 | + return original_node |
| 33 | + # `Dict` or `List` constructor without keyword |
| 34 | + elif original_node.args[0].keyword is None: |
| 35 | + return original_node |
| 36 | + # `Dict` or `List` constructor with `dict` or `list` keyword |
| 37 | + elif matchers.matches( |
| 38 | + original_node.args[0].keyword, self.dict_keyword | self.list_keyword |
| 39 | + ): |
| 40 | + arguments = list(updated_node.args) |
| 41 | + arguments[0] = updated_node.args[0].with_changes( |
| 42 | + equal=cst.MaybeSentinel.DEFAULT, keyword=None |
| 43 | + ) |
| 44 | + return updated_node.with_changes(args=arguments) |
| 45 | + |
| 46 | + return original_node |
0 commit comments