Add description to work decorator. (#2605)

* Add description to work decorator.

* Fix stutter.
This commit is contained in:
Rodrigo Girão Serrão
2023-05-22 10:32:23 +01:00
committed by GitHub
parent 33da5c1afc
commit 5e04a4d4de
2 changed files with 24 additions and 10 deletions

View File

@@ -15,6 +15,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Textual will now scroll focused widgets to center if not in view
## Unreleased
### Added
- `work` decorator accepts `description` parameter to add debug string https://github.com/Textualize/textual/issues/2597
## [0.25.0] - 2023-05-17
### Changed

View File

@@ -58,6 +58,7 @@ def work(
group: str = "default",
exit_on_error: bool = True,
exclusive: bool = False,
description: str | None = None,
) -> Callable[FactoryParamSpec, Worker[ReturnType]] | Decorator:
"""A decorator used to create [workers](/guide/workers).
@@ -67,6 +68,9 @@ def work(
group: A short string to identify a group of workers.
exit_on_error: Exit the app if the worker raises an error. Set to `False` to suppress exceptions.
exclusive: Cancel all workers in the same group.
description: Readable description of the worker for debugging purposes.
By default, it uses a string representation of the decorated method
and its arguments.
"""
def decorator(
@@ -87,22 +91,25 @@ def work(
self = args[0]
assert isinstance(self, DOMNode)
if description is not None:
debug_description = description
else:
try:
positional_arguments = ", ".join(repr(arg) for arg in args[1:])
keyword_arguments = ", ".join(
f"{name}={value!r}" for name, value in kwargs.items()
)
tokens = [positional_arguments, keyword_arguments]
worker_description = f"{method.__name__}({', '.join(token for token in tokens if token)})"
debug_description = f"{method.__name__}({', '.join(token for token in tokens if token)})"
except Exception:
worker_description = "<worker>"
debug_description = "<worker>"
worker = cast(
"Worker[ReturnType]",
self.run_worker(
partial(method, *args, **kwargs),
name=name or method.__name__,
group=group,
description=worker_description,
description=debug_description,
exclusive=exclusive,
exit_on_error=exit_on_error,
),