Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-line strings lead to incorrect syntax #226

Closed
yarnabrina opened this issue Feb 20, 2023 · 1 comment
Closed

multi-line strings lead to incorrect syntax #226

yarnabrina opened this issue Feb 20, 2023 · 1 comment

Comments

@yarnabrina
Copy link

Python Version

3.9.16

Package Version

1.13.0

Description

Question

If there is a multi-line string within ''' inside a docstring within """, blacken-docs changes the quote to """, leading to syntax error. Is there any way to tell blacken-docs to skip specific line(s), similar to black itself or most other tools?

Reproducible Code

Create test.py with the following snippet:

"""Module docstring."""
import sys


def function(argument: str) -> None:
    """Function docstring.

    Parameters
    ----------
    argument : str
        argument documentation

    Examples
    --------
    .. code-block:: pycon

        >>> sample = '''
        ...     this is
        ...     unnecessarily split
        ...     in milriple lines
        ...     only for demonstration
        ... '''
        >>> function(sample)  # doctest: +NORMALIZE_WHITESPACE
        3
            this is
            unnecessarily split
            in milriple lines
            only for demonstration
    """
    print(sys.version_info.major)
    print(argument)

If I execute blacken-docs test.py, the formatted code will become this:

"""Module docstring."""
import sys


def function(argument: str) -> None:
    """Function docstring.

    Parameters
    ----------
    argument : str
        argument documentation

    Examples
    --------
    .. code-block:: pycon

        >>> sample = """
        ...     this is
        ...     unnecessarily split
        ...     in milriple lines
        ...     only for demonstration
        ... """
        >>> function(sample)  # doctest: +NORMALIZE_WHITESPACE
        3
            this is
            unnecessarily split
            in milriple lines
            only for demonstration
    """
    print(sys.version_info.major)
    print(argument)

This is not valid python code, because of """ inside """.

Attempts

I tried passing # fmt: skip, #fmt: off, etc., but that does not skip the formatting of code.

Known Workaround

If I use ''' for the main docstring and """ in the example, this problem won't arise as blacken won't modify the example and black will not modify main docstring as well detecting there's a """ inside.

@adamchainz
Copy link
Owner

Given that blacken-docs doesn't have a real parser, only regexes, I think the only way forwards is to add enable/disable comments, as covered in #193.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants