simplistix / sybil

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

Escapes inside docstring cause Sybil to fail #119

Closed taminomara closed 2 months ago

taminomara commented 2 months ago

I noticed that if I use string escapes in docstrings, Sybil fails to evaluate them. Minimal example is this:

def decorator(fn):
    """
    Example:

    >>> @foo
    ... def decorated():
    ...     \"\"\"docstring of a decorated function\"\"\"

    """

Sybil fails with SyntaxError: unexpected character after line continuation character.

I'm using sybil==6.1.1 and Python 3.12.2.

taminomara commented 2 months ago

Am I correct that Sybil is not aware of Python syntax, and just looks through sources using regular expressions? If so, I'll switch to using raw docstrings to avoid escapes, but still I'd like to see this fixed.

cjw296 commented 2 months ago

Sybil fails with SyntaxError: unexpected character after line continuation character.

Please can you provide a full traceback.

You are correct that Sybil is agnostic to Python syntax, however for docstrings the ast module is used to parse python source code and extract docstrings.

it may be that this is a quick fix, so that full traceback would be very useful.

cjw296 commented 2 months ago

Here's the docs to literal escaping, so this does feel like a bug in Sybil: https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences