@@ -274,6 +274,10 @@ def filter_needs(app: Sphinx, needs, filter_string: str = "", current_need=None)
274
274
return found_needs
275
275
276
276
277
+ def need_search (* args , ** kwargs ):
278
+ return bool (re .search (* args , ** kwargs ))
279
+
280
+
277
281
@measure_time ("filtering" )
278
282
def filter_single_need (
279
283
app : Sphinx , need , filter_string : str = "" , needs = None , current_need = None , filter_compiled = None
@@ -300,15 +304,20 @@ def filter_single_need(
300
304
# Get needs external filter data and merge to filter_context
301
305
filter_context .update (app .config .needs_filter_data )
302
306
303
- filter_context ["search" ] = re . search
307
+ filter_context ["search" ] = need_search
304
308
result = False
305
309
try :
306
310
# Set filter_context as globals and not only locals in eval()!
307
311
# Otherwise, the vars not be accessed in list comprehensions.
308
312
if filter_compiled :
309
- result = bool ( eval (filter_compiled , filter_context ) )
313
+ result = eval (filter_compiled , filter_context )
310
314
else :
311
- result = bool (eval (filter_string , filter_context ))
315
+ result = eval (filter_string , filter_context )
316
+ if not isinstance (result , bool ):
317
+ # Raises NeedsInvalidFilter if the result is a string type
318
+ raise NeedsInvalidFilter (
319
+ f"Error when evaluating filter: expected output to have True/False but got a string <{ result } >"
320
+ )
312
321
except Exception as e :
313
322
raise NeedsInvalidFilter (f"Filter { filter_string } not valid. Error: { e } ." )
314
323
return result
0 commit comments