Lazy-load concrete renderer classes via PEP 562 __getattr__#64
Merged
Conversation
``renderers/__init__.py`` previously imported every concrete renderer class (``DefaultRenderer``, ``Qwen3Renderer``, …) at module load. Each of those modules imports ``transformers.tokenization_utils.PreTrainedTokenizer`` at the top, so plain ``import renderers`` always pulled ``transformers`` into ``sys.modules``. That blocked light-import callers — most visibly ``prime-rl-configs``'s slim-install path, which only needs the ``RendererConfig`` discriminated union from ``renderers.configs`` (no heavy deps). Defer the concrete-class imports to a module-level ``__getattr__`` (PEP 562). Configs and base symbols stay eager; ``from renderers import DefaultRenderer`` still works but only loads ``renderers.default`` (and hence ``transformers``) on first access. ``create_renderer`` is unaffected — ``renderers.base._populate_registry`` already lazy-imports the concrete classes when a renderer is instantiated. End-user API is unchanged. Full test suite passes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The lazy-loading is a general win for any consumer of the config layer; no need to name a specific downstream. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ApprovabilityVerdict: Approved This PR refactors eager imports to lazy loading using Python's standard PEP 562 getattr pattern. The change is mechanical with no functional difference - renderer classes remain accessible through the same API, just loaded on-demand to avoid unnecessary transformers import overhead. You can customize Macroscope's approvability policy. Learn more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
renderers/__init__.pypreviously imported every concrete renderer module (renderers.default,renderers.qwen3, …) at load time. Each doesfrom transformers.tokenization_utils import PreTrainedTokenizerat the top, so plainimport renderersalways pulledtransformersintosys.modules.prime-rl-configs's slim-install gate, which only needs theRendererConfigdiscriminated union fromrenderers.configs(no heavy deps).__getattr__(PEP 562). Configs and base symbols stay eager;from renderers import DefaultRendererstill works but only loadsrenderers.default(and hencetransformers) on first attribute access.create_rendereris unaffected —renderers.base._populate_registryalready lazy-imports the concrete classes when a renderer is instantiated.Test plan
from renderers import RendererConfigno longer pullstransformersintosys.modulesrenderers.DefaultRenderer(and every other concrete class) still resolves;transformersloads on first accessAttributeErrorwith the standard message🤖 Generated with Claude Code
Note
Low Risk
Single-package import wiring with no logic changes to rendering or registry behavior; public API and
create_rendererpaths stay the same aside from deferred heavy imports.Overview
renderers/__init__.pyno longer eagerly imports every concrete renderer module, so a plainimport renderers(for configs, base types,create_renderer, etc.) does not transitively loadtransformersuntil something actually needs a renderer class.Fifteen top-level renderer imports are replaced with a
_LAZY_RENDERERSname → submodule map and PEP 562__getattr__/__dir__: the firstfrom renderers import DefaultRenderer(orrenderers.Qwen3Renderer, etc.) runsimportlib.import_module, caches the class on the package, and only then pulls in that submodule’stransformersdependency.__all__and the public names stay the same; unknown attributes still raise a normalAttributeError.create_rendererbehavior is unchanged — it still relies onrenderers.base._populate_registry()to import concrete classes when a renderer is instantiated, not on the package__init__eager imports.Reviewed by Cursor Bugbot for commit dc43e11. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Lazy-load concrete renderer classes in
renderers/__init__.pyvia PEP 562__getattr__Replaces eager imports of concrete renderer classes with on-demand loading using a
_LAZY_RENDERERSname-to-module mapping. A module-level__getattr__imports and caches the class on first access;__dir__advertises all lazy names upfront. This reduces import-time overhead for any code that imports from therendererspackage without using every renderer.Macroscope summarized dc43e11.