Skip to content

Commit d8c8d98

Browse files
[DICT] Add Python support for clear (pyccel#2029)
Files dict_methods.py, class_defs.py, test_epyccel_dicts.py and pycode.py were changed. Reference to issue pyccel#1880. Closes pyccel#1880 --------- Co-authored-by: Emily Bourne <[email protected]>
1 parent 86f761d commit d8c8d98

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ Contributors
3737
* Emmmanuel Ferdman
3838
* Mohammed Ayaz Shaikh
3939
* Amir Movassaghi
40+
* Leonardo Merlin Paloschi

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
### Added
77

8+
- #1880 : Add Python support for dict method `clear()`
89
- #1720 : Add support for `Ellipsis` as the only index for an array.
910
- #1787 : Ensure STC is installed with Pyccel.
1011
- #1656 : Ensure gFTL is installed with Pyccel.

docs/builtin-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
120120

121121
| Method | Supported |
122122
|----------|-----------|
123-
| `clear` | No |
123+
| `clear` | Python-only |
124124
| `copy` | No |
125125
| `get` | Python-only |
126126
| `items` | No |

pyccel/ast/builtin_methods/dict_methods.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
This module contains objects which describe these methods within Pyccel's AST.
1111
"""
1212

13-
from pyccel.ast.datatypes import InhomogeneousTupleType
13+
from pyccel.ast.datatypes import InhomogeneousTupleType, VoidType
1414
from pyccel.ast.internals import PyccelFunction
1515

16-
__all__ = ('DictGet',
16+
17+
__all__ = ('DictClear',
18+
'DictGet',
1719
'DictMethod',
1820
'DictPop',
1921
'DictPopitem',
@@ -178,7 +180,6 @@ def default_value(self):
178180
return self._args[1]
179181

180182
#==============================================================================
181-
182183
class DictSetDefault(DictMethod):
183184
"""
184185
Represents a call to the .setdefault() method.
@@ -236,3 +237,23 @@ def default_value(self):
236237
The value that should be returned if the key is not present in the dictionary.
237238
"""
238239
return self._args[1]
240+
241+
#==============================================================================
242+
class DictClear(DictMethod) :
243+
"""
244+
Represents a call to the .clear() method.
245+
246+
The clear() method removes all items from the dictionary.
247+
248+
Parameters
249+
----------
250+
dict_obj : TypedAstNode
251+
The object from which the method is called.
252+
"""
253+
__slots__ = ()
254+
_shape = None
255+
_class_type = VoidType()
256+
name = 'clear'
257+
258+
def __init__(self, dict_obj):
259+
super().__init__(dict_obj)

pyccel/ast/class_defs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pyccel.ast.builtin_methods.list_methods import (ListAppend, ListInsert, ListPop,
1212
ListClear, ListExtend, ListRemove,
1313
ListCopy, ListSort)
14-
from pyccel.ast.builtin_methods.dict_methods import( DictPop, DictPopitem, DictGet,
14+
from pyccel.ast.builtin_methods.dict_methods import (DictPop, DictPopitem, DictGet, DictClear,
1515
DictSetDefault)
1616

1717
from .builtins import PythonImag, PythonReal, PythonConjugate
@@ -175,6 +175,7 @@
175175

176176
DictClass = ClassDef('dict',
177177
methods=[
178+
PyccelFunctionDef('clear', func_class = DictClear),
178179
PyccelFunctionDef('get', func_class = DictGet),
179180
PyccelFunctionDef('pop', func_class = DictPop),
180181
PyccelFunctionDef('popitem', func_class = DictPopitem),

tests/epyccel/test_epyccel_dicts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,14 @@ def dict_ptr():
217217
python_result = dict_ptr()
218218
assert isinstance(python_result, type(pyccel_result))
219219
assert python_result == pyccel_result
220+
221+
def test_dict_clear(python_only_language):
222+
def dict_clear():
223+
a = {1:1.0, 2:2.0}
224+
a.clear()
225+
return a
226+
epyc_dict_clear = epyccel(dict_clear, language = python_only_language)
227+
pyccel_result = epyc_dict_clear()
228+
python_result = dict_clear()
229+
assert python_result == pyccel_result
230+

0 commit comments

Comments
 (0)