feat(check): make check fails when a service imports another service's sdk at startup
This commit is contained in:
1 parent
97fdafa092
commit
c19cdc4e11
4 files changed
+69
-2
No files matched your search
@@ -30,7 +30,8 @@ check:
|
||||
uv run ruff format --check
|
||||
uv run ruff check
|
||||
uv run ty check
|
||||
{% if svc_modules %}
|
||||
{% if svc_modules %} uv run python scripts/check_imports.py
|
||||
{% endif %}{% if svc_modules %}
|
||||
build:
|
||||
$(COMPOSE) build {{ svc_modules[0] }}
|
||||
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
HEAVY = ["aiogram", "pyrogram", "fastapi", "google.genai"]
|
||||
SERVICES = {
|
||||
{%- if 'api' in backend_services %}
|
||||
"api": (["api.app"], {"fastapi"}),
|
||||
{%- endif %}
|
||||
{%- if 'aiogram_bot' in backend_services %}
|
||||
"bot": ({% if include_payments %}["bot"]{% else %}["bot.common", "bot.handlers"]{% endif %}, {"aiogram"}),
|
||||
{%- endif %}
|
||||
{%- if 'kurigram_userbot' in backend_services %}
|
||||
"userbot": (["userbot.runner", "userbot.handlers"], {"pyrogram"}),
|
||||
{%- endif %}
|
||||
{%- if 'taskiq_worker' in backend_services %}
|
||||
"worker": (["worker.broker", "worker.tasks"], {% if 'api' in backend_services %}{"fastapi"}{% else %}set(){% endif %}),
|
||||
{%- elif 'scheduler' in backend_services %}
|
||||
"scheduler": (["scheduler"], set()),
|
||||
{%- endif %}
|
||||
}
|
||||
PROBE = """
|
||||
import importlib, sys
|
||||
for module in sys.argv[1].split(","):
|
||||
importlib.import_module(module)
|
||||
print(" ".join(name for name in sys.argv[2:] if name in sys.modules))
|
||||
"""
|
||||
{%- if 'aiogram_bot' in backend_services %}
|
||||
ENV = {**os.environ, "BOT__TOKEN": "1:check"}
|
||||
{%- else %}
|
||||
ENV = dict(os.environ)
|
||||
{%- endif %}
|
||||
|
||||
|
||||
def loaded(modules: list[str]) -> tuple[set[str], str]:
|
||||
result = subprocess.run( # noqa: S603
|
||||
[sys.executable, "-c", PROBE, ",".join(modules), *HEAVY],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd="src",
|
||||
env=ENV,
|
||||
check=False,
|
||||
)
|
||||
error = (result.stderr.strip().splitlines() or ["exit code"])[-1]
|
||||
return set(result.stdout.split()) & set(HEAVY), error if result.returncode else ""
|
||||
|
||||
|
||||
def main() -> int:
|
||||
failed = False
|
||||
for service, (modules, allowed) in SERVICES.items():
|
||||
found, error = loaded(modules)
|
||||
extra = sorted(found - allowed)
|
||||
status = error or ("imports " + ", ".join(extra) if extra else "")
|
||||
print(f"{service:10s} {'FAIL: ' + status if status else 'ok'}")
|
||||
failed |= bool(status)
|
||||
return int(failed)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -5,3 +5,9 @@ ruff format
|
||||
ruff check --fix
|
||||
ty check
|
||||
```
|
||||
|
||||
## Imports
|
||||
Every service imports only what it needs to start. `make check` runs `scripts/check_imports.py`, which fails when a service loads a heavy package it is not allowed; `SERVICES` there is the contract, extend it on purpose.
|
||||
- Package `__init__` files don't re-export clients or SDKs.
|
||||
- An SDK used by one function is imported inside it. On an async path load it first with `await preload("<module>")` from `utils.imports`, so the event loop never waits on the import.
|
||||
- Types used in Dishka provider signatures and `FromDishka[...]` stay real top-level imports.
|
||||
Reference in new issue
Block a user