File tree 2 files changed +19
-9
lines changed
2 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -588,15 +588,18 @@ def find_imports_in_file(
588
588
- **top_only**: If True, only return imports that occur before the first function or class.
589
589
- ****config_kwargs**: Any config modifications.
590
590
"""
591
- with io .File .read (filename ) as source_file :
592
- yield from find_imports_in_stream (
593
- input_stream = source_file .stream ,
594
- config = config ,
595
- file_path = file_path or source_file .path ,
596
- unique = unique ,
597
- top_only = top_only ,
598
- ** config_kwargs ,
599
- )
591
+ try :
592
+ with io .File .read (filename ) as source_file :
593
+ yield from find_imports_in_stream (
594
+ input_stream = source_file .stream ,
595
+ config = config ,
596
+ file_path = file_path or source_file .path ,
597
+ unique = unique ,
598
+ top_only = top_only ,
599
+ ** config_kwargs ,
600
+ )
601
+ except OSError as error :
602
+ warn (f"Unable to parse file { filename } due to { error } " )
600
603
601
604
602
605
def find_imports_in_paths (
Original file line number Diff line number Diff line change @@ -94,6 +94,13 @@ def test_find_imports_in_file(imperfect):
94
94
assert "b" in [found_import .module for found_import in found_imports ]
95
95
96
96
97
+ def test_find_imports_in_file_error (tmpdir ):
98
+ broken_link = tmpdir .join ("broken_link.py" )
99
+ broken_link .mksymlinkto ("not-exist" )
100
+ with pytest .warns (UserWarning ):
101
+ assert not list (api .find_imports_in_file (broken_link ))
102
+
103
+
97
104
def test_find_imports_in_code ():
98
105
code = """
99
106
from x.y import z as a
You can’t perform that action at this time.
0 commit comments