Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #580 to v0.4.7. #580

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ chronological order. Releases follow [semantic versioning](https://semver.org/)
releases are available on [PyPI](https://pypi.org/project/pytask) and
[Anaconda.org](https://anaconda.org/conda-forge/pytask).

## 0.4.7 - 2024-03-19

- {pull}`580` is a backport of {pull}`579`.

## 0.4.6 - 2024-03-13

- {pull}`576` fixes accidentally collecting `pytask.MarkGenerator` when using
Expand Down
6 changes: 4 additions & 2 deletions src/_pytask/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
capman = session.config["pm"].get_plugin("capturemanager")
live_manager = session.config["pm"].get_plugin("live_manager")
try:
task_function(*args, **kwargs)
return task_function(*args, **kwargs)

except Exception:
# Order is important! Pausing the live object before the capturemanager
Expand Down Expand Up @@ -409,11 +409,13 @@
console.rule("Captured stderr", style="default")
console.print(err)

_pdb.runcall(task_function, *args, **kwargs)
out = _pdb.runcall(task_function, *args, **kwargs)

live_manager.resume()
capman.resume()

return out

Check warning on line 417 in src/_pytask/debugging.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/debugging.py#L417

Added line #L417 was not covered by tests

task.function = wrapper


Expand Down
47 changes: 43 additions & 4 deletions tests/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
IS_PEXPECT_INSTALLED = True


def _escape_ansi(line):
"""Escape ANSI sequences produced by rich."""
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)


@pytest.mark.unit()
@pytest.mark.parametrize(
("value", "expected", "expectation"),
Expand Down Expand Up @@ -487,7 +493,40 @@ def test_function():
_flush(child)


def _escape_ansi(line):
"""Escape ANSI sequences produced by rich."""
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)
@pytest.mark.end_to_end()
@pytest.mark.skipif(not IS_PEXPECT_INSTALLED, reason="pexpect is not installed.")
@pytest.mark.skipif(sys.platform == "win32", reason="pexpect cannot spawn on Windows.")
def test_pdb_with_task_that_returns(tmp_path, runner):
source = """
from typing_extensions import Annotated
from pathlib import Path

def task_example() -> Annotated[str, Path("data.txt")]:
return "1"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, [tmp_path.as_posix(), "--pdb"])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("data.txt").read_text() == "1"


@pytest.mark.end_to_end()
@pytest.mark.skipif(not IS_PEXPECT_INSTALLED, reason="pexpect is not installed.")
@pytest.mark.skipif(sys.platform == "win32", reason="pexpect cannot spawn on Windows.")
def test_trace_with_task_that_returns(tmp_path):
source = """
from typing_extensions import Annotated
from pathlib import Path

def task_example() -> Annotated[str, Path("data.txt")]:
return "1"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

child = pexpect.spawn(f"pytask {tmp_path.as_posix()}")
child.sendline("c")
rest = child.read().decode("utf8")
assert "1 Succeeded" in _escape_ansi(rest)
assert tmp_path.joinpath("data.txt").read_text() == "1"
_flush(child)
Loading