@@ -927,3 +927,61 @@ def test_excluded_packages(self):
927927 assert "pyspark" not in imports
928928 finally :
929929 os .unlink (temp_path )
930+
931+ def test_local_module_exclusion (self ):
932+ """Test that local modules (files in the same directory) are excluded."""
933+ # Create a temporary directory with multiple Python files
934+ temp_dir = tempfile .mkdtemp ()
935+
936+ try :
937+ # Create a local module file
938+ utility_path = os .path .join (temp_dir , "utility.py" )
939+ with open (utility_path , "w" ) as f :
940+ f .write (textwrap .dedent (
941+ """
942+ def helper_function():
943+ return "helper"
944+ """
945+ ))
946+
947+ # Create another local module
948+ helpers_path = os .path .join (temp_dir , "helpers.py" )
949+ with open (helpers_path , "w" ) as f :
950+ f .write (textwrap .dedent (
951+ """
952+ def another_helper():
953+ return "another"
954+ """
955+ ))
956+
957+ # Create the main script that imports both local modules and external packages
958+ main_content = textwrap .dedent (
959+ """
960+ from utility import helper_function
961+ from helpers import another_helper
962+ import pandas as pd
963+ import numpy as np
964+ """
965+ )
966+ main_path = os .path .join (temp_dir , "main.py" )
967+ with open (main_path , "w" ) as f :
968+ f .write (main_content )
969+
970+ # Scan for imports
971+ imports = scan_file_for_imports (main_path )
972+
973+ # External packages should be included
974+ assert "pandas" in imports
975+ assert "numpy" in imports
976+
977+ # Local modules should be excluded
978+ assert "utility" not in imports
979+ assert "helpers" not in imports
980+
981+ finally :
982+ # Clean up
983+ for file in ["utility.py" , "helpers.py" , "main.py" ]:
984+ file_path = os .path .join (temp_dir , file )
985+ if os .path .exists (file_path ):
986+ os .unlink (file_path )
987+ os .rmdir (temp_dir )
0 commit comments