Skip to content

Option to disable runtime codegen; pydantic models only #418

@magicmark

Description

@magicmark

Would be nice to avoid requiring httpx imports for use cases where only the types are required, and not the runtime client.

FWIW this can be approximated with this plugin that opus4.6 kindly generated:

Details
from __future__ import annotations

import ast

from ariadne_codegen.plugins.base import Plugin  # type: ignore[import-not-found]  # build-time only dep

# Modules that contain runtime client code (httpx dependency chain).
# These are stripped from __init__.py and the corresponding files are
# deleted by the Makefile after codegen.
_RUNTIME_MODULES = frozenset({"client", "async_base_client", "exceptions"})


class PruneClientPlugin(Plugin):
    """ariadne-codegen plugin that removes runtime client code from generated output.

    Keeps only Pydantic model classes (result types, input types, enums, base_model).
    Removes the httpx-based async client, generated Client class, and exceptions.
    """

    def generate_client_code(self, generated_code: str) -> str:
        return ""

    def generate_init_module(self, module: ast.Module) -> ast.Module:
        pruned_names: set[str] = set()
        new_body: list[ast.stmt] = []

        for node in module.body:
            if isinstance(node, ast.ImportFrom) and node.module in _RUNTIME_MODULES:
                for alias in node.names or []:
                    pruned_names.add(alias.asname or alias.name)
                continue
            new_body.append(node)

        for node in new_body:
            if (
                isinstance(node, ast.Assign)
                and len(node.targets) == 1
                and isinstance(node.targets[0], ast.Name)
                and node.targets[0].id == "__all__"
                and isinstance(node.value, ast.List)
            ):
                node.value.elts = [
                    elt
                    for elt in node.value.elts
                    if not (isinstance(elt, ast.Constant) and elt.value in pruned_names)
                ]

        module.body = new_body
        return module

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions