Skip to content

Error while restructuring in py3.7 #311

@bbcVld

Description

@bbcVld

While running Restructure on project which has async method which call sync method I got an error.
Example below:

import asyncio


class Gotcha:

    async def some_async_method(self):
        # do stuff
        pass
        self.some_sync_method()
        # do another stuff
        await asyncio.sleep(1)

    def some_sync_method(self):
        pass

    def next_method(self):
        pass

def pow(x, y):
    result = 1
    for i in range(y):
        result *= x
    return result

a = pow(2, 3)  # example from tutorail. Try to change to 2 ** 3

Traceback:

/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py:87: RuntimeWarning: Unknown node type <AsyncFunctionDef>; please report!
  % node.__class__.__name__, RuntimeWarning)
in file test.py
Traceback (most recent call last):
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 770, in consume
    new_offset = self.source.index(token, self.offset)
ValueError: substring not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/vladislav/projects/rope_test/tmp38d72y", line 12, in <module>
    changes = Restructure(myproject, 'pow(${param1}, ${param2})', '${param1} ** ${param2}').get_changes()
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/restructure.py", line 143, in get_changes
    wildcards=self.wildcards)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/similarfinder.py", line 29, in __init__
    pymodule.source_code, pymodule.get_ast(), self._does_match)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/similarfinder.py", line 78, in __init__
    self._init_using_ast(node, source)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/similarfinder.py", line 87, in _init_using_ast
    patchedast.patch_ast(node, source)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 41, in patch_ast
    ast.call_for_nodes(node, walker)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/base/ast.py", line 63, in call_for_nodes
    result = callback(node)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 84, in __call__
    return method(node)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 574, in _Module
    self._handle(node, list(node.body), eat_spaces=True)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 112, in _handle
    ast.call_for_nodes(child, self)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/base/ast.py", line 63, in call_for_nodes
    result = callback(node)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 84, in __call__
    return method(node)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 323, in _ClassDef
    self._handle(node, children)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 112, in _handle
    ast.call_for_nodes(child, self)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/base/ast.py", line 63, in call_for_nodes
    result = callback(node)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 84, in __call__
    return method(node)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 438, in _FunctionDef
    self._handle(node, children)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 128, in _handle
    region = self.source.consume(child)
  File "/home/vladislav/.virtualenvs/rope_test/lib/python3.7/site-packages/rope/refactor/patchedast.py", line 778, in consume
    (token, self._get_location()))
rope.refactor.patchedast.MismatchedTokenError: Token <next_method> at (19, 3) cannot be matched

[Refactoring was finished in 0.2s with exit code 1]
[shell_cmd: /home/vladislav/.virtualenvs/rope_test/bin/python -u '/home/vladislav/projects/rope_test/tmp38d72y']
[dir: /home/vladislav/projects/rope_test]
[path: /home/vladislav/.local/bin:/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/usr/lib/qt4/bin:/sbin:/usr/sbin:/home/vladislav/bin].

If I change function to async It will work.

versions:

 python --version
Python 3.7.6

pip show rope
Name: rope
Version: 0.18.0

I run this whith sublime plugin PyRefactor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions