Skip to content

Commit

Permalink
Merge pull request #20 from pytest-dev/fix-10-alias-missing-should-fail
Browse files Browse the repository at this point in the history
fix #10 undo the genral py.test hack
  • Loading branch information
RonnyPfannschmidt authored Feb 28, 2021
2 parents e54efbf + bdba5fb commit 4b28704
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/apipkg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,22 @@ def getmod():
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)
Expand Down
18 changes: 16 additions & 2 deletions test_apipkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 4b28704

Please sign in to comment.