File: test_module_dir.py

package info (click to toggle)
scikit-build-core 0.11.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,408 kB
  • sloc: python: 13,380; ansic: 140; cpp: 134; sh: 27; fortran: 18; makefile: 7
file content (61 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import annotations

import importlib
import importlib.machinery
import importlib.util
import inspect
import pkgutil
from pathlib import Path
from typing import Generator


def on_all_modules(
    name: str, base_path: Path | None = None, *, pkg: bool
) -> Generator[str, None, None]:
    if base_path is None:
        base_module = importlib.import_module(name)
        base_path = Path(inspect.getfile(base_module)).parent
    for module_info in pkgutil.iter_modules([str(base_path)]):
        package_name = f"{name}.{module_info.name}"
        if module_info.ispkg:
            if pkg:
                yield package_name
            yield from on_all_modules(
                package_name, base_path / module_info.name, pkg=pkg
            )
        else:
            yield package_name


def test_all_modules_filter_all():
    all_modules = on_all_modules("scikit_build_core", pkg=False)
    all_modules = (n for n in all_modules if not n.split(".")[-1].startswith("__"))
    for name in all_modules:
        try:
            module = importlib.import_module(name)
        except ModuleNotFoundError:
            continue

        try:
            dir_module = set(dir(module))
        except Exception:
            print(f"dir() failed on {name}")
            raise
        items = ["annotations", "os", "sys"]
        for item in items:
            assert item not in dir_module, f"{module.__file__} has {item!r}"


def test_all_modules_has_all():
    all_modules = on_all_modules("scikit_build_core", pkg=True)
    all_modules = (n for n in all_modules if not n.split(".")[-1].startswith("_"))
    for name in all_modules:
        try:
            module = importlib.import_module(name)
        except ModuleNotFoundError:
            continue

        dir_module = module.__dict__
        items = ["__all__"]
        for item in items:
            assert item in dir_module, f"{module.__file__} missing {item!r}"