pass trough annotated exceptions

This commit is contained in:
Ronny Pfannschmidt 2016-01-23 19:31:17 +01:00
parent 60e9698530
commit b825af2e66
2 changed files with 18 additions and 12 deletions

View File

@ -2,6 +2,7 @@
import os, sys
import re
import pytest
from py.builtin import _basestring
@ -52,17 +53,22 @@ def resolve(name):
raise ImportError(
'import error in %s: %s' % (used, ex)
)
try:
found = getattr(found, part)
except AttributeError:
raise AttributeError(
'%r object at %s has no attribute %r' %(
type(found).__name__, used, part
)
)
found = annotated_getattr(found, part, used)
return found
def annotated_getattr(obj, name, ann):
try:
obj = getattr(obj, name)
except AttributeError:
raise AttributeError(
'%r object at %s has no attribute %r' % (
type(obj).__name__, ann, name
)
)
return obj
def derive_importpath(import_path, raising):
if not isinstance(import_path, _basestring) or "." not in import_path:
raise TypeError("must be absolute import path string, not %r" %
@ -70,7 +76,7 @@ def derive_importpath(import_path, raising):
module, attr = import_path.rsplit('.', 1)
target = resolve(module)
if raising:
getattr(target, attr)
annotated_getattr(target, attr, ann=module)
return attr, target

View File

@ -62,11 +62,11 @@ class TestSetattrWithImportPath:
pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None))
def test_unknown_import(self, monkeypatch):
pytest.raises(pytest.fail.Exception,
pytest.raises(ImportError,
lambda: monkeypatch.setattr("unkn123.classx", None))
def test_unknown_attr(self, monkeypatch):
pytest.raises(pytest.fail.Exception,
pytest.raises(AttributeError,
lambda: monkeypatch.setattr("os.path.qweqwe", None))
def test_unknown_attr_non_raising(self, monkeypatch):
@ -283,7 +283,7 @@ def test_importerror(testdir):
"""))
result = testdir.runpytest()
result.stdout.fnmatch_lines("""
*import error in package.a.x: No module named {0}doesnotexist{0}*
*import error in package.a: No module named {0}doesnotexist{0}*
""".format("'" if sys.version_info > (3, 0) else ""))