Skip to content

AST Rewriter doesn't correctly detect the whole comment when it is multiline #189

Open

Description

@FrancescoPezzella

Discovered while applying #127 to redis-py.

Currently, the AST rewriter only assumes a comment is a single line long. It doesn't account for a comment being longer than that.

For example, in this case, only the line "# comment" would be marked as the comment and the earlier lines will be considered separate comments.

# This
# Is
# A
# Multiline
# Comment
[ enter func here ]
    @staticmethod
    def get_comment_location(start_offset: int, stop_offset: int, content: bytes) -> tuple[int, int]:
        """Get the location of the comment before the location, but after the stop_location
        a comment is a line that starts with // or a block that starts with /* and ends with */
        or a line that starts with #.
        """
        # search last occurrence of //, /*, # in a byte array
        comment_start = content.rfind(b"//", start_offset, stop_offset)
        if comment_start != -1:
            comment_end = _RewriteActions.__get_end_of_line(content, comment_start)
            return comment_start, comment_end
        comment_start = content.rfind(b"/*", start_offset, stop_offset)
        if comment_start != -1:
            comment_end = content.find(b"*/", comment_start, stop_offset)
            if comment_end != -1:
                comment_end += len("*/")
            return comment_start, comment_end
        comment_start = content.rfind(b"#", start_offset, stop_offset)
        if comment_start != -1:
            comment_end = _RewriteActions.__get_end_of_line(content, comment_start)
            return comment_start, comment_end
        return -1, -1

This seems to me like an oversight. It is not that uncommon for a comment to be longer than a few lines, and still mention the same thing.

@pjljvandelaar - thoughts?

Activity

  1. removed their assignment
    on Sep 16, 2026
  2. added 4 commits that reference this issue on Oct 12, 2026
  3. added 6 commits that reference this issue on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions