Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,18 @@ def find_imports_in_file(
- **top_only**: If True, only return imports that occur before the first function or class.
- ****config_kwargs**: Any config modifications.
"""
with io.File.read(filename) as source_file:
yield from find_imports_in_stream(
input_stream=source_file.stream,
config=config,
file_path=file_path or source_file.path,
unique=unique,
top_only=top_only,
**config_kwargs,
)
try:
with io.File.read(filename) as source_file:
yield from find_imports_in_stream(
input_stream=source_file.stream,
config=config,
file_path=file_path or source_file.path,
unique=unique,
top_only=top_only,
**config_kwargs,
)
except OSError as error:
warn(f"Unable to parse file {filename} due to {error}")


def find_imports_in_paths(
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def test_find_imports_in_file(imperfect):
assert "b" in [found_import.module for found_import in found_imports]


def test_find_imports_in_file_error(tmpdir):
broken_link = tmpdir.join("broken_link.py")
broken_link.mksymlinkto("not-exist")
with pytest.warns(UserWarning):
assert not list(api.find_imports_in_file(broken_link))


def test_find_imports_in_code():
code = """
from x.y import z as a
Expand Down