Skip to content

TypeError raised inside a code block makes the block execute twice #69

Description

@EnesYilmazcode

When a markdown code block raises TypeError, pytest-markdown-docs runs the block a second time. The except TypeError fallback in MarkdownInlinePythonItem.runtest (plugin.py L122-L130, added in #62) is there to detect custom runners that don't accept the asyncio_runner kwarg, but it also catches a TypeError coming from the user's code and re-runs the whole block. Side effects happen twice, and combined with retry:N the block runs 2*(N+1) times.

Repro with pytest-markdown-docs 0.9.2, test_bug.md:

```python
with open("counter.txt", "a") as f:
    f.write("ran\n")
raise TypeError("boom")
```
$ pytest --markdown-docs test_bug.md
$ wc -l counter.txt
2 counter.txt

Expected 1 line, the block should run once. The same block with retry:2 writes 6 lines instead of 3.

The test still fails with the right traceback either way, so this is invisible unless the block has side effects (file writes, network calls, counters in docs examples).

I have a fix ready that inspects the runner's runtest signature once instead of catching TypeError at call time, so custom runners without the kwarg keep working. Happy to open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions