You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a piece of code containing at least one unused multiline import statement, autoimport exits abnormally.
Steps to reproduce
Given the following code:
fromsympyimport*defcompute_expression(expression: str) ->float:
expression=expression.replace('^', '**')
assignments, calculation=expression.split(';')[:-1], expression.split(';')[-1]
symbols_table= {}
forassignmentinassignments:
variable, value=assignment.split('=')
symbols_table[variable.strip()] =float(value.strip())
forvariable, valueinsymbols_table.items():
calculation=calculation.replace(variable, str(value))
result=eval(calculation)
returnresultimportunittestfromsympyimport*classTestComputeExpression(unittest.TestCase):
deftest_single_operation(self):
expression="x = 8; y = 4; z = 2; w = 3; x + y"result=compute_expression(expression)
self.assertEqual(result, 12.0)
deftest_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)
deftest_parentheses(self):
expression="x = 8; y = 4; z = 2; w = 3; (x + y) * z"result=compute_expression(expression)
self.assertEqual(result, 24.0)
deftest_exponentiation(self):
expression="x = 8; y = 4; z = 2; w = 3; x ^ y"result=compute_expression(expression)
self.assertEqual(result, 4096.0)
deftest_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.
The text was updated successfully, but these errors were encountered:
Hi @fuanan thanks for taking the time to open an issue. Using from X import * is not recommended when writing python code because:
It can be hard to follow the breadcrumbs of the origin of an object
If you do it with more than one package you can get unexpected behaviours if there are objects with the same name.
Thus it's not some syntax I'd like to support. That being said, if you want to make a pull request to solve it that doesn't add complexity I'd be fine merging it
Description
Given a piece of code containing at least one unused multiline import statement, autoimport exits abnormally.
Steps to reproduce
Given the following code:
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.
The text was updated successfully, but these errors were encountered: