rope icon indicating copy to clipboard operation
rope copied to clipboard

patchedast doesn't consider the annotation of arguments

Open v587su opened this issue 1 year ago • 0 comments

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])

v587su avatar Oct 24 '24 12:10 v587su