Skip to content

Commit 1390622

Browse files
Set support: discard method (pyccel#1773)
Add support for the `discard()` method of Python sets to the semantic stage. Add handling of that method to the Python printer. This fixes pyccel#1743
1 parent cb4165a commit 1390622

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
1313
- #1740 : Add Python support for set method `copy()`.
1414
- #1750 : Add Python support for set method `remove()`.
1515
- #1787 : Ensure `STC` is installed with Pyccel.
16+
- #1743 : Add Python support for set method `discard()`.
1617

1718
### Fixed
1819

@@ -67,7 +68,7 @@ All notable changes to this project will be documented in this file.
6768
- #1425 : Add support for `numpy.isnan`, `numpy.isinf` and `numpy.isfinite`.
6869
- #1738 : Add Python support for creating scalar sets with `{}`.
6970
- #1738 : Add Python support for set method `add`.
70-
- #1749 : Add Python support for set method `pop()`
71+
- #1749 : Add Python support for set method `pop()`.
7172

7273
### Fixed
7374

pyccel/ast/builtin_methods/set_methods.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
from pyccel.ast.internals import PyccelInternalFunction
1414
from pyccel.ast.basic import TypedAstNode
1515

16-
__all__ = ('SetAdd', 'SetClear', 'SetMethod', 'SetCopy', 'SetPop', 'SetRemove')
17-
16+
__all__ = (
17+
'SetAdd',
18+
'SetClear',
19+
'SetCopy',
20+
'SetDiscard',
21+
'SetMethod',
22+
'SetPop',
23+
'SetRemove'
24+
)
1825

1926
class SetMethod(PyccelInternalFunction):
2027
"""
@@ -125,6 +132,7 @@ def __init__(self, set_variable):
125132
self._class_type = set_variable._class_type
126133
super().__init__(set_variable)
127134

135+
128136
class SetPop(SetMethod):
129137
"""
130138
Represents a call to the .pop() method.
@@ -150,6 +158,7 @@ def __init__(self, set_variable):
150158
self._class_type = set_variable.class_type.element_type
151159
super().__init__(set_variable)
152160

161+
153162
class SetRemove(SetMethod):
154163
"""
155164
Represents a call to the .remove() method.
@@ -183,3 +192,37 @@ def __init__(self, set_variable, item) -> None:
183192
if not is_homogeneous:
184193
raise TypeError(f"Can't remove an element of type {item.dtype} from a set of {set_variable.dtype}")
185194
super().__init__(set_variable, item)
195+
196+
197+
class SetDiscard(SetMethod):
198+
"""
199+
Represents a call to the .discard() method.
200+
201+
The discard() is a built-in method to remove elements from the set.
202+
The discard() method takes exactly one argument.
203+
This method does not return any value.
204+
205+
Parameters
206+
----------
207+
set_variable : TypedAstNode
208+
The name of the set.
209+
210+
item : TypedAstNode
211+
The item to search for, and remove.
212+
"""
213+
__slots__ = ()
214+
_shape = None
215+
_order = None
216+
_rank = 0
217+
_class_type = VoidType()
218+
name = 'discard'
219+
220+
def __init__(self, set_variable, item) -> None:
221+
expected_type = set_variable.class_type.element_type
222+
is_homogeneous = (
223+
expected_type == item.class_type and
224+
set_variable.rank - 1 == item.rank
225+
)
226+
if not is_homogeneous:
227+
raise TypeError("Expecting an argument of the same type as the elements of the set")
228+
super().__init__(set_variable, item)

pyccel/ast/class_defs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This module contains all types which define a python class which is automatically recognised by pyccel
77
"""
88

9-
from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear, SetCopy, SetPop, SetRemove
9+
from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear, SetCopy, SetPop, SetRemove, SetDiscard
1010
from pyccel.ast.builtin_methods.list_methods import (ListAppend, ListInsert, ListPop,
1111
ListClear, ListExtend, ListRemove,
1212
ListCopy)
@@ -157,6 +157,7 @@
157157
PyccelFunctionDef('add', func_class = SetAdd ),
158158
PyccelFunctionDef('clear', func_class = SetClear),
159159
PyccelFunctionDef('copy', func_class = SetCopy),
160+
PyccelFunctionDef('discard', func_class = SetDiscard),
160161
PyccelFunctionDef('pop', func_class = SetPop),
161162
PyccelFunctionDef('remove', func_class = SetRemove),
162163
])

tests/epyccel/test_epyccel_sets.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,32 @@ def remove_float():
174174
python_result = remove_float()
175175
assert python_result == pyccel_result
176176

177+
def test_Discard_int(language):
178+
def Discard_int():
179+
se = {2.7, 4.3, 9.2}
180+
se.discard(4.3)
181+
return se
182+
epyccel_remove = epyccel(Discard_int, language = language)
183+
pyccel_result = epyccel_remove()
184+
python_result = Discard_int()
185+
assert python_result == pyccel_result
186+
187+
def test_Discard_complex(language):
188+
def Discard_complex():
189+
se = {2j, 5j, 3j, 7j}
190+
se.discard(5j)
191+
return se
192+
epyccel_remove = epyccel(Discard_complex, language = language)
193+
pyccel_result = epyccel_remove()
194+
python_result = Discard_complex()
195+
assert python_result == pyccel_result
196+
197+
def test_Discard_wrong_arg(language):
198+
def Discard_wrong_arg():
199+
se = {4.7, 1.3, 8.2}
200+
se.discard(8.6)
201+
return se
202+
epyccel_remove = epyccel(Discard_wrong_arg, language = language)
203+
pyccel_result = epyccel_remove()
204+
python_result = Discard_wrong_arg()
205+
assert python_result == pyccel_result
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# pylint: disable=missing-function-docstring, missing-module-docstring
2+
3+
a = {7, 9, 1}
4+
b = 2j
5+
a.discard(b)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# pylint: disable=missing-function-docstring, missing-module-docstring
2+
3+
a = {5.3, 9.2, 1.7}
4+
b = 3
5+
a.discard(b)

0 commit comments

Comments
 (0)