From 469d0c67db95e0e01400c8a303913c203a710070 Mon Sep 17 00:00:00 2001 From: Scott Huberty Date: Sun, 16 Jun 2024 19:12:29 -0700 Subject: [PATCH 1/2] FIX: make if-statement for extra-informative message in evaluate_connect_function more permissive the if-statement in this function is very specific and ensures that the error message both starts with "global name" and ends with "is not defined" before raising the informative error about nested imports. https://github.com/nipreps/nibabies/pull/365 gives one example where this if-statement is too specific, and doesn't catch a NameError that does actually arise from a module-level import. --- nipype/pipeline/engine/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/pipeline/engine/utils.py b/nipype/pipeline/engine/utils.py index c5bc83c832..0f192ccedb 100644 --- a/nipype/pipeline/engine/utils.py +++ b/nipype/pipeline/engine/utils.py @@ -689,7 +689,7 @@ def evaluate_connect_function(function_source, args, first_arg): try: output_value = func(first_arg, *list(args)) except NameError as e: - if e.args[0].startswith("global name") and e.args[0].endswith("is not defined"): + if e.args[0].endswith("is not defined"): e.args = ( e.args[0], ( From df241b29b45933337962d3d44ba52ef9456e2e3e Mon Sep 17 00:00:00 2001 From: Scott Huberty Date: Tue, 8 Oct 2024 10:53:22 -0700 Subject: [PATCH 2/2] FIX: improve evaluate_connect_function error handling. This incorporates Chris's suggestions from https://github.com/nipy/nipype/pull/3655#issuecomment-2395502745 Except I raise a new error and include the original error message (instead of revising the original error message). Co-authored-by: Chris Markiewicz --- nipype/pipeline/engine/utils.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/nipype/pipeline/engine/utils.py b/nipype/pipeline/engine/utils.py index 0f192ccedb..0f800aa02a 100644 --- a/nipype/pipeline/engine/utils.py +++ b/nipype/pipeline/engine/utils.py @@ -689,15 +689,10 @@ def evaluate_connect_function(function_source, args, first_arg): try: output_value = func(first_arg, *list(args)) except NameError as e: - if e.args[0].endswith("is not defined"): - e.args = ( - e.args[0], - ( - "Due to engine constraints all imports have to be done " - "inside each function definition" - ), - ) - raise e + raise NameError( + f"{e}: Due to engine constraints all imports have to be done inside each " + " function definition." + ) return output_value