Skip to content

Commit 25ebcee

Browse files
committed
Fix import testing for non .py entrypoints
1 parent 72377e6 commit 25ebcee

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

.github/actions/verify_imports.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
1-
import importlib
1+
import importlib.machinery
2+
import importlib.util
23
import traceback
4+
from pathlib import Path
35

46
# List all entry points here
5-
entry_points = [
6-
"chat-chainlit",
7-
"chat-fastapi",
8-
".embeddings_manager"
9-
]
7+
entry_points: list[Path] = list(
8+
map(
9+
Path("bin").joinpath,
10+
[
11+
"chat-chainlit.py",
12+
"chat-fastapi.py",
13+
"embeddings_manager",
14+
],
15+
)
16+
)
1017

11-
failed_imports = []
18+
failed_imports: list[str] = []
1219

13-
for entry in entry_points:
20+
location: Path
21+
for location in entry_points:
22+
name: str = location.stem
1423
try:
15-
importlib.import_module(entry)
24+
loader = importlib.machinery.SourceFileLoader(name, str(location))
25+
spec = importlib.util.spec_from_loader(name, loader)
26+
if spec is None:
27+
raise ModuleNotFoundError(name)
28+
module = importlib.util.module_from_spec(spec)
29+
loader.exec_module(module)
1630
except ImportError:
17-
failed_imports.append(entry)
18-
print(f"Failed to import {entry} due to ImportError:")
31+
failed_imports.append(name)
32+
print(f"Failed to import {location} due to ImportError:")
1933
traceback.print_exc()
2034
except Exception as e:
21-
print(f"Non-import error for {entry}: {e}")
35+
print(f"Non-import error for {location}: {e}")
2236
traceback.print_exc()
2337

2438
if failed_imports:

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
PYTHONPATH: ./bin:./src
5757
run: |
5858
poetry check
59-
poetry run python -m ./.github/actions/verify_imports.py
59+
poetry run python ./.github/actions/verify_imports.py
6060
6161
docker-build:
6262
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)