Skip to content

Commit 3ef2ce7

Browse files
drop general attribute hack and include py.test special case with test
1 parent 8fe4bae commit 3ef2ce7

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

src/apipkg/__init__.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,15 @@ def _py_test_hack(modpath, attrname, importerror_alternative):
191191
return importerror_alternative
192192

193193

194-
def _alias_mod_repr(modname, modpath, attrname, importerror_alternative):
194+
def _alias_mod_repr(modname, modpath, attrname):
195195
x = modpath
196196
if attrname:
197197
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)
198+
return "<AliasModule {!r} for {!r}>".format(modname, x)
204199

205200

206-
def AliasModule(modname, modpath, attrname=None, importerror_alternative=ImportError):
201+
def AliasModule(modname, modpath, attrname=None):
207202
mod = []
208-
importerror_alternative = _py_test_hack(modpath, attrname, importerror_alternative)
209203

210204
def getmod():
211205
if not mod:
@@ -215,7 +209,7 @@ def getmod():
215209
mod.append(x)
216210
return mod[0]
217211

218-
repr_result = _alias_mod_repr(modname, modpath, attrname, importerror_alternative)
212+
repr_result = _alias_mod_repr(modname, modpath, attrname)
219213

220214
class AliasModule(ModuleType):
221215
def __repr__(self):
@@ -225,10 +219,11 @@ def __getattribute__(self, name):
225219
try:
226220
return getattr(getmod(), name)
227221
except ImportError:
228-
if importerror_alternative is ImportError:
229-
raise
222+
if modpath == "pytest" and attrname is None:
223+
# hack for pylibs py.test
224+
return None
230225
else:
231-
return importerror_alternative
226+
raise
232227

233228
def __setattr__(self, name, value):
234229
setattr(getmod(), name, value)

test_apipkg.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -560,14 +560,6 @@ def test_aliasmodule_aliases_unimportable_fails():
560560
am.qwe is None
561561

562562

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
568-
assert am.qwe is None
569-
570-
571563
def test_aliasmodule_pytest_autoreturn_none_for_hack(monkeypatch):
572564
def error(*k):
573565
raise ImportError(k)
@@ -576,7 +568,7 @@ def error(*k):
576568
# apipkg 1.3 added this hack
577569
am = apipkg.AliasModule("mymod", "pytest")
578570
r = repr(am)
579-
assert "<AliasModule 'mymod' for 'pytest' alternative None>" == r
571+
assert "<AliasModule 'mymod' for 'pytest'>" == r
580572
assert am.test is None
581573

582574

0 commit comments

Comments
 (0)