Skip to content

Commit

Permalink
Set support: pop method (#1772)
Browse files Browse the repository at this point in the history
Add support for the `pop()` method of Python sets to the semantic stage.
Add handling of that method to the Python printer. This fixes  #1749
  • Loading branch information
mustapha-belbiad committed Mar 18, 2024
1 parent e248f8a commit 667b2fc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ All notable changes to this project will be documented in this file.
- #1425 : Add support for `numpy.isnan`, `numpy.isinf` and `numpy.isfinite`.
- #1738 : Add Python support for creating scalar sets with `{}`.
- #1738 : Add Python support for set method `add`.
- #1749 : Add Python support for set method `pop()`

### Fixed

Expand Down
28 changes: 27 additions & 1 deletion pyccel/ast/builtin_methods/set_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pyccel.ast.datatypes import VoidType
from pyccel.ast.internals import PyccelInternalFunction

__all__ = ('SetAdd', 'SetClear', 'SetMethod', 'SetCopy')
__all__ = ('SetAdd', 'SetClear', 'SetMethod', 'SetCopy', 'SetPop')


class SetMethod(PyccelInternalFunction):
Expand Down Expand Up @@ -123,3 +123,29 @@ def __init__(self, set_variable):
self._rank = set_variable._rank
self._class_type = set_variable._class_type
super().__init__(set_variable)


class SetPop(SetMethod):
"""
Represents a call to the .pop() method.
The pop() method pops an element from the set.
It does not take any arguments but returns the popped
element. It raises an error if the set is empty.
The class does not raise an error as it assumes that the
user code is valid.
Parameters
----------
set_variable : TypedAstNode
The name of the set.
"""
__slots__ = ('_class_type',)
_rank = 0
_order = None
_shape = None
name = 'pop'

def __init__(self, set_variable):
self._class_type = set_variable.class_type.element_type
super().__init__(set_variable)
3 changes: 2 additions & 1 deletion pyccel/ast/class_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
This module contains all types which define a python class which is automatically recognised by pyccel
"""

from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear, SetCopy
from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear, SetCopy, SetPop
from pyccel.ast.builtin_methods.list_methods import ListAppend, ListInsert, ListPop, ListClear, ListExtend


Expand Down Expand Up @@ -154,6 +154,7 @@
PyccelFunctionDef('add', func_class = SetAdd ),
PyccelFunctionDef('clear', func_class = SetClear),
PyccelFunctionDef('copy', func_class = SetCopy),
PyccelFunctionDef('pop', func_class = SetPop),
])

#=======================================================================================
Expand Down
30 changes: 30 additions & 0 deletions tests/epyccel/test_epyccel_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,33 @@ def copy_complex():
assert python_result == pyccel_result
assert all(isinstance(elem, type(pyccel_result.pop())) for elem in python_result)

def test_Pop_int(language):
def Pop_int():
se = {2, 4, 9}
se.pop()
return se
epyccel_remove = epyccel(Pop_int, language = language)
pyccel_result = epyccel_remove()
python_result = Pop_int()
assert python_result == pyccel_result

def test_Pop_float(language):
def Pop_float():
se = {2.7, 4.3, 9.2}
se.pop()
return se
epyccel_remove = epyccel(Pop_float, language = language)
pyccel_result = epyccel_remove()
python_result = Pop_float()
assert python_result == pyccel_result

def test_Pop_complex(language):
def Pop_complex():
se = {1j, 3j, 6j}
se.pop()
return se
epyccel_remove = epyccel(Pop_complex, language = language)
pyccel_result = epyccel_remove()
python_result = Pop_complex()
assert python_result == pyccel_result

0 comments on commit 667b2fc

Please sign in to comment.