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: Interaction between TYPE_CHECKING and star import #242

Closed
Jasha10 opened this issue Jul 10, 2023 · 1 comment
Closed

Bug: Interaction between TYPE_CHECKING and star import #242

Jasha10 opened this issue Jul 10, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@Jasha10
Copy link
Contributor

Jasha10 commented Jul 10, 2023

Description

I've stumbled upon a bug that comes up when using the TYPE_CHECKING guard together with a star import.

Steps to reproduce

Here's a minimal reproducible example:

# repro.py
from typing import TYPE_CHECKING

from module_1 import *

if TYPE_CHECKING:
    import module_2
Click here to see the traceback
$ autoimport repro.py
Traceback (most recent call last):
  File "/home/homestar/tmp/.direnv/python-3.11.3/bin/autoimport", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/autoimport/entrypoints/cli.py", line 94, in cli
    fixed_code = services.fix_files(flattened_files, config)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/autoimport/services.py", line 29, in fix_files
    fixed_source = fix_code(source, config)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/autoimport/services.py", line 73, in fix_code
    return SourceCode(original_source_code, config=config).fix()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/autoimport/model.py", line 67, in fix
    self._fix_flake_import_errors()
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/autoimport/model.py", line 308, in _fix_flake_import_errors
    self._remove_unused_imports(import_name)
  File "/home/homestar/tmp/.direnv/python-3.11.3/lib/python3.11/site-packages/autoimport/model.py", line 455, in _remove_unused_imports
    if re.match(
       ^^^^^^^^^
  File "/home/homestar/.pyenv/versions/3.11.3/lib/python3.11/re/__init__.py", line 166, in match
    return _compile(pattern, flags).match(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/.pyenv/versions/3.11.3/lib/python3.11/re/__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/.pyenv/versions/3.11.3/lib/python3.11/re/_compiler.py", line 743, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/.pyenv/versions/3.11.3/lib/python3.11/re/_parser.py", line 980, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/.pyenv/versions/3.11.3/lib/python3.11/re/_parser.py", line 455, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homestar/.pyenv/versions/3.11.3/lib/python3.11/re/_parser.py", line 685, in _parse
    raise source.error("multiple repeat",
re.error: multiple repeat at position 37

It seems to be an issue with one of Autoimport's regex patterns. The error only occurs if both the star import and the if TYPE_CHECKING block are present. (Edit: see my comment below)

Environment

------------------------------------------------------------------
     autoimport: 1.3.3
     Python: 3.11.3
     Platform: Linux-5.15.0-75-generic-x86_64-with-glibc2.31
------------------------------------------------------------------
@Jasha10 Jasha10 added the bug Something isn't working label Jul 10, 2023
@Jasha10
Copy link
Contributor Author

Jasha10 commented Jul 10, 2023

Oops, looks like I was wrong about the interaction with TYPE_CHECKING. This seems to be an issue with star imports specifically.

@Jasha10 Jasha10 closed this as completed Jul 10, 2023
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

No branches or pull requests

1 participant