Skip to content

Commit 9340fb4

Browse files
committed
Simplify again.
1 parent 192856b commit 9340fb4

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

src/_pytask/task_utils.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,20 @@ def create_text_file() -> Annotated[str, Path("file.txt")]:
101101
# 3.14+. The wrapper closure captures this variable.
102102
caller_locals = sys._getframe(1).f_locals.copy()
103103

104-
# Detect if decorator is used without parentheses: @task instead of @task()
105-
# In this case, `name` is actually the function being decorated.
106-
if is_task_function(name) and kwargs is None:
107-
func_to_wrap = name
108-
actual_name = None
109-
else:
110-
func_to_wrap = None
111-
actual_name = name
112-
# Validate arguments only when used with parentheses
113-
for arg, arg_name in ((name, "name"), (id, "id")):
104+
def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
105+
# Omits frame when a builtin function is wrapped.
106+
_rich_traceback_omit = True
107+
108+
# When @task is used without parentheses, name is the function, not a string.
109+
effective_name = None if is_task_function(name) else name
110+
111+
for arg, arg_name in ((effective_name, "name"), (id, "id")):
114112
if not (isinstance(arg, str) or arg is None):
115113
msg = (
116114
f"Argument {arg_name!r} of @task must be a str, but it is {arg!r}."
117115
)
118116
raise ValueError(msg)
119117

120-
def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
121-
# Omits frame when a builtin function is wrapped.
122-
_rich_traceback_omit = True
123-
124118
unwrapped = unwrap_task_function(func)
125119
if isinstance(unwrapped, Function):
126120
coiled_kwargs = extract_coiled_function_kwargs(unwrapped)
@@ -141,7 +135,7 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
141135
path = get_file(unwrapped)
142136

143137
parsed_kwargs = {} if kwargs is None else kwargs
144-
parsed_name = _parse_name(unwrapped, actual_name)
138+
parsed_name = _parse_name(unwrapped, effective_name)
145139
parsed_after = _parse_after(after)
146140

147141
if hasattr(unwrapped, "pytask_meta"):
@@ -174,8 +168,9 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
174168

175169
return unwrapped
176170

177-
if func_to_wrap is not None:
178-
return wrapper(func_to_wrap)
171+
# When decorator is used without parentheses, call wrapper directly.
172+
if is_task_function(name) and kwargs is None:
173+
return wrapper(name)
179174
return wrapper
180175

181176

0 commit comments

Comments
 (0)