Skip to content

Write test to detect unescaped double-underscore and single-underscore values in docstrings #1189

Open

Description

@Strilanc

These are almost always intended to refer to magic methods, but markdown/rst/etc interpret it as emphasis and remove the underscores. We should detect that this is happening and raise errors.

Activity

  1. removed this from the v0.6 - Western Cwm milestone on Oct 9, 2019
  2. dabacon commented on May 1, 2020

    @dabacon
    Collaborator

    An example can be found in
    https://cirq.readthedocs.io/en/stable/generated/cirq.phase_by.html
    where we see an un-escaped _phased_by_

  3. changed the title [-]Detect unescaped double-underscore and single-underscore values in docstrings[/-] [+]Write test to detect unescaped double-underscore and single-underscore values in docstrings[/+] on May 1, 2020
  4. self-assigned this
    on Jul 14, 2020
  5. added
    kind/healthFor CI/testing/release process/refactoring/technical debt items
    triage/needs-more-evidence[Feature requests] Seems plausible, but maintainers are not convinced about the use cases yet
    on Sep 17, 2020
  6. balopat commented on Sep 22, 2020

    @balopat
    Contributor

    As we're retiring sphinx, the work here is to check this behavior in devsite.

  7. removed their assignment
    on May 10, 2021
  8. 21 remaining items

  9. removed
    kind/healthFor CI/testing/release process/refactoring/technical debt items
    on Apr 7, 2025
  10. matti-sarjala commented on May 14, 2026

    @matti-sarjala

    A small implementation boundary that might keep this issue manageable:

    1. First fix one concrete rendered example, such as _phase_by_, by wrapping the literal method name in backticks in the source docstring.
    2. Then add a lightweight docs lint/test that searches public docstrings for likely unescaped protocol or magic method names.

    A conservative pattern could focus on names that look like Cirq protocol hooks:

    • _unitary_
    • _decompose_
    • _apply_unitary_
    • _phase_by_
    • _circuit_diagram_info_
    • _json_dict_

    The test does not need to understand all Markdown/RST. It could simply flag occurrences of these known literal names when they appear outside code formatting. That keeps the first version low-risk and avoids many false positives.

    This would turn the current one-off rendering fix into a guardrail so future docstrings do not reintroduce the same problem.

  11. added
    help wantedContributions welcome!
    and removed
    help wantedContributions welcome!
    on Jun 1, 2026
  12. prarthana-ma commented on Sep 24, 2026

    @prarthana-ma

    Hi @mhucka, would love to pick this up if it's still available!

    I saw PR #8368 fixes the existing unescaped symbols, but it's not meant to catch new ones going forward. I'd like to take on that part, the test @matti-sarjala described above.

    Plan: use ast to pull out actual docstrings (not just grep the whole file), check them against the known list of protocol names (_unitary_, _decompose_, _apply_unitary_, _phase_by_, _circuit_diagram_info_, _json_dict_, etc.), and flag any that aren't wrapped in backquotes, with the file and line number so it's easy to fix. I've already got a working version tested on some sample cases, so I can adapt it to the actual codebase once I'm on it.

    @WingCode I noticed you referenced this issue with a PR on your fork back in May, is that still active? Don't want to duplicate work if you're already on it.

    Thanks!

  13. mhucka commented on Sep 30, 2026

    @mhucka
    Contributor

    @prarthana-ma Thank you for your interest! It's assigned to you now.

  14. mhucka commented on Nov 1, 2026

    @mhucka
    Contributor

    @prarthana-ma I'm going around and checking the statuses of things. Could you let us know where things stand with this work?

  15. BretMishler commented on Nov 10, 2026

    @BretMishler

    I opened #8514, which adds a pylint checker for this. It flags unescaped __foo__, _foo_, _foo_bar, and foo_bar_ symbols in docstrings and document() strings.

    @prarthana-ma, I noticed this is assigned to you, so apologies for the overlap. Happy to coordinate, or to step back if you already have something in progress.

    @mhucka, there's an open scope question on the PR about whether test files should be checked too: #8514 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/docsarea/sitecomplexity/lowintroduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributorgood first issueThis issue can be resolved by someone who is not familiar with the codebase. A good starting issue.no QC knowledge neededWant to contribute to Cirq, but don't know quantum computing? This issue is for you.skill/beginnerZero to little experience with Cirq requiredtriage/acceptedA consensus emerged that this bug report, feature request, or other action should be worked on

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions