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

crashes with ValueError when the import to remove appears twice #225

Closed
Jasha10 opened this issue Oct 8, 2022 · 2 comments · Fixed by #229
Closed

crashes with ValueError when the import to remove appears twice #225

Jasha10 opened this issue Oct 8, 2022 · 2 comments · Fixed by #229
Labels
bug Something isn't working

Comments

@Jasha10
Copy link
Contributor

Jasha10 commented Oct 8, 2022

Description

autoimport sometimes throws ValueError when the import to remove appears twice

Steps to reproduce

Create the following python file:

# tmp.py
import my_module.m
from my_module import _tm

Invoke autoimport:

 $ autoimport tmp.py
Traceback (most recent call last):
  File "/home/rig1/miniconda3/envs/utils/bin/autoimport", line 8, in <module>
    sys.exit(cli())
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/autoimport/entrypoints/cli.py", line 84, in cli
    fixed_code = services.fix_files(flattened_files, config)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/autoimport/services.py", line 29, in fix_files
    fixed_source = fix_code(source, config)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/autoimport/services.py", line 73, in fix_code
    return SourceCode(original_source_code, config=config).fix()
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/autoimport/model.py", line 68, in fix
    self._fix_flake_import_errors()
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/autoimport/model.py", line 288, in _fix_flake_import_errors
    self._remove_unused_imports(import_name)
  File "/home/rig1/miniconda3/envs/utils/lib/python3.10/site-packages/autoimport/model.py", line 454, in _remove_unused_imports
    imports.remove(object_name)
ValueError: list.remove(x): x not in list

Strangely, this bug seems dependent on the exact names of the imports. For example, if I do from my_module import _t instead of from my_module import _tm, no ValueError occurs.

Environment

 $ python -c "import autoimport.version; print(autoimport.version.version_info())"
------------------------------------------------------------------
     autoimport: 1.2.3
     Python: 3.10.5
     Platform: Linux-5.15.0-48-generic-x86_64-with-glibc2.35
------------------------------------------------------------------
@Jasha10 Jasha10 added the bug Something isn't working label Oct 8, 2022
@lyz-code
Copy link
Owner

Weird indeed, only the combination you showed make the error raise. Any other combination of the elements works.

I'll try to take a look whenever I can, as always, if you can jump forward and do a PR, I'll appreciate it a lot :)

lyz-code added a commit that referenced this issue Oct 14, 2022
@lyz-code
Copy link
Owner

Available since 1.3.1

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

Successfully merging a pull request may close this issue.

2 participants