Skip to content

Commit

Permalink
Fix import testing for non .py entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
GFJHogue committed Nov 29, 2024
1 parent 72377e6 commit 25ebcee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
38 changes: 26 additions & 12 deletions .github/actions/verify_imports.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import importlib
import importlib.machinery
import importlib.util
import traceback
from pathlib import Path

# List all entry points here
entry_points = [
"chat-chainlit",
"chat-fastapi",
".embeddings_manager"
]
entry_points: list[Path] = list(
map(
Path("bin").joinpath,
[
"chat-chainlit.py",
"chat-fastapi.py",
"embeddings_manager",
],
)
)

failed_imports = []
failed_imports: list[str] = []

for entry in entry_points:
location: Path
for location in entry_points:
name: str = location.stem
try:
importlib.import_module(entry)
loader = importlib.machinery.SourceFileLoader(name, str(location))
spec = importlib.util.spec_from_loader(name, loader)
if spec is None:
raise ModuleNotFoundError(name)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
except ImportError:
failed_imports.append(entry)
print(f"Failed to import {entry} due to ImportError:")
failed_imports.append(name)
print(f"Failed to import {location} due to ImportError:")
traceback.print_exc()
except Exception as e:
print(f"Non-import error for {entry}: {e}")
print(f"Non-import error for {location}: {e}")
traceback.print_exc()

if failed_imports:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
PYTHONPATH: ./bin:./src
run: |
poetry check
poetry run python -m ./.github/actions/verify_imports.py
poetry run python ./.github/actions/verify_imports.py
docker-build:
runs-on: ubuntu-latest
Expand Down

0 comments on commit 25ebcee

Please sign in to comment.