Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: unused multiline import statement trigges re.error #245

Open
fuanan opened this issue Aug 30, 2023 · 1 comment
Open

Bug: unused multiline import statement trigges re.error #245

fuanan opened this issue Aug 30, 2023 · 1 comment
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@fuanan
Copy link

fuanan commented Aug 30, 2023

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.

@fuanan fuanan added the bug Something isn't working label Aug 30, 2023
@lyz-code
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants