Skip to content

Unexpected space in docstring near escaped quote #16640

@vincent0426

Description

@vincent0426

Summary

Description

Ruff formatter modifies the spacing in a string within a docstring when the string contains an escaped quote. Specifically, it adds an unwanted space between the escaped quote and the closing triple quotes of the docstring.

Steps to Reproduce

  1. Create a Python class with the following content:
    class Sample:
        """Hello "World\""""
    
        def __init__(self, name):
            self.name = name
  2. Run ruff format on the file
  3. Observe that the formatter modifies the docstring to:
    class Sample:
        """Hello "World\" """
    
        def __init__(self, name):
            self.name = name

Expected Behavior

Docstring remain unchanged after formatting as its escaped already

"""Hello "World\""""

Version

ruff 0.9.10 (0dfa810 2025-03-07)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingformatterRelated to the formatter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions