Skip to content

Commit

Permalink
Merge branch 'main' into restore_on_use
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher authored Jan 19, 2025
2 parents 0e038ad + 2b0da2f commit 5f990d5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion autoimport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from autoimport.main import LazyLoader, lazy

__all__ = ("LazyLoader", "lazy")
__version__ = "0.0.2"
__version__ = "0.0.3"
5 changes: 4 additions & 1 deletion autoimport/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def _load_module(self):
self._loading = True # Set loading flag
try:
self._module = importlib.import_module(self.__name__)
for attr in ["__file__", "__path__", "__package__", "__spec__", "__class__"]:
if hasattr(self._module, attr):
setattr(self, attr, getattr(self._module, attr))
finally:
self._loading = False # Reset loading flag

Expand All @@ -40,7 +43,7 @@ def __dir__(self):

def __repr__(self):
"""Returns a string representation of the LazyLoader module wrapper instance."""
return f"<LazyLoader for '{self.__name__}'>"
return repr(self._module) if self._module is not None else f"<LazyLoader for '{self.__name__}'>"


class lazy:
Expand Down
27 changes: 23 additions & 4 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Tests Directory (`tests/`)

This directory houses the tests for our project, primarily using pytest for testing.
This directory houses the tests for our project, providing two options for running tests: using **pytest** or **unittest**.

## Overview

Expand All @@ -13,9 +13,28 @@ This directory houses the tests for our project, primarily using pytest for test

## Running Tests

To execute the tests:
### Option 1: Using `pytest` (Recommended)

1. **Install pytest:** Ensure pytest is installed in your environment.
2. **Run Tests:** Execute `pytest` in the root directory of the project.
1. **Install pytest:**
```bash
pip install pytest
```
2. **Run Tests:**
```bash
python -m pytest tests -v
```

### Option 2: Using `unittest` (No Additional Dependencies)

If you'd prefer to avoid external dependencies, you can use Python's built-in `unittest` framework:

1. **Run Tests:**
```bash
python -m unittest discover tests -v
```

### Notes
- `pytest` offers more advanced features and better readability, making it the recommended choice.
- `unittest` is built into Python and eliminates the need for additional installations, providing a lightweight alternative.

Make sure to regularly run tests to maintain the reliability and quality of the project.
2 changes: 1 addition & 1 deletion tests/test_autoimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_simple_imports(self):
with lazy():
import numpy as np
random_number = np.random.rand()
self.assertIsInstance(np, LazyLoader)
self.assertNotIsInstance(np, LazyLoader)
self.assertLess(random_number, 1.0)

def test_multiple_imports(self):
Expand Down

0 comments on commit 5f990d5

Please sign in to comment.