Repository: pytest-dev/apipkg

Issue:
the `py.test workaround hides legitimate mistakes
c9b997713cb77d2c1334acb7847ee6b24e3261b2 introduced returning None on missing aliasmodules unconditionally
this does hide actual errors

Discussion:

Patch:
diff --git a/src/apipkg/__init__.py b/src/apipkg/__init__.py
index 7981008..350d8c4 100644
--- a/src/apipkg/__init__.py
+++ b/src/apipkg/__init__.py
@@ -191,18 +191,22 @@ def AliasModule(modname, modpath, attrname=None):
             mod.append(x)
         return mod[0]
 
+    x = modpath + ("." + attrname if attrname else "")
+    repr_result = "<AliasModule {!r} for {!r}>".format(modname, x)
+
     class AliasModule(ModuleType):
         def __repr__(self):
-            x = modpath
-            if attrname:
-                x += "." + attrname
-            return "<AliasModule {!r} for {!r}>".format(modname, x)
+            return repr_result
 
         def __getattribute__(self, name):
             try:
                 return getattr(getmod(), name)
             except ImportError:
-                return None
+                if modpath == "pytest" and attrname is None:
+                    # hack for pylibs py.test
+                    return None
+                else:
+                    raise
 
         def __setattr__(self, name, value):
             setattr(getmod(), name, value)


Test patch:
diff --git a/test_apipkg.py b/test_apipkg.py
index 72ec775..58ee2b7 100644
--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -551,11 +551,25 @@ def test_aliasmodule_aliases_an_attribute():
     assert not hasattr(am, "lqkje")
 
 
-def test_aliasmodule_aliases_unimportable():
+def test_aliasmodule_aliases_unimportable_fails():
     am = apipkg.AliasModule("mymod", "qlwkejqlwe", "main")
     r = repr(am)
     assert "<AliasModule 'mymod' for 'qlwkejqlwe.main'>" == r
-    assert am.qwe is None
+    # this would pass starting with apipkg 1.3 to work around a pytest bug
+    with pytest.raises(ImportError):
+        am.qwe is None
+
+
+def test_aliasmodule_pytest_autoreturn_none_for_hack(monkeypatch):
+    def error(*k):
+        raise ImportError(k)
+
+    monkeypatch.setattr(apipkg, "importobj", error)
+    # apipkg 1.3 added this hack
+    am = apipkg.AliasModule("mymod", "pytest")
+    r = repr(am)
+    assert "<AliasModule 'mymod' for 'pytest'>" == r
+    assert am.test is None
 
 
 def test_aliasmodule_unicode():