Python doctest how to match single or double quote in output as opposed to only single quote

Viewed 589

I commonly use the code auto-formatter Black, which has trained me to use double quotes. It has become "muscle memory" for me to use double quotes.

The default repr for many classes prints from the Python interactive output with single quotes. I know doctest seeks to reproduce the Python interactive output.

Is there some simple way to have doctest match single or double quotes?

I know one option is to make a custom doctest.OutputChecker. I am wondering if there's a simpler option.


Code Sample

from dataclasses import dataclass


@dataclass
class SomeDataClass:
    """Contains a string."""

    data: str


def get_some_dataclass(data: str) -> SomeDataClass:
    """Get a data class containing some specified data.

    Returns:
        Data class containing the data.

    Examples:
        >>> get_some_dataclass("hi")
        SomeDataClass(data="hi")

    """
    return SomeDataClass(data=data)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

Output:

**********************************************************************
File "quote_or_apostrophe.py", line 23, in __main__.get_some_dataclass
Failed example:
    get_some_dataclass("hi")
Expected:
    SomeDataClass(data="hi")
Got:
    SomeDataClass(data='hi')
**********************************************************************

Versions

I invoke doctest via pytest's doctest integration.

python==3.8.2
pytest==5.4.1
1 Answers

I imagine you could achieve this with blacken-docs but I haven't seen an example of this done yet...

Either the approach of:

  • Leave the strings in black standard (double quoted) and find a way to transform the expected output doctest so that it has double quotes (by running black on it somehow?)
  • Run blacken-docs with a single quote setting applied, which also avoids having to think about it yourself

I would instinctively go for the first option because it feels like it solves the problem better, but I expect it is easier to do the latter (and TBH not needing to think about this too much would be a plus).

Upon looking into it though, this doesn't seem supported at present according to this issue, and running blacken-docs on some code which prints a double-quoted __repr__ in a >>> doctest block does not affect said double quoting.

My interpretation of that issue and the other one it links to is that blacken-docs does not support code blocks in the format:

"""
Docstring starts here.

Code example comes next:

  >>> from some_module import some_func
  >>> some_func(1, 2)
  "1+2"

"""

I've tried rewriting code in the suggested format:

"""
Docstring starts here.

Code example comes next:

.. code-block:: python

    >>> from some_module import some_func
    >>> some_func(1, 2)
    "1+2"

"""

but this gives me

code block parse error Cannot parse: 1:0: >>> from some_module import some_func

So I'm not going to use blacken-docs or the more verbose .. code-block: python format, perhaps in time it'll be supported.

Related