Skip to content

Commit

Permalink
Merge pull request #140 from fsouza/fix-IndexError-empty-file
Browse files Browse the repository at this point in the history
Avoid IndexError when extracting typing statements
  • Loading branch information
lyz-code committed Oct 15, 2021
2 parents b17ee8e + 1130357 commit c61a4ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/autoimport/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _extract_typing_statements(self, source_lines: List[str]) -> None:
"""
typing_start_line = len(self.header) + len(self.imports)

if len(source_lines) > 0 and re.match(
if typing_start_line < len(source_lines) and re.match(
r"^if TYPE_CHECKING:$", source_lines[typing_start_line]
):
self.typing.append(source_lines[typing_start_line])
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,21 @@ def test_fix_doesnt_fail_on_empty_file() -> None:
result = fix_code(source)

assert result == source


def test_file_that_only_has_unused_imports() -> None:
"""
Given: A file that only has unused imports.
When: Fix code is run.
Then: The output should be a single empty line.
"""
source = dedent(
"""\
import os
import sys
"""
)

result = fix_code(source)

assert result == "\n"

0 comments on commit c61a4ea

Please sign in to comment.