Skip to content

feat: add print_debug_info function #2913

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions changes/2913.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a `print_debug_info` function for bug reports.
49 changes: 49 additions & 0 deletions src/zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,54 @@
# in case setuptools scm screw up and find version to be 0.0.0
assert not __version__.startswith("0.0.0")


def print_debug_info() -> None:
"""
Print version info for use in bug reports.
"""
import platform
from importlib.metadata import version

Check warning on line 46 in src/zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/__init__.py#L45-L46

Added lines #L45 - L46 were not covered by tests

def print_packages(packages: list[str]) -> None:
not_installed = []
for package in packages:
try:
print(f"{package}: {version(package)}")
except ModuleNotFoundError:
not_installed.append(package)
if not_installed:
print("\n**Not Installed:**")
for package in not_installed:
print(package)

Check warning on line 58 in src/zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/__init__.py#L48-L58

Added lines #L48 - L58 were not covered by tests

required = [

Check warning on line 60 in src/zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/__init__.py#L60

Added line #L60 was not covered by tests
"packaging",
"numpy",
"numcodecs",
"typing_extensions",
"donfig",
]
optional = [

Check warning on line 67 in src/zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/__init__.py#L67

Added line #L67 was not covered by tests
"botocore",
"cupy-cuda12x",
"fsspec",
"numcodecs",
"s3fs",
"gcsfs",
"universal-pathlib",
"rich",
"obstore",
]

print(f"platform: {platform.platform()}")
print(f"python: {platform.python_version()}")
print(f"zarr: {__version__}\n")
print("**Required dependencies:**")
print_packages(required)
print("\n**Optional dependencies:**")
print_packages(optional)

Check warning on line 85 in src/zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/__init__.py#L79-L85

Added lines #L79 - L85 were not covered by tests


__all__ = [
"Array",
"AsyncArray",
Expand Down Expand Up @@ -67,6 +115,7 @@
"open_consolidated",
"open_group",
"open_like",
"print_debug_info",
"save",
"save_array",
"save_group",
Expand Down
18 changes: 18 additions & 0 deletions tests/test_zarr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import zarr


Expand All @@ -9,3 +11,19 @@ def test_exports() -> None:

for export in __all__:
getattr(zarr, export)


def test_print_debug_info(capsys: pytest.CaptureFixture[str]) -> None:
"""
Ensure that print_debug_info does not raise an error
"""
from importlib.metadata import version

from zarr import __version__, print_debug_info

print_debug_info()
Copy link
Member

Choose a reason for hiding this comment

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

to make this test a bit more meaningful, let's capture the output and assert that it matches our basic expectations. https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html#accessing-captured-output-from-a-test-function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested two outputs that should always be there. I didn't test every output because at some point that just becomes rewriting the fucntion

captured = capsys.readouterr()
# test that at least some of what we expect is
# printed out
assert f"zarr: {__version__}" in captured.out
assert f"numpy: {version('numpy')}" in captured.out