-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add job to check imports (#8594)
* try checking imports * clarify error message * better fmt * do not show complete list of successfully imported packages * refinements * relnote * add missing forward references * better function name * linting * fix linting * Update .github/utils/check_imports.py Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com> --------- Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
129 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
import sys | ||
import importlib | ||
import traceback | ||
from haystack import logging # pylint: disable=unused-import # this is needed to avoid circular imports | ||
|
||
def validate_package_imports(directory: str): | ||
""" | ||
Recursively search for directories with __init__.py and import them. | ||
""" | ||
imported = [] | ||
failed = [] | ||
|
||
sys.path.insert(0, directory) | ||
|
||
for root, _, files in os.walk(directory): | ||
# skip directories without __init__.py | ||
if not '__init__.py' in files: | ||
continue | ||
|
||
init_path = os.path.join(root, '__init__.py') | ||
|
||
# skip haystack/__init__.py to avoid circular imports | ||
if init_path.endswith(f'{directory}/__init__.py'): | ||
continue | ||
|
||
# convert filesystem path to Python module name | ||
relative_path = os.path.relpath(root, directory) | ||
module_name = relative_path.replace(os.path.sep, '.') | ||
if module_name == '.': | ||
module_name = os.path.basename(directory) | ||
|
||
try: | ||
importlib.import_module(module_name) | ||
imported.append(module_name) | ||
except: | ||
failed.append({ | ||
'module': module_name, | ||
'traceback': traceback.format_exc() | ||
}) | ||
|
||
return imported, failed | ||
|
||
|
||
def main(): | ||
""" | ||
This script checks that all Haystack packages can be imported successfully. | ||
This can detect several issues. | ||
One example is forgetting to use a forward reference for a type hint coming | ||
from a lazy import. | ||
""" | ||
print("Checking imports from Haystack packages...") | ||
imported, failed = validate_package_imports(directory="haystack") | ||
|
||
if not imported: | ||
print("\nNO PACKAGES WERE IMPORTED") | ||
sys.exit(1) | ||
|
||
print(f"\nSUCCESSFULLY IMPORTED {len(imported)} PACKAGES") | ||
|
||
if failed: | ||
print(f"\nFAILED TO IMPORT {len(failed)} PACKAGES:") | ||
for fail in failed: | ||
print(f" - {fail['module']}") | ||
|
||
print("\nERRORS:") | ||
for fail in failed: | ||
print(f" - {fail['module']}\n") | ||
print(f" {fail['traceback']}\n\n") | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
enhancements: | ||
- | | ||
Add a testing job to check that all packages can be imported successfully. | ||
This should help detecting several issues, such as forgetting to use a | ||
forward reference for a type hint coming from a lazy import. |
This file was deleted.
Oops, something went wrong.