Skip to content

Commit ad5d36c

Browse files
EmilyBourneyguclu
andauthored
Assert is not a function (pyccel#1845)
Remove all brackets that handle an assertion like a function call. Docs are also added and dead code is removed from the Errors classes (unused methods `unset_target` and `reset_target`). The function `Errors.set_target` is modified to remove the unnecessary argument `kind`. Further, lots of unnecessary whitespace is removed from the unit tests. --------- Co-authored-by: Yaman Güçlü <[email protected]>
1 parent 75e7f29 commit ad5d36c

32 files changed

+796
-761
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## \[UNRELEASED\]
55

66
### Added
7+
78
- #1720 : Add support for `Ellipsis` as the only index for an array.
89
- #1694 : Add Python support for list method `extend()`.
910
- #1700 : Add Python support for list method `sort()`.
@@ -39,6 +40,7 @@ All notable changes to this project will be documented in this file.
3940
- #1842 : Fix homogeneous tuples incorrectly identified as inhomogeneous.
4041

4142
### Changed
43+
4244
- #1720 : functions with the `@inline` decorator are no longer exposed to Python in the shared library.
4345
- #1720 : Error raised when incompatible arguments are passed to an `inlined` function is now fatal.
4446
- \[TESTS\] Filter out cast warnings in cast tests.
@@ -63,6 +65,7 @@ All notable changes to this project will be documented in this file.
6365
- \[INTERNALS\] All internal classes which can be generated from `FunctionCall`s must inherit from `PyccelFunction`.
6466
- \[INTERNALS\] `PyccelFunction` objects which do not represent objects in memory have the type `SymbolicType`.
6567
- \[INTERNALS\] Rename `_visit` functions called from a `FunctionCall` which don't match the documented naming pattern to `_build` functions.
68+
- \[INTERNALS\] Remove unnecessary argument `kind` to `Errors.set_target`.
6669

6770
### Deprecated
6871

@@ -76,6 +79,7 @@ All notable changes to this project will be documented in this file.
7679
- \[INTERNALS\] Remove unused parameters `expr`, `status` and `like` from `pyccel.ast.core.Assign`.
7780
- \[INTERNALS\] Remove `pyccel.ast.utilities.builtin_functions`.
7881
- \[INTERNALS\] Remove unused/unnecessary functions in `pyccel.parser.utilities` : `read_file`, `header_statement`, `accelerator_statement`, `get_module_name`, `view_tree`.
82+
- \[INTERNALS\] Remove unused functions `Errors.unset_target`, and `Errors.reset_target`.
7983

8084
## \[1.11.2\] - 2024-03-05
8185

pyccel/ast/basic.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ def substitute(self, original, replacement, excluded_nodes = (), invalidate = Tr
280280
self._recursion_in_progress = True
281281

282282
if iterable(original):
283-
assert(iterable(replacement))
284-
assert(len(original) == len(replacement))
283+
assert iterable(replacement)
284+
assert len(original) == len(replacement)
285285
else:
286286
original = (original,)
287287
replacement = (replacement,)
@@ -454,19 +454,23 @@ def clear_syntactic_user_nodes(self):
454454
self._user_nodes = [u for u in self._user_nodes if u.pyccel_staging != 'syntactic']
455455

456456
def remove_user_node(self, user_node, invalidate = True):
457-
""" Indicate that the current node is no longer used
458-
by the user_node. This function is usually called by
459-
the substitute method
457+
"""
458+
Remove the specified user node from the AST tree.
459+
460+
Indicate that the current node is no longer used by the user_node.
461+
This function is usually called by the substitute method. It removes
462+
the specified user node from the user nodes internal property
463+
meaning that the node cannot appear in the results when searching
464+
through the tree.
460465
461466
Parameters
462467
----------
463468
user_node : PyccelAstNode
464-
Node which previously used the current node
469+
Node which previously used the current node.
465470
invalidate : bool
466-
Indicates whether the removed object should
467-
be invalidated
471+
Indicates whether the removed object should be invalidated.
468472
"""
469-
assert(user_node in self._user_nodes)
473+
assert user_node in self._user_nodes
470474
self._user_nodes.remove(user_node)
471475
if self.is_unused and invalidate:
472476
self.invalidate_node()

pyccel/ast/builtins.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,13 @@ def imag(self):
330330

331331
@property
332332
def internal_var(self):
333-
""" When the complex call is a cast, returns the variable being cast """
334-
assert(self._is_cast)
333+
"""
334+
When the complex call is a cast, returns the variable being cast.
335+
336+
When the complex call is a cast, returns the variable being cast.
337+
This property should only be used when handling a cast.
338+
"""
339+
assert self._is_cast
335340
return self._internal_var
336341

337342
def __str__(self):
@@ -1145,7 +1150,7 @@ def __call__(self, *args):
11451150
""" Returns the expression with the arguments replaced with
11461151
the calling arguments
11471152
"""
1148-
assert(len(args) == len(self.variables))
1153+
assert len(args) == len(self.variables)
11491154
return self.expr.subs(self.variables, args)
11501155

11511156
def __str__(self):

pyccel/ast/literals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class LiteralInteger(Literal):
137137
__slots__ = ('_value', '_class_type')
138138

139139
def __init__(self, value, dtype = PythonNativeInt()):
140-
assert(value >= 0)
140+
assert value >= 0
141141
if not isinstance(value, int):
142142
raise TypeError("A LiteralInteger can only be created with an integer")
143143
self._value = value

pyccel/ast/utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def insert_index(expr, pos, index_var):
333333
# Add index at the required position
334334
if base.shape[pos]==1:
335335
# If there is no dimension in this axis, reduce the rank
336-
assert(indices[pos].start is None)
336+
assert indices[pos].start is None
337337
index_var = LiteralInteger(0)
338338

339339
else:

pyccel/codegen/printing/ccode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class CCodePrinter(CodePrinter):
296296

297297
def __init__(self, filename, prefix_module = None):
298298

299-
errors.set_target(filename, 'file')
299+
errors.set_target(filename)
300300

301301
super().__init__()
302302
self.prefix_module = prefix_module

pyccel/codegen/printing/fcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class FCodePrinter(CodePrinter):
245245

246246
def __init__(self, filename, prefix_module = None):
247247

248-
errors.set_target(filename, 'file')
248+
errors.set_target(filename)
249249

250250
super().__init__()
251251
self._constantImports = {}

pyccel/codegen/printing/pycode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PythonCodePrinter(CodePrinter):
7575
}
7676

7777
def __init__(self, filename):
78-
errors.set_target(filename, 'file')
78+
errors.set_target(filename)
7979
super().__init__()
8080
self._additional_imports = {}
8181
self._aliases = {}

pyccel/errors/errors.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ def __str__(self):
172172

173173

174174
class ErrorsMode(metaclass = Singleton):
175-
"""Developper or User mode.
176-
pyccel command line will set it.
175+
"""
176+
The mode for the error output.
177+
178+
The mode for the error output. This is either 'developer' or 'user'.
179+
In developer mode the errors are more verbose and include a traceback
180+
this helps developers debug errors.
177181
"""
178182
def __init__(self):
179183
self._mode = 'user'
@@ -183,7 +187,17 @@ def value(self):
183187
return self._mode
184188

185189
def set_mode(self, mode):
186-
assert(mode in ['user', 'developer'])
190+
"""
191+
Set the error mode.
192+
193+
Set the error mode to either 'developer' or 'user'.
194+
195+
Parameters
196+
----------
197+
mode : str
198+
The new error mode.
199+
"""
200+
assert mode in ['user', 'developer']
187201
self._mode = mode
188202

189203

@@ -211,32 +225,39 @@ def mode(self):
211225
return self._mode.value
212226

213227
def initialize(self):
228+
"""
229+
Initialise the Errors singleton.
230+
231+
Initialise the Errors singleton. This function is necessary so
232+
the singleton can be reinitialised using the `reset` function.
233+
"""
214234
self.error_info_map = OrderedDict()
215235

216-
self._target = {}
217-
self._target['file'] = None
218-
self._target['module'] = None
219-
self._target['function'] = None
220-
self._target['class'] = None
236+
self._target = None
221237

222238
def reset(self):
239+
"""
240+
Reset the Errors singleton.
241+
242+
Reset the Errors singleton. This removes any information about
243+
previously generated errors or warnings. This method should be
244+
called before starting a new translation.
245+
"""
223246
self.initialize()
224247

225-
def set_target(self, target, kind):
226-
assert(kind in ['file', 'module', 'function', 'class'])
227-
self._target[kind] = target
248+
def set_target(self, target):
249+
"""
250+
Set the current translation target.
228251
229-
def unset_target(self, kind):
230-
assert(kind in ['file', 'module', 'function', 'class'])
231-
self._target[kind] = None
252+
Set the current translation target which describes the location
253+
from which the error is being raised.
232254
233-
def reset_target(self):
234-
"""."""
235-
self._target = {}
236-
self._target['file'] = None
237-
self._target['module'] = None
238-
self._target['function'] = None
239-
self._target['class'] = None
255+
Parameters
256+
----------
257+
target : str
258+
The name of the file being translated.
259+
"""
260+
self._target = target
240261

241262
def report(self,
242263
message,
@@ -292,7 +313,7 @@ def report(self,
292313
return
293314

294315
if filename is None:
295-
filename = self.target['file']
316+
filename = self.target
296317

297318
# TODO improve. it is assumed here that tl and br have the same line
298319
if bounding_box:

pyccel/parser/semantic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def annotate(self):
287287

288288
errors = Errors()
289289
if self.filename:
290-
errors.set_target(self.filename, 'file')
290+
errors.set_target(self.filename)
291291

292292
# then we treat the current file
293293

@@ -2925,7 +2925,7 @@ def _visit_Assign(self, expr):
29252925
# TODO unset position at the end of this part
29262926
new_expressions = []
29272927
python_ast = expr.python_ast
2928-
assert(python_ast)
2928+
assert python_ast
29292929

29302930
rhs = expr.rhs
29312931
lhs = expr.lhs
@@ -3058,7 +3058,7 @@ def _visit_Assign(self, expr):
30583058
c_ranks = [x.value.rank for x in call_args]
30593059
same_ranks = [x==y for (x,y) in zip(f_ranks, c_ranks)]
30603060
if not all(same_ranks):
3061-
assert(len(c_ranks) == 1)
3061+
assert len(c_ranks) == 1
30623062
arg = call_args[0].value
30633063
d_var['shape' ] = arg.shape
30643064
d_var['memory_handling'] = arg.memory_handling
@@ -4478,7 +4478,7 @@ def _visit_MacroVariable(self, expr):
44784478

44794479
def _visit_StarredArguments(self, expr):
44804480
var = self._visit(expr.args_var)
4481-
assert(var.rank==1)
4481+
assert var.rank==1
44824482
size = var.shape[0]
44834483
return StarredArguments([var[i] for i in range(size)])
44844484

0 commit comments

Comments
 (0)