Created
June 12, 2026 10:55
-
-
Save rix0rrr/f298b44ffccdd059b2d6ebaf774d4e11 to your computer and use it in GitHub Desktop.
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
| class LazyLazy(dict): | |
| def __init__(self): | |
| self.factories = {} | |
| self.cache = {} | |
| def register(self, key, factory): | |
| self.factories[key] = factory | |
| def factory(self, key): | |
| def decorator(factory): | |
| self.factories[key] = factory | |
| return factory | |
| return decorator | |
| def __dir__(self): | |
| return dir(self.factories) | |
| def __getitem__(self, key): | |
| existing = self.cache.get(key) | |
| if existing: | |
| return existing | |
| fac = self.factories.get(key) | |
| if fac is None: | |
| raise AttributeError(f"Module has no attribute {key!r}") | |
| ret = fac() | |
| self.cache[key] = ret | |
| return ret | |
| def __contains__(self, key): | |
| return key in self.factories |
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
| import one | |
| import two | |
| print(one.stub_types()) | |
| print(two.stub_types()) | |
| # Prints | |
| # {'x': <class 'one.Homonymous'>} | |
| # {'x': <class 'two.Homonymous'>} |
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
| import typing | |
| import lazy | |
| if typing.TYPE_CHECKING: | |
| class Homonymous: | |
| pass | |
| THIS_MODULE = lazy.LazyLazy() | |
| @THIS_MODULE.factory("Homonymous") | |
| def _build_Homonymous(): | |
| class Homonymous: | |
| def __repr__(self): | |
| return "one.Homonymous" | |
| return Homonymous | |
| @THIS_MODULE.factory("stub") | |
| def _build_stub(): | |
| def stub(x: "Homonymous"): | |
| pass | |
| return stub | |
| @THIS_MODULE.factory("other_member") | |
| def _build_other_member(): | |
| raise RuntimeError('Was not expecting to access other_member! You should not see this.') | |
| @THIS_MODULE.factory("stub_types") | |
| def _build_stub_types(): | |
| def stub_types(): | |
| stub = THIS_MODULE['stub'] | |
| return typing.get_type_hints(stub, globalns=THIS_MODULE, localns=THIS_MODULE) | |
| return stub_types | |
| def __getattr__(key): | |
| return THIS_MODULE[key] |
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
| import typing | |
| import lazy | |
| if typing.TYPE_CHECKING: | |
| class Homonymous: | |
| pass | |
| THIS_MODULE = lazy.LazyLazy() | |
| @THIS_MODULE.factory("Homonymous") | |
| def _build_Homonymous(): | |
| class Homonymous: | |
| def __repr__(self): | |
| return "two.Homonymous" | |
| return Homonymous | |
| @THIS_MODULE.factory("stub") | |
| def _build_stub(): | |
| def stub(x: "Homonymous"): | |
| pass | |
| return stub | |
| @THIS_MODULE.factory("stub_types") | |
| def _build_stub_types(): | |
| def stub_types(): | |
| stub = THIS_MODULE['stub'] | |
| return typing.get_type_hints(stub, globalns=THIS_MODULE, localns=THIS_MODULE) | |
| return stub_types | |
| def __getattr__(key): | |
| return THIS_MODULE[key] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment