From 7433fa4ec014f2269a08262e5c0d97304f5fad33 Mon Sep 17 00:00:00 2001 From: nstarman Date: Sat, 27 Jan 2024 15:11:27 -0500 Subject: [PATCH 1/6] Add lazy_loader dependency Signed-off-by: nstarman --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 96aff5b4..c32e7953 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,7 @@ "equinox != 0.11.3", "jax", "jaxlib", + "lazy_loader", "typing_extensions", ] From 6cef0116469c670fac3c800d6b21d1e2e73537c3 Mon Sep 17 00:00:00 2001 From: nstarman Date: Sat, 27 Jan 2024 15:25:44 -0500 Subject: [PATCH 2/6] use lazy_loader Signed-off-by: nstarman --- pyproject.toml | 1 + src/galax/__init__.py | 31 +++++++++---------------------- src/galax/__init__.pyi | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 src/galax/__init__.pyi diff --git a/pyproject.toml b/pyproject.toml index c32e7953..d1ad1c09 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -197,6 +197,7 @@ ] "noxfile.py" = ["ERA001", "T20"] "__init__.py" = ["F403"] + "__init__.pyi" = ["F401"] [tool.ruff.lint.isort] diff --git a/src/galax/__init__.py b/src/galax/__init__.py index 4cd7b254..17026e2e 100644 --- a/src/galax/__init__.py +++ b/src/galax/__init__.py @@ -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" +_RUNTIME_TYPECHECKER: str | None +if os.environ.get("GALAX_ENABLE_RUNTIME_TYPECHECKS", "1") == "1": + _RUNTIME_TYPECHECKER = "beartype.beartype" else: - TYPECHECKER = None + _RUNTIME_TYPECHECKER = 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) diff --git a/src/galax/__init__.pyi b/src/galax/__init__.pyi new file mode 100644 index 00000000..4741d38a --- /dev/null +++ b/src/galax/__init__.pyi @@ -0,0 +1,20 @@ +__all__ = [ + "__version__", + # 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 version as __version__ From c01fe3a72c8fa7b6a4de8356bec212647acabf33 Mon Sep 17 00:00:00 2001 From: nstarman Date: Sat, 27 Jan 2024 18:03:22 -0500 Subject: [PATCH 3/6] ignores Signed-off-by: nstarman --- .pre-commit-config.yaml | 3 ++- pyproject.toml | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c3ba4d7a..5a482e33 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -70,6 +70,7 @@ repos: hooks: - id: mypy files: src + exclude: ^src/galax/__init__\.py$ additional_dependencies: - pytest @@ -77,7 +78,7 @@ repos: 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 diff --git a/pyproject.toml b/pyproject.toml index d1ad1c09..2cc34fdc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -124,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"] From 3d5c83ec79ccaa45382f6de4ae14b83ddd7a321e Mon Sep 17 00:00:00 2001 From: nstarman Date: Sat, 27 Jan 2024 18:15:24 -0500 Subject: [PATCH 4/6] fix version Signed-off-by: nstarman --- src/galax/__init__.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/galax/__init__.pyi b/src/galax/__init__.pyi index 4741d38a..816ccac6 100644 --- a/src/galax/__init__.pyi +++ b/src/galax/__init__.pyi @@ -1,5 +1,6 @@ __all__ = [ "__version__", + "__version_tuple__", # modules "dynamics", "integrate", @@ -17,4 +18,7 @@ from . import ( units as units, utils as utils, ) -from ._version import version as __version__ +from ._version import ( # type: ignore[attr-defined] + __version__ as __version__, + __version_tuple__ as __version_tuple__, +) From 4d9485e411ae98269b50c729b833437f512651ed Mon Sep 17 00:00:00 2001 From: nstarman Date: Sat, 27 Jan 2024 18:16:52 -0500 Subject: [PATCH 5/6] Add smoke test Signed-off-by: nstarman --- tests/smoke/test_package.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/smoke/test_package.py b/tests/smoke/test_package.py index f5a6b674..546110b3 100644 --- a/tests/smoke/test_package.py +++ b/tests/smoke/test_package.py @@ -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", + } From f7001b539a139d257d2654b16a488a0353bcf380 Mon Sep 17 00:00:00 2001 From: nstarman Date: Sat, 27 Jan 2024 18:26:35 -0500 Subject: [PATCH 6/6] use ternary Signed-off-by: nstarman --- src/galax/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/galax/__init__.py b/src/galax/__init__.py index 17026e2e..81640878 100644 --- a/src/galax/__init__.py +++ b/src/galax/__init__.py @@ -9,11 +9,11 @@ config.update("jax_enable_x64", True) # noqa: FBT003 -_RUNTIME_TYPECHECKER: str | None -if os.environ.get("GALAX_ENABLE_RUNTIME_TYPECHECKS", "1") == "1": - _RUNTIME_TYPECHECKER = "beartype.beartype" -else: - _RUNTIME_TYPECHECKER = None +_RUNTIME_TYPECHECKER = ( + "beartype.beartype" + if (os.environ.get("GALAX_ENABLE_RUNTIME_TYPECHECKS", "1") == "1") + else None +) with install_import_hook("galax", _RUNTIME_TYPECHECKER): __getattr__, __dir__, __all__ = _attach_stub(__name__, __file__)