Skip to content

Fix ImportErrors not being raised #150

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

Merged
merged 3 commits into from
May 21, 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
10 changes: 10 additions & 0 deletions mendeleev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

__version__ = "0.16.1"

import importlib

from .mendeleev import * # noqa F403
from .models import * # noqa F403

Expand Down Expand Up @@ -150,3 +152,11 @@ def __getattr__(name):
element_obj = element(name) # noqa: F405
globals()[name] = element_obj
return element_obj

# attempt to import the module if it's not an element symbol
try:
module = importlib.import_module(name)
globals()[name] = module
return module
except ModuleNotFoundError as e:
raise AttributeError(f"module 'mendeleev' has no attribute '{name}'") from e
46 changes: 46 additions & 0 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest


def test_from_import():
"""Test that the main imports work"""
from mendeleev import fetch

assert fetch is not None
from mendeleev import vis

assert vis is not None
from mendeleev import db

assert db is not None
from mendeleev import ion

assert ion is not None
from mendeleev import models

assert models is not None
from mendeleev import mendeleev

assert mendeleev is not None


def test_direct_import():
"""Test that the main imports work"""
import mendeleev.db # noqa: F401
import mendeleev.fetch # noqa: F401
import mendeleev.vis # noqa: F401
import mendeleev.ion # noqa: F401
import mendeleev.mendeleev # noqa: F401
import mendeleev.models # noqa: F401


def test_missing_import():
"""Test that the import of a non-existing module raises ImportError"""
with pytest.raises(ImportError):
from mendeleev import nonexisting # noqa: F401


def test_element_import():
"""Test that the element instances can be imported"""
from mendeleev import C

assert C is not None
Loading