Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy submodule imports #251

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions optype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from importlib import metadata as _metadata
import importlib as _importlib
import importlib.metadata as _metadata

from . import copy, dataclasses, inspect, json, pickle, string, types, typing
from ._core._can import (
CanAEnter,
CanAEnterSelf,
Expand Down Expand Up @@ -315,6 +315,13 @@
)


__version__: str = _metadata.version(__package__ or __file__.split("/")[-1])


def __c(s: str, k: str, /) -> str:
return "".join(chr(ord(c) ^ ord(k)) for c in s)


__all__ = [
"CanAEnter",
"CanAEnterSelf",
Expand Down Expand Up @@ -539,8 +546,6 @@
"HasTypeParams",
"HasWrapped",
"__version__",
"copy",
"dataclasses",
"do_abs",
"do_add",
"do_aiter",
Expand Down Expand Up @@ -623,20 +628,37 @@
"do_truediv",
"do_trunc",
"do_xor",
"inspect",
"json",
"pickle",
"rick",
"string",
"types",
"typing",
]

__version__: str = _metadata.version(__package__ or __file__.split("/")[-1])

_submodules = {
"copy": "copy",
"dataclasses": "dataclasses",
"dlpack": "dlpack",
"inspect": "inspect",
"json": "json",
"numpy": "numpy",
"pickle": "pickle",
"string": "string",
"types": "types",
"typing": "typing",
}
__all__ += list(_submodules) # pyright: ignore[reportUnsupportedDunderAll]

# stop digging for hidden layers and be impressed
rick = pickle
_submodules[__c("🦞🦅🦏🦇", "🧬")] = __c("🤢🤻🤱🤹🤾🤷", "🥒")


def __dir__() -> list[str]:
return __all__


def __getattr__(name: str) -> object:
if submodule := _submodules.get(name):
return _importlib.import_module(f"{__name__}.{submodule}")
try:
return globals()[name] # pyright: ignore[reportAny]
except KeyError:
msg = f"module '{__name__}' has no attribute '{name}'"
module = _importlib.import_module(__name__)
raise AttributeError(msg, name=name, obj=module) from None
Loading
Loading