simplistix / sybil

Automated testing for the examples in your documentation.

Home Page:https://sybil.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

REPL functionality in code blocks

Chris-May opened this issue · comments

I'm creating a ReST document that uses code blocks to explain things in code via REPL-like syntax, like this:

.. code-block:: Python

	class Rectangle:
		pass


That is all we need to have a custom ``Rectangle`` object, as we can see below:


.. code-block:: Python

	>>> rect = Rectangle()
	>>> type(rect)
	<class '__main__.Rectangle'>

When I run code like this through Sybil, I get overlap errors:

================================================================== ERRORS ===================================================================
_________________________________________________ ERROR collecting 1 1 what are classes.rst _________________________________________________
../.venv/lib/python3.9/site-packages/sybil/integration/pytest.py:101: in collect
    self.document = self.sybil.parse(self.fspath.strpath)
../.venv/lib/python3.9/site-packages/sybil/sybil.py:106: in parse
    return Document.parse(path, *self.parsers, encoding=self.encoding)
../.venv/lib/python3.9/site-packages/sybil/document.py:46: in parse
    document.add(region)
../.venv/lib/python3.9/site-packages/sybil/document.py:89: in add
    self.raise_overlap(region, next)
../.venv/lib/python3.9/site-packages/sybil/document.py:69: in raise_overlap
    raise ValueError('{} overlaps {}'.format(*reprs))
E   ValueError: <Region start=936 end=1039 <function evaluate_code_block at 0x1043e4700>> from line 40, column 1 to line 48, column 1 overlaps <Region start=960 end=982 <bound method DocTestParser.evaluate of <sybil.parsers.doctest.DocTestParser object at 0x1043ecbb0>>> from line 42, column 1 to line 43, column 1

I take it Sybil is trying to parse the >>> indented code with a DocTestParser and CodeBlockParser.

I'm not sure how to resolve this. In my conftest.py file, if I remove DocTestParser from the parsers Sybil would use, I get SyntaxErrors:

E           >>> rect = Rectangle()
E       SyntaxError: invalid syntax

Do you have any ideas how I might solve this issue?

I'm not sure the .. code-block:: python is either necessary or correct for repl block, see examples here:

https://testfixtures.readthedocs.io/en/latest/comparing.html#the-compare-function

https://github.com/Simplistix/testfixtures/blob/5e82707f24781435fa5f555d7e06d64d256dab34/docs/comparing.txt#L20-L24

Can you try without and let me know if you have problems?

Interesting!

I had no idea that ReST would identify a REPL block. That must've flown by me every time I read the various documentations of the spec.

I appreciate the direction. Hopefully, I can work around the blocks where I combined the two styles.