Skip to content

Commit 8fe4bae

Browse files
fix #10 undo the genral py.test hack
add a opt-in add a auto-opt-in for the py.test module
1 parent e54efbf commit 8fe4bae

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/apipkg/__init__.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,32 @@ def __dict__(self):
180180
return dict
181181

182182

183-
def AliasModule(modname, modpath, attrname=None):
183+
def _py_test_hack(modpath, attrname, importerror_alternative):
184+
if (
185+
modpath == "pytest"
186+
and attrname is None
187+
and importerror_alternative is ImportError
188+
):
189+
return None
190+
else:
191+
return importerror_alternative
192+
193+
194+
def _alias_mod_repr(modname, modpath, attrname, importerror_alternative):
195+
x = modpath
196+
if attrname:
197+
x += "." + attrname
198+
if importerror_alternative is not ImportError:
199+
return "<AliasModule {!r} for {!r} alternative {!r}>".format(
200+
modname, x, importerror_alternative
201+
)
202+
else:
203+
return "<AliasModule {!r} for {!r}>".format(modname, x)
204+
205+
206+
def AliasModule(modname, modpath, attrname=None, importerror_alternative=ImportError):
184207
mod = []
208+
importerror_alternative = _py_test_hack(modpath, attrname, importerror_alternative)
185209

186210
def getmod():
187211
if not mod:
@@ -191,18 +215,20 @@ def getmod():
191215
mod.append(x)
192216
return mod[0]
193217

218+
repr_result = _alias_mod_repr(modname, modpath, attrname, importerror_alternative)
219+
194220
class AliasModule(ModuleType):
195221
def __repr__(self):
196-
x = modpath
197-
if attrname:
198-
x += "." + attrname
199-
return "<AliasModule {!r} for {!r}>".format(modname, x)
222+
return repr_result
200223

201224
def __getattribute__(self, name):
202225
try:
203226
return getattr(getmod(), name)
204227
except ImportError:
205-
return None
228+
if importerror_alternative is ImportError:
229+
raise
230+
else:
231+
return importerror_alternative
206232

207233
def __setattr__(self, name, value):
208234
setattr(getmod(), name, value)

test_apipkg.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,35 @@ def test_aliasmodule_aliases_an_attribute():
551551
assert not hasattr(am, "lqkje")
552552

553553

554-
def test_aliasmodule_aliases_unimportable():
554+
def test_aliasmodule_aliases_unimportable_fails():
555555
am = apipkg.AliasModule("mymod", "qlwkejqlwe", "main")
556556
r = repr(am)
557557
assert "<AliasModule 'mymod' for 'qlwkejqlwe.main'>" == r
558+
# this would pass starting with apipkg 1.3 to work around a pytest bug
559+
with pytest.raises(ImportError):
560+
am.qwe is None
561+
562+
563+
def test_aliasmodule_aliases_unimportable_can_return_none():
564+
am = apipkg.AliasModule("mymod", "qlwkejqlwe", "main", None)
565+
r = repr(am)
566+
assert "<AliasModule 'mymod' for 'qlwkejqlwe.main' alternative None>" == r
567+
# this would pass starting with apipkg 1.3 to work around a pytest bug
558568
assert am.qwe is None
559569

560570

571+
def test_aliasmodule_pytest_autoreturn_none_for_hack(monkeypatch):
572+
def error(*k):
573+
raise ImportError(k)
574+
575+
monkeypatch.setattr(apipkg, "importobj", error)
576+
# apipkg 1.3 added this hack
577+
am = apipkg.AliasModule("mymod", "pytest")
578+
r = repr(am)
579+
assert "<AliasModule 'mymod' for 'pytest' alternative None>" == r
580+
assert am.test is None
581+
582+
561583
def test_aliasmodule_unicode():
562584
am = apipkg.AliasModule(u"mymod", "pprint")
563585
assert am

0 commit comments

Comments
 (0)