-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ NEW: Transformer for removing dict and list keywords
We recently adapted the constructors of the `Dict` and `List` nodes so they no longer require the `dict` and `list` keywords, see: aiidateam/aiida-core#5165 Here we add a transformer that automatically removes these keywords from `Dict` and `List` constructors if present.
- Loading branch information
Showing
6 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: ci | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
|
||
tests: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
python-version: ['3.8', '3.9', '3.10'] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Cache Python dependencies | ||
uses: actions/cache@v1 | ||
with: | ||
path: ~/.cache/pip | ||
key: pip-${{ matrix.python-version }}-tests-${{ hashFiles('**/setup.json') }} | ||
restore-keys: | ||
pip-${{ matrix.python-version }}-tests | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install Python dependencies | ||
run: | | ||
pip install --upgrade pip setuptools wheel | ||
pip install -e .[tests] | ||
- name: Run pytest | ||
run: pytest -sv tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Transformers for upgrading AiiDA methods.""" | ||
import libcst as cst | ||
from libcst import matchers | ||
|
||
|
||
class DictListNoKeywordTransformer(cst.CSTTransformer): | ||
"""Remove ``dict`` and ``list`` keywords from constructors of ``Dict`` and ``List`` nodes.""" | ||
|
||
dict_constructor = matchers.Name("Dict") | ||
list_constructor = matchers.Name("List") | ||
|
||
dict_keyword = matchers.Name("dict") | ||
list_keyword = matchers.Name("list") | ||
|
||
def leave_Call(self, original_node: cst.Call, updated_node: cst.Call) -> cst.Call: | ||
|
||
if matchers.matches( | ||
original_node.func, self.dict_constructor | self.list_constructor | ||
): | ||
|
||
# Empty `Dict` or `List` constructor | ||
if len(original_node.args) == 0: | ||
return original_node | ||
# `Dict` or `List` constructor without keyword | ||
elif original_node.args[0].keyword is None: | ||
return original_node | ||
# `Dict` or `List` constructor with `dict` or `list` keyword | ||
elif matchers.matches( | ||
original_node.args[0].keyword, self.dict_keyword | self.list_keyword | ||
): | ||
arguments = list(updated_node.args) | ||
arguments[0] = updated_node.args[0].with_changes( | ||
equal=cst.MaybeSentinel.DEFAULT, keyword=None | ||
) | ||
return updated_node.with_changes(args=arguments) | ||
|
||
return original_node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Tests for the transformers for upgrading AiiDA entry points.""" | ||
import libcst as cst | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("expression", "result"), | ||
( | ||
("Dict()", "Dict()"), | ||
("Dict(dict={'a': 1})", "Dict({'a': 1})"), | ||
("Dict(value={'a': 1})", "Dict(value={'a': 1})"), | ||
("List(list=[1, 2, 3])", "List([1, 2, 3])"), | ||
("List(value=[1, 2, 3])", "List(value=[1, 2, 3])"), | ||
), | ||
) | ||
def test_dict_list_no_keyword(expression, result): | ||
"""Test the ``DictListNoKeywordTransformer`` class.""" | ||
from aiida_upgrade.methods import DictListNoKeywordTransformer | ||
|
||
cst_tree = cst.parse_module(expression) | ||
assert cst_tree.visit(DictListNoKeywordTransformer()).code == result |