Skip to content

patchedast doesn't consider the annotation of arguments #802

Description

@v587su

The following code will trigger an AttributeError:

source_code = dedent("""
def foo(l: list):
    pass
""")

finder = RawSimilarFinder(source_code)
pattern = "${a}"
print(list(finder.get_matches(pattern)))
Traceback (most recent call last):
  File "/workspace/test.py", line 11, in <module>
    print(list(finder.get_matches(pattern)))
  File "/usr/local/lib/python3.10/dist-packages/rope/refactor/similarfinder.py", line 109, in get_matches
    match_start, match_end = match.get_region()
  File "/usr/local/lib/python3.10/dist-packages/rope/refactor/similarfinder.py", line 259, in get_region
    return self.ast.region
AttributeError: 'Name' object has no attribute 'region'

This is because line 632 in rope.refactor.patchedast doesn't consider the annotation of arg.

    def _arg(self, node):
        self._handle(node, [node.arg])

Change it to the following code can fix the bug

    def _arg(self, node):
        if node.annotation:
            self._handle(node, [node.arg, ":", node.annotation])
        else:
            self._handle(node, [node.arg])

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnexpected or incorrect user-visible behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions