simplistix / sybil

Automated testing for the examples in your documentation.
https://sybil.readthedocs.io/en/latest/
Other
74 stars 14 forks source link

Unexpected whitespace error #46

Closed bveeramani closed 1 year ago

bveeramani commented 1 year ago

I used Sybil to test a simple docstring, but got an unexpected error.

conftest.py

from sybil import Sybil
from sybil.parsers.rest import DocTestParser

pytest_collect_file = Sybil(
    parsers=[DocTestParser()],
    patterns=['*.py'],
).pytest()

module.py

def f():
    """Do something.

    Example:
        >>> x = 3
        >>> x
        3
    """
    pass

Traceback

❯ pytest
================================================================================================ test session starts ================================================================================================
platform darwin -- Python 3.10.9, pytest-7.2.2, pluggy-1.0.0
rootdir: /Users/balaji/Documents/sybil-example, configfile: pytest.ini
collected 0 items / 1 error                                                                                                                                                                                         

====================================================================================================== ERRORS =======================================================================================================
____________________________________________________________________________________________ ERROR collecting module.py _____________________________________________________________________________________________
.venv/lib/python3.10/site-packages/sybil/integration/pytest.py:104: in collect
    self.document = self.sybil.parse(Path(self.fspath.strpath))
.venv/lib/python3.10/site-packages/sybil/sybil.py:143: in parse
    return type_.parse(str(path), *self.parsers, encoding=self.encoding)
.venv/lib/python3.10/site-packages/sybil/document.py:54: in parse
    for region in parser(document):
.venv/lib/python3.10/site-packages/sybil/parsers/abstract/doctest.py:41: in __call__
    source, options, want, exc_msg = self._parse_example(m, name, lineno)
/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/doctest.py:721: in _parse_example
    self._check_prefix(want_lines, ' '*indent, name,
/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/doctest.py:806: in _check_prefix
    raise ValueError('line %r of the docstring for %s has '
E   ValueError: line 8 of the docstring for /Users/balaji/Documents/sybil-example/module.py has inconsistent leading whitespace: '    """'
============================================================================================== short test summary info ==============================================================================================
ERROR module.py - ValueError: line 8 of the docstring for /Users/balaji/Documents/sybil-example/module.py has inconsistent leading whitespace: '    """'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================= 1 error in 0.05s ==================================================================================================

Dependencies

❯ pip freeze
attrs==22.2.0
exceptiongroup==1.1.0
iniconfig==2.0.0
packaging==23.0
pluggy==1.0.0
pytest==7.2.2
sybil==4.0.1
tomli==2.0.1
bveeramani commented 1 year ago

If I add an extra newline, everything works as expected.

def f():
    """Do something.

    Example:
        >>> x = 3
        >>> x
        3

    """
    pass
bveeramani commented 1 year ago

Seems like there's no issue with pure doctest

❯ python -m doctest module.py
cjw296 commented 1 year ago

This is a surprisingly difficult issue to fix, but I am looking into it!

cjw296 commented 1 year ago

Released in https://pypi.org/project/sybil/5.0.0/