Repository: pyccel/pyccel
Pull request #2256 (2025-03-24 15:02:47)

Add `C` and `Fortran` support for the `list.clear()` method.
This PR adds `C` and `Fortran` printing for the `list.clear()` method and activates relevant tests.

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`

jalalium: /bot mark-as-ready

pyccel-bot[bot]: This bot reacts to all comments which begin with `/bot`. This phrase can be followed by any of these commands:
- `show tests` : Lists the tests which can be triggered
- `run X` : Triggers the test X (acceptable values for X can be seen using `show tests`). Multiple tests can be specified separated by spaces.
- `try V X` : Triggers the test X (acceptable values for X can be seen using `show tests`) using Python version V. Multiple tests can be specified separated by spaces, but all will use the same Python version.
- `mark as ready` : Runs the PR tests. If they pass then it adds the appropriate review flag and requests reviews. This command should be used when the PR is first ready for review, or when a review has been answered.
- `commands` : Shows this list detailing all the commands which are understood.
- `trust user X` : Tells the bot that a new user X is trusted to run workflows (prevents misuse of GitHub actions for mining etc). This command can only be used by a trusted reviewer.

Beware: if you have never contributed to this repository and you are not a member of the Pyccel organisation, the bot will ignore all requests to run tests until permitted by a trusted reviewer.

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

EmilyBourne: Documentation is also missing
EmilyBourne: /bot run pr_tests

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 64a228eb..96fadd1f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,6 +33,7 @@ All notable changes to this project will be documented in this file.
 -   #1876 : Add C support for indexing lists.
 -   #1690 : Add C and Fortran support for list method `pop()`.
 -   #1695 : Add C and Fortran support for list method `reverse()`.
+-   #2256 : Add C and Fortran support for list method `clear()`.
 -   #1663 : Add C and Fortran support for sets as arguments.
 -   #1664 : Add C and Fortran support for returning sets from functions.
 -   #2023 : Add support for iterating over a `set`.
diff --git a/docs/builtin-functions.md b/docs/builtin-functions.md
index 64aa4341..9d65a8ba 100644
--- a/docs/builtin-functions.md
+++ b/docs/builtin-functions.md
@@ -79,7 +79,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
 | Method | Supported |
 |----------|-----------|
 | **`append`** | **Yes** |
-| `clear` | Python-only |
+| **`clear`** | **Yes** |
 | `copy` | Python-only |
 | `count` | No |
 | `extend` | Python-only |
diff --git a/pyccel/codegen/printing/ccode.py b/pyccel/codegen/printing/ccode.py
index 7e1288a4..5e6f9b94 100644
--- a/pyccel/codegen/printing/ccode.py
+++ b/pyccel/codegen/printing/ccode.py
@@ -2903,6 +2903,13 @@ class CCodePrinter(CodePrinter):
         else:
             return f'{c_type}_pull({list_obj})'
 
+    def _print_ListClear(self, expr):
+        target = expr.list_obj
+        class_type = target.class_type
+        c_type = self.get_c_type(class_type)
+        list_obj = self._print(ObjectAddress(expr.list_obj))
+        return f'{c_type}_clear({list_obj});\n'
+
     def _print_ListReverse(self, expr):
         class_type = expr.list_obj.class_type
         c_type = self.get_c_type(class_type)
@@ -2910,7 +2917,6 @@ class CCodePrinter(CodePrinter):
         self.add_import(Import('stc/algorithm' ,AsName(VariableTypeAnnotation(class_type), c_type)))
         return f'c_reverse({c_type}, {list_obj});\n'
 
-
     #================== Set methods ==================
 
     def _print_SetPop(self, expr):
diff --git a/pyccel/codegen/printing/fcode.py b/pyccel/codegen/printing/fcode.py
index 11943b04..ec0d4b79 100644
--- a/pyccel/codegen/printing/fcode.py
+++ b/pyccel/codegen/printing/fcode.py
@@ -1412,6 +1412,10 @@ class FCodePrinter(CodePrinter):
             self._additional_code += code
             return lhs_code
 
+    def _print_ListClear(self, expr):
+        target = self._print(expr.list_obj)
+        return f'call {target} % clear()\n'
+
     def _print_ListReverse(self, expr):
         target = self._print(expr.list_obj)
         type_name = self._print(expr.list_obj.class_type)


Test patch:
diff --git a/tests/epyccel/test_epyccel_lists.py b/tests/epyccel/test_epyccel_lists.py
index b4598507..fe688d11 100644
--- a/tests/epyccel/test_epyccel_lists.py
+++ b/tests/epyccel/test_epyccel_lists.py
@@ -369,26 +369,26 @@ def test_insert_user_defined_objects(limited_language):
     for python_elem, accelerated_elem in zip(python_list, accelerated_list):
         assert python_elem.x == accelerated_elem.x
 
-def test_clear_1(limited_language):
+def test_clear_1(language):
 
     def clear_1():
         a = [1, 2, 3]
         a.clear()
         return a
 
-    epyc_clear_1 = epyccel(clear_1, language = limited_language)
+    epyc_clear_1 = epyccel(clear_1, language = language)
     pyccel_result = epyc_clear_1()
     python_result = clear_1()
     assert python_result == pyccel_result
 
-def test_clear_2(limited_language):
+def test_clear_2(language):
 
     def clear_2():
-        a = []
+        a : 'list[int]' = []
         a.clear()
         return a
 
-    epyc_clear_2 = epyccel(clear_2, language = limited_language)
+    epyc_clear_2 = epyccel(clear_2, language = language)
     pyccel_result = epyc_clear_2()
     python_result = clear_2()
     assert python_result == pyccel_result