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 loader #99

Merged
merged 6 commits into from
Jan 27, 2024
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
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ repos:
hooks:
- id: mypy
files: src
exclude: ^src/galax/__init__\.py$
additional_dependencies:
- pytest

- repo: https://github.com/codespell-project/codespell
rev: "v2.2.6"
hooks:
- id: codespell
exclude: ^(?!.*\/notebooks\/)(?!.AUTHORS\.rst$).+$
exclude: ^(notebooks|AUTHORS\.rst)$

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.16
Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"equinox != 0.11.3",
"jax",
"jaxlib",
"lazy_loader",
"typing_extensions",
]

Expand Down Expand Up @@ -123,7 +124,11 @@
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
exclude = '''(^|/)tests/|(^/)docs/|(^/)conftest\.py'''
exclude = [
'(^/)docs/', # docs
'(^|/)tests/', # tests
'^conftest\.py$', # nox test configuration
]

enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]

Expand Down Expand Up @@ -196,6 +201,7 @@
]
"noxfile.py" = ["ERA001", "T20"]
"__init__.py" = ["F403"]
"__init__.pyi" = ["F401"]


[tool.ruff.lint.isort]
Expand Down
33 changes: 10 additions & 23 deletions src/galax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
"""Copyright (c) 2023 galax maintainers. All rights reserved."""
# ruff:noqa: F401

__all__ = [
"__version__",
# modules
"units",
"potential",
"integrate",
"dynamics",
"utils",
"typing",
]

import os

from jax import config
from jaxtyping import install_import_hook

from ._version import version as __version__
from lazy_loader import attach_stub as _attach_stub

config.update("jax_enable_x64", True) # noqa: FBT003

TYPECHECKER: str | None
if os.environ.get("GALDYNAMIX_ENABLE_RUNTIME_TYPECHECKS", "1") == "1":
TYPECHECKER = "beartype.beartype"
else:
TYPECHECKER = None
_RUNTIME_TYPECHECKER = (
"beartype.beartype"
if (os.environ.get("GALAX_ENABLE_RUNTIME_TYPECHECKS", "1") == "1")
else None
)

with install_import_hook("galax", TYPECHECKER):
from galax import dynamics, integrate, potential, typing, units, utils
with install_import_hook("galax", _RUNTIME_TYPECHECKER):
__getattr__, __dir__, __all__ = _attach_stub(__name__, __file__)


# Clean up the namespace
for name in set(dir()) - set(__all__):
del globals()[name]
# Install the runtime typechecker
install_import_hook("galax", _RUNTIME_TYPECHECKER)
24 changes: 24 additions & 0 deletions src/galax/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
__all__ = [
"__version__",
"__version_tuple__",
# modules
"dynamics",
"integrate",
"potential",
"typing",
"units",
"utils",
]

from . import (
dynamics as dynamics,
integrate as integrate,
potential as potential,
typing as typing,
units as units,
utils as utils,
)
from ._version import ( # type: ignore[attr-defined]
__version__ as __version__,
__version_tuple__ as __version_tuple__,
)
18 changes: 17 additions & 1 deletion tests/smoke/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@
import galax as pkg


def test_version():
def test_version() -> None:
assert version("galax") == pkg.__version__


def test_all() -> None:
"""Test the `galax` package contents."""
# Test detailed contents (not order)
assert set(pkg.__all__) == {
"__version__",
"__version_tuple__",
# modules
"dynamics",
"integrate",
"potential",
"typing",
"units",
"utils",
}
Loading