Skip to content

Commit 72f7289

Browse files
authored
Backport #580 to v0.4.7. (#580)
1 parent 19325a1 commit 72f7289

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

docs/source/changes.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ chronological order. Releases follow [semantic versioning](https://semver.org/)
55
releases are available on [PyPI](https://pypi.org/project/pytask) and
66
[Anaconda.org](https://anaconda.org/conda-forge/pytask).
77

8+
## 0.4.7 - 2024-03-19
9+
10+
- {pull}`580` is a backport of {pull}`579`.
11+
812
## 0.4.6 - 2024-03-13
913

1014
- {pull}`576` fixes accidentally collecting `pytask.MarkGenerator` when using

src/_pytask/debugging.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def wrapper(*args: Any, **kwargs: Any) -> None:
328328
capman = session.config["pm"].get_plugin("capturemanager")
329329
live_manager = session.config["pm"].get_plugin("live_manager")
330330
try:
331-
task_function(*args, **kwargs)
331+
return task_function(*args, **kwargs)
332332

333333
except Exception:
334334
# Order is important! Pausing the live object before the capturemanager
@@ -409,11 +409,13 @@ def wrapper(*args: Any, **kwargs: Any) -> None:
409409
console.rule("Captured stderr", style="default")
410410
console.print(err)
411411

412-
_pdb.runcall(task_function, *args, **kwargs)
412+
out = _pdb.runcall(task_function, *args, **kwargs)
413413

414414
live_manager.resume()
415415
capman.resume()
416416

417+
return out
418+
417419
task.function = wrapper
418420

419421

tests/test_debugging.py

+43-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
IS_PEXPECT_INSTALLED = True
2222

2323

24+
def _escape_ansi(line):
25+
"""Escape ANSI sequences produced by rich."""
26+
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
27+
return ansi_escape.sub("", line)
28+
29+
2430
@pytest.mark.unit()
2531
@pytest.mark.parametrize(
2632
("value", "expected", "expectation"),
@@ -487,7 +493,40 @@ def test_function():
487493
_flush(child)
488494

489495

490-
def _escape_ansi(line):
491-
"""Escape ANSI sequences produced by rich."""
492-
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
493-
return ansi_escape.sub("", line)
496+
@pytest.mark.end_to_end()
497+
@pytest.mark.skipif(not IS_PEXPECT_INSTALLED, reason="pexpect is not installed.")
498+
@pytest.mark.skipif(sys.platform == "win32", reason="pexpect cannot spawn on Windows.")
499+
def test_pdb_with_task_that_returns(tmp_path, runner):
500+
source = """
501+
from typing_extensions import Annotated
502+
from pathlib import Path
503+
504+
def task_example() -> Annotated[str, Path("data.txt")]:
505+
return "1"
506+
"""
507+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
508+
509+
result = runner.invoke(cli, [tmp_path.as_posix(), "--pdb"])
510+
assert result.exit_code == ExitCode.OK
511+
assert tmp_path.joinpath("data.txt").read_text() == "1"
512+
513+
514+
@pytest.mark.end_to_end()
515+
@pytest.mark.skipif(not IS_PEXPECT_INSTALLED, reason="pexpect is not installed.")
516+
@pytest.mark.skipif(sys.platform == "win32", reason="pexpect cannot spawn on Windows.")
517+
def test_trace_with_task_that_returns(tmp_path):
518+
source = """
519+
from typing_extensions import Annotated
520+
from pathlib import Path
521+
522+
def task_example() -> Annotated[str, Path("data.txt")]:
523+
return "1"
524+
"""
525+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
526+
527+
child = pexpect.spawn(f"pytask {tmp_path.as_posix()}")
528+
child.sendline("c")
529+
rest = child.read().decode("utf8")
530+
assert "1 Succeeded" in _escape_ansi(rest)
531+
assert tmp_path.joinpath("data.txt").read_text() == "1"
532+
_flush(child)

0 commit comments

Comments
 (0)