55from typing import TYPE_CHECKING
66from typing import Any
77
8+ from _pytask .collect import _clear_annotation_locals
89from _pytask .config import hookimpl
910from _pytask .dag import create_dag_from_session
1011from _pytask .exceptions import CollectionError
3031 from _pytask .reports import CollectionReport
3132 from _pytask .reports import ExecutionReport
3233 from _pytask .session import Session
34+ from _pytask .typing import TaskFunction
3335
3436
3537@hookimpl (tryfirst = True )
@@ -53,49 +55,67 @@ def pytask_execute_task(session: Session, task: PTask) -> bool | None:
5355 if not is_task_generator (task ):
5456 return None
5557
56- execute_task (task )
58+ registered_functions = {
59+ id (function ) for functions in COLLECTED_TASKS .values () for function in functions
60+ }
61+ try :
62+ execute_task (task )
63+ except BaseException :
64+ # Children can be registered before the generator raises, without reaching
65+ # collection below. Release only their snapshots and preserve the exception.
66+ _clear_annotation_locals (
67+ function
68+ for functions in COLLECTED_TASKS .values ()
69+ for function in functions
70+ if id (function ) not in registered_functions
71+ )
72+ raise
5773
58- name_to_function : Mapping [str , Callable [..., Any ] | PTask ]
59- if isinstance (task , PTaskWithPath ) and task .path in COLLECTED_TASKS :
60- tasks = COLLECTED_TASKS .pop (task .path )
61- name_to_function = parse_collected_tasks_with_task_marker (tasks )
62- elif None in COLLECTED_TASKS :
63- tasks = COLLECTED_TASKS .pop (None )
74+ tasks : list [TaskFunction ] = []
75+ try :
76+ if isinstance (task , PTaskWithPath ) and task .path in COLLECTED_TASKS :
77+ tasks = COLLECTED_TASKS .pop (task .path )
78+ elif None in COLLECTED_TASKS :
79+ tasks = COLLECTED_TASKS .pop (None )
80+ else :
81+ msg = f"The task generator { task .name !r} did not create any tasks."
82+ raise RuntimeError (msg )
83+
84+ name_to_function : Mapping [str , Callable [..., Any ] | PTask ]
6485 name_to_function = parse_collected_tasks_with_task_marker (tasks )
65- else :
66- msg = f"The task generator { task .name !r} did not create any tasks."
67- raise RuntimeError (msg )
68-
69- new_reports : list [CollectionReport ] = []
70- for name , function in name_to_function .items ():
71- report = session .hook .pytask_collect_task_protocol (
72- session = session ,
73- reports = new_reports ,
74- path = task .path if isinstance (task , PTaskWithPath ) else None ,
75- name = name ,
76- obj = function ,
77- )
78- if report is not None :
79- new_reports .append (report )
80-
81- session .collection_reports .extend (new_reports )
82- failed_reports = [
83- report for report in new_reports if report .outcome == CollectionOutcome .FAIL
84- ]
85- if failed_reports :
86- _raise_error_for_failed_generated_tasks (task , failed_reports )
87-
88- generated_tasks = [
89- report .node
90- for report in new_reports
91- if report .outcome == CollectionOutcome .SUCCESS
92- and isinstance (report .node , PTask )
93- ]
94- _commit_generated_tasks (session , generated_tasks )
95- # Generators must run to discover tasks even in simulation modes.
96- if session .config ["dry_run" ] or session .config ["explain" ]:
97- raise WouldBeExecuted
98- return True
86+
87+ new_reports : list [CollectionReport ] = []
88+ for name , function in name_to_function .items ():
89+ report = session .hook .pytask_collect_task_protocol (
90+ session = session ,
91+ reports = new_reports ,
92+ path = task .path if isinstance (task , PTaskWithPath ) else None ,
93+ name = name ,
94+ obj = function ,
95+ )
96+ if report is not None :
97+ new_reports .append (report )
98+
99+ session .collection_reports .extend (new_reports )
100+ failed_reports = [
101+ report for report in new_reports if report .outcome == CollectionOutcome .FAIL
102+ ]
103+ if failed_reports :
104+ _raise_error_for_failed_generated_tasks (task , failed_reports )
105+
106+ generated_tasks = [
107+ report .node
108+ for report in new_reports
109+ if report .outcome == CollectionOutcome .SUCCESS
110+ and isinstance (report .node , PTask )
111+ ]
112+ _commit_generated_tasks (session , generated_tasks )
113+ # Generators must run to discover tasks even in simulation modes.
114+ if session .config ["dry_run" ] or session .config ["explain" ]:
115+ raise WouldBeExecuted
116+ return True
117+ finally :
118+ _clear_annotation_locals (tasks )
99119
100120
101121def _raise_error_for_failed_generated_tasks (
0 commit comments