-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Description
Given a piece of code containing at least one unused multiline import statement, autoimport exits abnormally.
Steps to reproduce
Given the following code:
from sympy import *
def compute_expression(expression: str) -> float:
expression = expression.replace('^', '**')
assignments, calculation = expression.split(';')[:-1], expression.split(';')[-1]
symbols_table = {}
for assignment in assignments:
variable, value = assignment.split('=')
symbols_table[variable.strip()] = float(value.strip())
for variable, value in symbols_table.items():
calculation = calculation.replace(variable, str(value))
result = eval(calculation)
return result
import unittest
from sympy import *
class TestComputeExpression(unittest.TestCase):
def test_single_operation(self):
expression = "x = 8; y = 4; z = 2; w = 3; x + y"
result = compute_expression(expression)
self.assertEqual(result, 12.0)
def test_multiple_operations(self):
expression = "x = 8; y = 4; z = 2; w = 3; x / (y + z) * w - z ^ w"
result = compute_expression(expression)
self.assertEqual(result, -1.0)
def test_parentheses(self):
expression = "x = 8; y = 4; z = 2; w = 3; (x + y) * z"
result = compute_expression(expression)
self.assertEqual(result, 24.0)
def test_exponentiation(self):
expression = "x = 8; y = 4; z = 2; w = 3; x ^ y"
result = compute_expression(expression)
self.assertEqual(result, 4096.0)
def test_variable_assignment(self):
expression = "x = 8; y = 4; z = 2; w = 3; x + y; x = 10; x + y"
result = compute_expression(expression)
self.assertEqual(result, 14.0)And we use autoimport.fix_code(code_str) to fix it, autoimport exits with the following traceback:
Traceback (most recent call last):
File "D:\XXX\fix_code_test.py", line 54, in
print(fix_code(code))
File "D:\anaconda3\envs\my_env\lib\site-packages\autoimport\services.py", line 73, in fix_code
return SourceCode(original_source_code, config=config).fix()
File "D:\anaconda3\envs\my_env\lib\site-packages\autoimport\model.py", line 67, in fix
self._fix_flake_import_errors()
File "D:\anaconda3\envs\my_env\lib\site-packages\autoimport\model.py", line 308, in _fix_flake_import_errors
self._remove_unused_imports(import_name)
File "D:\anaconda3\envs\my_env\lib\site-packages\autoimport\model.py", line 455, in _remove_unused_imports
if re.match(
File "D:\anaconda3\envs\my_env\lib\re.py", line 190, in match
return _compile(pattern, flags).match(string)
File "D:\anaconda3\envs\my_env\lib\re.py", line 303, in _compile
p = sre_compile.compile(pattern, flags)
File "D:\anaconda3\envs\my_env\lib\sre_compile.py", line 788, in compile
p = sre_parse.parse(p, flags)
File "D:\anaconda3\envs\my_env\lib\sre_parse.py", line 955, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "D:\anaconda3\envs\my_env\lib\sre_parse.py", line 444, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "D:\anaconda3\envs\my_env\lib\sre_parse.py", line 672, in _parse
raise source.error("multiple repeat",
re.error: multiple repeat at position 31
Plausible cause for this issue:
It seems that the _remove_unused_imports function in model.py (lines 441 to 505) failed to consider the scenario where object_name is not a regular identifier but can be a "*" , which can leads to an abnormal regular expression in lines 456 and 457, which then leads to a multiple repeat exception when using re.match to match lines in the code.