Skip to content

Commit e54efbf

Browse files
Merge pull request #19 from pytest-dev/modernize
fix #13: pass over __spec__ and test aliases/specs
2 parents 8da14ac + 48445de commit e54efbf

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/apipkg/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def initpkg(pkgname, exportdefs, attr=None, eager=False):
5656
d["__package__"] = oldmod.__package__
5757
if "__doc__" not in exportdefs and getattr(oldmod, "__doc__", None):
5858
d["__doc__"] = oldmod.__doc__
59+
d["__spec__"] = getattr(oldmod, "__spec__", None)
5960
d.update(attr)
6061
if hasattr(oldmod, "__dict__"):
6162
oldmod.__dict__.update(d)
@@ -66,6 +67,7 @@ def initpkg(pkgname, exportdefs, attr=None, eager=False):
6667
for module in list(sys.modules.values()):
6768
if isinstance(module, ApiModule):
6869
module.__dict__
70+
return mod
6971

7072

7173
def importobj(modpath, attrname):

test_apipkg.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,45 @@ def test_eagerload_on_bython(monkeypatch):
668668
apipkg.initpkg(
669669
"apipkg.testmodule.example.lazy", {"test": "apipkg.does_not_exist"}
670670
)
671+
672+
673+
@pytest.fixture
674+
def find_spec():
675+
try:
676+
from importlib.util import find_spec
677+
except ImportError:
678+
pytest.xfail("no importlib")
679+
return find_spec
680+
681+
682+
def test_importlib_find_spec_fake_module(find_spec):
683+
mod = apipkg.initpkg("apipkg.testmodule.example.missing", {})
684+
with pytest.raises(ValueError, match=mod.__name__ + r"\.__spec__ is None"):
685+
find_spec(mod.__name__)
686+
687+
688+
def test_importlib_find_spec_aliasmodule(find_spec):
689+
am = apipkg.AliasModule("apipkg.testmodule.example.email_spec", "email")
690+
spec = find_spec(am.__name__)
691+
assert spec is am.__spec__
692+
693+
694+
def test_importlib_find_spec_initpkg(find_spec, tmpdir, monkeypatch):
695+
696+
modname = "apipkg_test_example_initpkg_findspec"
697+
698+
pkgdir = tmpdir.mkdir("apipkg_test_example_initpkg_findspec")
699+
pkgdir.ensure("__init__.py").write(
700+
textwrap.dedent(
701+
"""
702+
import apipkg
703+
apipkg.initpkg(__name__, {
704+
'email': 'email',
705+
})
706+
"""
707+
)
708+
)
709+
710+
monkeypatch.syspath_prepend(tmpdir)
711+
find_spec(modname)
712+
find_spec(modname + ".email")

0 commit comments

Comments
 (0)