Repository: pyccel/pyccel
Pull request #1772 (2024-02-29 15:15:38)

Set support: `pop` method
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


pyccel-bot[bot]: Hello again! Thank you for this new pull request :star_struck:.

Please begin by requesting your checklist using the command `/bot checklist`

mustapha-belbiad: /bot run coverage docs pylint spelling pyccel_lint
mustapha-belbiad: /bot run coverage docs pylint spelling pyccel_lint
mustapha-belbiad: /bot checklist
EmilyBourne: @mustapha-belbiad I don't think that this PR needs merging after #1765 . These 2 PRs should be independent. Please rebase this PR onto the devel branch so it can be reviewed simultaneously. The command should be something like:
```
git fetch
git rebase --onto origin/devel Set_Support-Remove_Method Set_Support-Pop_Method
```
mustapha-belbiad: /bot run pr_tests

pyccel-bot[bot]: Hey @pyccel/pyccel-dev ! @mustapha-belbiad has just created this great new pull request! Check it out and let me know what you think!

pyccel-bot[bot]: @mustapha-belbiad, @EmilyBourne has a few questions/comments about your code. Can you go through and see if you agree with them. If not go ahead and explain why. Once you've adressed all the comments let me know with `/bot mark as ready` and we will see if we can get approval.

pyccel-bot[bot]: Unfortunately your PR is not passing the tests so it is not quite ready for review yet. Let me know when it is fixed with `/bot mark as ready`.

EmilyBourne: /bot run pr_tests
pyccel-bot[bot]: Unfortunately your PR is not passing the tests so it is not quite ready for review yet. Let me know when it is fixed with `/bot mark as ready`.

EmilyBourne: Error due to slightly too tight tolerance. I am rerunning the test

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2812c9b..02a52a8e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
 
diff --git a/pyccel/ast/builtin_methods/set_methods.py b/pyccel/ast/builtin_methods/set_methods.py
index f7290449..997c1d16 100644
--- a/pyccel/ast/builtin_methods/set_methods.py
+++ b/pyccel/ast/builtin_methods/set_methods.py
@@ -12,7 +12,7 @@ This module contains objects which describe these methods within Pyccel's AST.
 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):
@@ -123,3 +123,29 @@ class SetCopy(SetMethod):
         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)
diff --git a/pyccel/ast/class_defs.py b/pyccel/ast/class_defs.py
index eee85e68..f1c08745 100644
--- a/pyccel/ast/class_defs.py
+++ b/pyccel/ast/class_defs.py
@@ -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
 
 
@@ -154,6 +154,7 @@ SetClass = ClassDef('set',
             PyccelFunctionDef('add', func_class = SetAdd ),
             PyccelFunctionDef('clear', func_class = SetClear),
             PyccelFunctionDef('copy', func_class = SetCopy),
+            PyccelFunctionDef('pop', func_class = SetPop),
         ])
 
 #=======================================================================================


Test patch:
diff --git a/tests/epyccel/test_epyccel_sets.py b/tests/epyccel/test_epyccel_sets.py
index 07d58850..3c3809cb 100644
--- a/tests/epyccel/test_epyccel_sets.py
+++ b/tests/epyccel/test_epyccel_sets.py
@@ -114,3 +114,33 @@ def test_copy_complex(language):
     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
+