Skip to content

Add register module in lazy manner#6

Merged
DevTGHa merged 5 commits intodevfrom
add-lazy-module-register
Feb 25, 2026
Merged

Add register module in lazy manner#6
DevTGHa merged 5 commits intodevfrom
add-lazy-module-register

Conversation

@DevTGHa
Copy link
Collaborator

@DevTGHa DevTGHa commented Feb 20, 2026

Summary

  • Adds lazy module registration to D3Session and D3AsyncSession: modules decorated with @d3function are now registered on first use inside execute() / rpc(), rather than requiring upfront declaration in context_modules.
  • Tracks registered modules in a new registered_modules set on the session base class, preventing duplicate registration calls.
  • Changes context_modules parameter type from list[str] to set[str] for correct deduplication semantics.

Before

@d3function("mymodule")
def my_time() -> str:
    return "hello world"

# mymodule needs to be passed to register on entering context
async with D3AsyncSession('localhost', 80, ["mymodule"]) as session:
    await session.rpc(rename_surface.payload("surface 1", "surface 2"))

After

@d3function("mymodule")
def my_time() -> str:
    return "hello world"

# no registration needed on entering context
async with D3AsyncSession('localhost', 80) as session:
    # mymodule will be lazily registered on first call
    await session.rpc(rename_surface.payload("surface 1", "surface 2"))

# if user want to register on entering context, it's still possible
async with D3AsyncSession('localhost', 80, {"mymodule"}) as session:
    await session.rpc(rename_surface.payload("surface 1", "surface 2"))

Motivation

Previously, callers had to know and list every module they intended to use when constructing a session. With lazy registration, modules are registered transparently on demand, reducing boilerplate and avoiding unnecessary network calls for modules that may not be used in a given session.

Changes

File Change
session.py D3SessionBase: added registered_modules: set[str]; execute() on both sync and async sessions now lazily registers unknown modules; register_module() records successfully registered modules
CHANGELOG.md Added lazy registration and registered_modules entries to [1.3.0]
README.md Removed context_modules from Functional API examples; updated prose to describe lazy registration behaviour

@DevTGHa DevTGHa self-assigned this Feb 20, 2026
@DevTGHa DevTGHa added the enhancement New feature or request label Feb 20, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-lazy-module-register

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

self.port: int = port
self.context_modules: list[str] = context_modules
self.context_modules: set[str] = context_modules
self.registered_modules: set[str] = set()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cache registered_modules in D3SessionBase so we can register module on demand in lazy manner.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between context_modules and registered_modules?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context_modules is modules that will get registered when entering context.
register_module is modules that is actually registered by the session.

@DevTGHa DevTGHa merged commit 2b03a13 into dev Feb 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

3 participants