From c19cdc4e116b1015cafcf10be2f1175c6196d024 Mon Sep 17 00:00:00 2001 From: h Date: Thu, 8 Oct 2026 04:53:54 +0200 Subject: [PATCH] feat(check): make check fails when a service imports another service's sdk at startup --- README.md | 2 +- template/{{ part_dir }}/Makefile.jinja | 3 +- ...rvices %}check_imports.py{% endif %}.jinja | 60 +++++++++++++++++++ ...if layout == 'part' %}CLAUDE.md{% endif %} | 6 ++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 template/{{ part_dir }}/scripts/{% if backend_services %}check_imports.py{% endif %}.jinja diff --git a/README.md b/README.md index 578cb4b..e703e10 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Copier template for a Python backend, either as a `backend/` part next to other parts or alone at the root of the project (`layout`): a uv project with one Docker image and one `python -m ` entrypoint per service (`api`, `bot`, `userbot`, `worker`, `scheduler`), pydantic-settings, rich logging, optional Dishka, MongoDB or PostgreSQL, Redis, taskiq (in-memory, Redis or PostgreSQL broker), CryptoPay, pydantic-ai and a DynamicConfig stored in the database. -Ships `backend/docker-compose.yml` with root-relative paths and profiles per service, `docker-compose.override.yml.example` (published ports and the `caddy` network aliases, copied by `make env`), a `Makefile` with `fmt`, `check`, `build` and service-specific targets, `.env.example` and `CLAUDE.md`. Applied on top of the `infra` template, `layout: parts` or `layout: single` respectively. +Ships `backend/docker-compose.yml` with root-relative paths and profiles per service, `docker-compose.override.yml.example` (published ports and the `caddy` network aliases, copied by `make env`), a `Makefile` with `fmt`, `check` (ruff, ty and `scripts/check_imports.py`, which keeps each service from importing the others' SDKs at startup), `build` and service-specific targets, `.env.example` and `CLAUDE.md`. Applied on top of the `infra` template, `layout: parts` or `layout: single` respectively. `DB__HOST`/`DB__PORT` and `REDIS__HOST`/`REDIS__PORT` say where the service really is. The bundled container takes the host as its network alias and listens on the port, the override publishes it on the machine. For an external database, point them at it and leave its profile out of `COMPOSE_PROFILES`. diff --git a/template/{{ part_dir }}/Makefile.jinja b/template/{{ part_dir }}/Makefile.jinja index 399ae18..8d61d2a 100644 --- a/template/{{ part_dir }}/Makefile.jinja +++ b/template/{{ part_dir }}/Makefile.jinja @@ -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] }} diff --git a/template/{{ part_dir }}/scripts/{% if backend_services %}check_imports.py{% endif %}.jinja b/template/{{ part_dir }}/scripts/{% if backend_services %}check_imports.py{% endif %}.jinja new file mode 100644 index 0000000..ecfc4b0 --- /dev/null +++ b/template/{{ part_dir }}/scripts/{% if backend_services %}check_imports.py{% endif %}.jinja @@ -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()) diff --git a/template/{{ part_dir }}/{% if layout == 'part' %}CLAUDE.md{% endif %} b/template/{{ part_dir }}/{% if layout == 'part' %}CLAUDE.md{% endif %} index 2cabb83..8f02917 100644 --- a/template/{{ part_dir }}/{% if layout == 'part' %}CLAUDE.md{% endif %} +++ b/template/{{ part_dir }}/{% if layout == 'part' %}CLAUDE.md{% endif %} @@ -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("")` 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.