Skip to content

Commit 667b2fc

Browse files
Set support: pop method (pyccel#1772)
Add support for the `pop()` method of Python sets to the semantic stage. Add handling of that method to the Python printer. This fixes pyccel#1749
1 parent e248f8a commit 667b2fc

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ All notable changes to this project will be documented in this file.
5252
- #1425 : Add support for `numpy.isnan`, `numpy.isinf` and `numpy.isfinite`.
5353
- #1738 : Add Python support for creating scalar sets with `{}`.
5454
- #1738 : Add Python support for set method `add`.
55+
- #1749 : Add Python support for set method `pop()`
5556

5657
### Fixed
5758

pyccel/ast/builtin_methods/set_methods.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pyccel.ast.datatypes import VoidType
1313
from pyccel.ast.internals import PyccelInternalFunction
1414

15-
__all__ = ('SetAdd', 'SetClear', 'SetMethod', 'SetCopy')
15+
__all__ = ('SetAdd', 'SetClear', 'SetMethod', 'SetCopy', 'SetPop')
1616

1717

1818
class SetMethod(PyccelInternalFunction):
@@ -123,3 +123,29 @@ def __init__(self, set_variable):
123123
self._rank = set_variable._rank
124124
self._class_type = set_variable._class_type
125125
super().__init__(set_variable)
126+
127+
128+
class SetPop(SetMethod):
129+
"""
130+
Represents a call to the .pop() method.
131+
132+
The pop() method pops an element from the set.
133+
It does not take any arguments but returns the popped
134+
element. It raises an error if the set is empty.
135+
The class does not raise an error as it assumes that the
136+
user code is valid.
137+
138+
Parameters
139+
----------
140+
set_variable : TypedAstNode
141+
The name of the set.
142+
"""
143+
__slots__ = ('_class_type',)
144+
_rank = 0
145+
_order = None
146+
_shape = None
147+
name = 'pop'
148+
149+
def __init__(self, set_variable):
150+
self._class_type = set_variable.class_type.element_type
151+
super().__init__(set_variable)

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
9+
from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear, SetCopy, SetPop
1010
from pyccel.ast.builtin_methods.list_methods import ListAppend, ListInsert, ListPop, ListClear, ListExtend
1111

1212

@@ -154,6 +154,7 @@
154154
PyccelFunctionDef('add', func_class = SetAdd ),
155155
PyccelFunctionDef('clear', func_class = SetClear),
156156
PyccelFunctionDef('copy', func_class = SetCopy),
157+
PyccelFunctionDef('pop', func_class = SetPop),
157158
])
158159

159160
#=======================================================================================

tests/epyccel/test_epyccel_sets.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,33 @@ def copy_complex():
114114
assert python_result == pyccel_result
115115
assert all(isinstance(elem, type(pyccel_result.pop())) for elem in python_result)
116116

117+
def test_Pop_int(language):
118+
def Pop_int():
119+
se = {2, 4, 9}
120+
se.pop()
121+
return se
122+
epyccel_remove = epyccel(Pop_int, language = language)
123+
pyccel_result = epyccel_remove()
124+
python_result = Pop_int()
125+
assert python_result == pyccel_result
126+
127+
def test_Pop_float(language):
128+
def Pop_float():
129+
se = {2.7, 4.3, 9.2}
130+
se.pop()
131+
return se
132+
epyccel_remove = epyccel(Pop_float, language = language)
133+
pyccel_result = epyccel_remove()
134+
python_result = Pop_float()
135+
assert python_result == pyccel_result
136+
137+
def test_Pop_complex(language):
138+
def Pop_complex():
139+
se = {1j, 3j, 6j}
140+
se.pop()
141+
return se
142+
epyccel_remove = epyccel(Pop_complex, language = language)
143+
pyccel_result = epyccel_remove()
144+
python_result = Pop_complex()
145+
assert python_result == pyccel_result
146+

0 commit comments

Comments
 (0)