Skip to content

Commit

Permalink
Merge pull request #10 from zabertech/10525--ensure-bad-import-yields…
Browse files Browse the repository at this point in the history
…-useful-error

Fix misleading import error
  • Loading branch information
zaberaki committed Nov 24, 2022
2 parents 1a40af5 + 8100f6f commit c8fe725
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
21 changes: 21 additions & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ Update `pyproject.toml` to be a bit more strict about what python versions are b

Tweaked to fix.

## 3.0.20221124

1. When an `izaber.*` module was being imported, if the module did not exist, the error was vague and non-useful. Amend to
to throw a `ModuleNotFoundError` that would allow the developer/user to identify what the issue might be

```python
(izaber-py3.8) root@7ed4086c1bd9:/src/tests# python 300_import_test.py
Traceback (most recent call last):
File "300_import_test.py", line 43, in <module>
test_submodule()
File "300_import_test.py", line 36, in test_submodule
import izaber.nonexistant
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap>", line 618, in _load_backward_compatible
File "/src/izaber/__init__.py", line 28, in load_module
if self.spec.name in sys.modules:
AttributeError: 'NoneType' object has no attribute 'name'
```

2 changes: 2 additions & 0 deletions izaber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def attempt_load(self,prefix,module_name):
for e in target_module.split('.'):
package = "_".join(package_path) or None
spec = importlib.util.find_spec(e,package)
if not spec:
raise ModuleNotFoundError(f"No module named {module_name}")
package_path.append(e)

return IZaberLoaderImportlib(spec,module_name)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "izaber"
description = "Base load point for iZaber code"
version = '3.0.20220929'
version = '3.0.20221124'
authors = ["Aki Mimoto <aki@zaber.com>"]

[build-system]
Expand Down
6 changes: 6 additions & 0 deletions tests/300_import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_submodule():
# Are objects the same?
assert id(DATA1) == id(DATA2)

# Let's import something that doesn't exist
try:
import izaber.nonexistant
except ModuleNotFoundError:
pass # this is the error we wish to see

initialize(config='data/izaber.yaml')

test_submodule()
Expand Down

0 comments on commit c8fe725

Please sign in to comment.