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

escape-sequence-in-docstring (D301) reports escaped docstrings within docstrings #12152

Closed
tjkuson opened this issue Jul 2, 2024 · 3 comments
Assignees
Labels
bug Something isn't working docstring Related to docstring linting or formatting help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@tjkuson
Copy link
Contributor

tjkuson commented Jul 2, 2024

Running

ruff check --isolated --select D301

on

def foo():
    """
    This docstring contains another docstring.

        def bar():
            \"\"\"Here it is!\"\"\"
    """

returns an escape-sequence-in-docstring (D301) diagnostic, printing

example.py:2:5: D301 Use `r"""` if any backslashes in a docstring
  |
1 |   def foo():
2 |       """
  |  _____^
3 | |     This docstring contains another docstring.
4 | |
5 | |         def bar():
6 | |             \"\"\"Here it is!\"\"\"
7 | |     """
  | |_______^ D301
  |
  = help: Add `r` prefix

However, adding the r prefix and removing the backslashes is a syntax error:

def foo():
    r"""
    This docstring contains another docstring.

        def bar():
            """Here it is!"""
    """

Docstrings in within docstrings can appear when a docstring contains a code snippet (which is how I discovered the issue). I would expect D301 not to be raised, as making the docstring raw doesn't work.

Ruff version 0.5.0

Search terms: D301, backslash, escape

Update: managed to reproduce with

def foo():
    """
    This docstring contains another docstring.

        def bar():
            \"""Here it is!\"""
    """

as well.

@AlexWaygood AlexWaygood added bug Something isn't working rule Implementing or modifying a lint rule docstring Related to docstring linting or formatting labels Jul 2, 2024
@MichaReiser MichaReiser added the help wanted Contributions especially welcome label Jul 3, 2024
@ukyen8
Copy link
Contributor

ukyen8 commented Jul 3, 2024

Hi, I would like to work on this issue. Is anyone already working on it?

@dhruvmanila
Copy link
Member

@ukyen8 I don't think so, go for it!

charliermarsh pushed a commit that referenced this issue Jul 18, 2024
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
This PR updates D301 rule to allow inclduing escaped docstring, e.g.
`\"""Foo.\"""` or `\"\"\"Bar.\"\"\"`, within a docstring.

Related issue: #12152 

## Test Plan

Add more test cases to D301.py and update the snapshot file.

<!-- How was it tested? -->
@charliermarsh
Copy link
Member

Closed by #12192 (review). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docstring Related to docstring linting or formatting help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

6 participants