Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion .github/workflows/medcat-v2_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@
run:
working-directory: ./medcat-v2
jobs:
build:
base-install-imports:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
# NOTE: using oldest supported python version
- name: Install uv for Python 3.10
uses: astral-sh/setup-uv@v7
with:
python-version: 3.10
enable-cache: true
cache-dependency-glob: "medcat-v2/uv.lock"
- name: Install the project
run: |
uv sync # NO extras
- name: Check that all packages / modules can be imported with default / no-extras install
run: |
uv run python tests/other/check_base_install_can_import_all.py medcat
types-lints-tests-regression:
Comment on lines 15 to 32

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}

Copilot Autofix

AI 1 day ago

To resolve this issue, add a permissions: block specifying the minimum required permissions to either the root of the workflow file or to each job. Since neither job requires write access to the repository or other privileged resources, a root-level block with contents: read is sufficient. This will apply contents: read to all jobs unless overridden. The change should be placed immediately after the name: and before the on: block (commonly, after line 1).

Suggested changeset 1
.github/workflows/medcat-v2_main.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/medcat-v2_main.yml b/.github/workflows/medcat-v2_main.yml
--- a/.github/workflows/medcat-v2_main.yml
+++ b/.github/workflows/medcat-v2_main.yml
@@ -1,4 +1,6 @@
 name: medcat-v2 - Test
+permissions:
+  contents: read
 
 on:
   push:
EOF
@@ -1,4 +1,6 @@
name: medcat-v2 - Test
permissions:
contents: read

on:
push:
Copilot is powered by AI and may make mistakes. Always verify output.
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
9 changes: 7 additions & 2 deletions medcat-v2/medcat/utils/legacy/convert_meta_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import json
import logging

import torch

from medcat.components.addons.meta_cat import MetaCAT, MetaCATAddon
from medcat.components.addons.meta_cat.mctokenizers.tokenizers import (
TokenizerWrapperBase, load_tokenizer)
Expand All @@ -13,6 +11,13 @@

from medcat.utils.legacy.helpers import fix_old_style_cnf

# NOTE: needs to be before torch since default doesn't include torch
from medcat.utils.import_utils import ensure_optional_extras_installed
_EXTRA_NAME = "meta-cat"
ensure_optional_extras_installed("medcat", _EXTRA_NAME)

import torch # noqa


logger = logging.getLogger(__name__)

Expand Down
9 changes: 7 additions & 2 deletions medcat-v2/medcat/utils/legacy/convert_rel_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import json
import logging

import torch

from medcat.cdb import CDB
from medcat.components.addons.relation_extraction.rel_cat import (
RelCAT, RelCATAddon)
Expand All @@ -13,6 +11,13 @@
from medcat.tokenizing.tokenizers import BaseTokenizer, create_tokenizer
from medcat.utils.legacy.helpers import fix_old_style_cnf

# NOTE: needs to be before torch since default doesn't include torch
from medcat.utils.import_utils import ensure_optional_extras_installed
_EXTRA_NAME = "rel-cat"
ensure_optional_extras_installed("medcat", _EXTRA_NAME)

import torch # noqa


logger = logging.getLogger(__name__)

Expand Down
3 changes: 3 additions & 0 deletions medcat-v2/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ dependencies = [ # Optional
"xxhash>=3.5.0,<4.0",
"pydantic>2.0",
"typing-extensions",
"packaging",
"pyyaml",
"requests",
# TODO - others
]

Expand Down
132 changes: 132 additions & 0 deletions medcat-v2/tests/other/check_base_install_can_import_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from typing import Iterable
import os
import subprocess
import sys
import re
from collections import Counter

MISSING_DEP_PATTERN = re.compile(
r"The optional dependency set '([\w\-_]*)' is missing")


def walk_packages(path: str,
base_pkg_name: str,
base_path: str = '') -> Iterable[str]:
if not base_path:
base_path = path
pkg_path = path.removeprefix(base_path).replace(
os.path.sep, '.').strip(".")
pkg_to_here = f"{base_pkg_name}.{pkg_path}" if pkg_path else base_pkg_name
for fn in os.listdir(path):
cur_path = os.path.join(path, fn)
if os.path.isdir(cur_path) and (
not fn.startswith("__") and not fn.endswith("__")):
yield from walk_packages(cur_path, base_pkg_name=base_pkg_name,
base_path=base_path)
continue
if not fn.endswith(".py"):
continue
if fn == "__init__.py":
yield pkg_to_here
continue
yield f"{pkg_to_here}.{fn.removesuffix('.py')}"


def find_all_modules(package_name, package_path=None):
"""Find all importable modules in a package."""
if package_path is None:
# Import the package to get its path
try:
pkg = __import__(package_name)
package_path = pkg.__path__
except ImportError:
print(f"Could not import {package_name}")
return []

modules = []
for modname in walk_packages(package_path[0],
base_pkg_name=package_name):
modules.append(modname)

return modules


def test_import(module_name):
"""Test if a module can be imported in isolation."""
code = f"import {module_name}"
result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
timeout=30,
)
return result.returncode == 0, result.stderr


def get_missing_dep_set(error: str) -> str | None:
err1 = error.strip().split('\n')[-1]
if "MissingDependenciesError" not in err1:
return None
matches = MISSING_DEP_PATTERN.findall(err1)
if len(matches) != 1:
raise ValueError(f"Unknown error:\n'{error}'\nLookin at:\n{err1}"
f"\ngot: {matches}")
return matches[0]


def main():
if len(sys.argv) < 2:
print("Usage: python check_imports.py <package_name>")
sys.exit(1)

package_name = sys.argv[1]

print(f"Finding all modules in {package_name}...")
modules = find_all_modules(package_name)

if not modules:
print(f"No modules found in {package_name}")
sys.exit(1)

print(f"Found {len(modules)} modules. Testing imports...\n")

successful = []
missing_opt_dep_expl = []
failed = []

for module in modules:
success, error = test_import(module)
if success:
successful.append(module)
print(f"✓ {module}")
elif (missing_dep := get_missing_dep_set(error)):
missing_opt_dep_expl.append((module, missing_dep))
print(f"M {module}: missing {missing_dep}")
else:
failed.append((module, error))
print(f"✗ {module}")
# Print the first line of error for quick diagnosis
first_error_line = (
error.strip().split('\n')[-1] if error else "Unknown error")
print(f" → {first_error_line}")

# Summary
print("\n" + "="*60)
per_opt_dep_missing = Counter()
for _, missing_dep in missing_opt_dep_expl:
per_opt_dep_missing[missing_dep] += 1
print(f"Results: {len(successful)} successful, "
f"{len(missing_opt_dep_expl)} missing optional deps "
f"({per_opt_dep_missing}), {len(failed)} failed")
print("="*60)

if failed:
print("\nFailed imports:")
for module, error in failed:
print(f"\n{module}:")
print(error)
sys.exit(1)


if __name__ == "__main__":
main()
Loading