pass trough annotated exceptions
This commit is contained in:
parent
60e9698530
commit
b825af2e66
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
import re
|
import re
|
||||||
|
import pytest
|
||||||
|
|
||||||
from py.builtin import _basestring
|
from py.builtin import _basestring
|
||||||
|
|
||||||
|
@ -52,17 +53,22 @@ def resolve(name):
|
||||||
raise ImportError(
|
raise ImportError(
|
||||||
'import error in %s: %s' % (used, ex)
|
'import error in %s: %s' % (used, ex)
|
||||||
)
|
)
|
||||||
try:
|
found = annotated_getattr(found, part, used)
|
||||||
found = getattr(found, part)
|
|
||||||
except AttributeError:
|
|
||||||
raise AttributeError(
|
|
||||||
'%r object at %s has no attribute %r' %(
|
|
||||||
type(found).__name__, used, part
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return found
|
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):
|
def derive_importpath(import_path, raising):
|
||||||
if not isinstance(import_path, _basestring) or "." not in import_path:
|
if not isinstance(import_path, _basestring) or "." not in import_path:
|
||||||
raise TypeError("must be absolute import path string, not %r" %
|
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)
|
module, attr = import_path.rsplit('.', 1)
|
||||||
target = resolve(module)
|
target = resolve(module)
|
||||||
if raising:
|
if raising:
|
||||||
getattr(target, attr)
|
annotated_getattr(target, attr, ann=module)
|
||||||
return attr, target
|
return attr, target
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -62,11 +62,11 @@ class TestSetattrWithImportPath:
|
||||||
pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None))
|
pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None))
|
||||||
|
|
||||||
def test_unknown_import(self, monkeypatch):
|
def test_unknown_import(self, monkeypatch):
|
||||||
pytest.raises(pytest.fail.Exception,
|
pytest.raises(ImportError,
|
||||||
lambda: monkeypatch.setattr("unkn123.classx", None))
|
lambda: monkeypatch.setattr("unkn123.classx", None))
|
||||||
|
|
||||||
def test_unknown_attr(self, monkeypatch):
|
def test_unknown_attr(self, monkeypatch):
|
||||||
pytest.raises(pytest.fail.Exception,
|
pytest.raises(AttributeError,
|
||||||
lambda: monkeypatch.setattr("os.path.qweqwe", None))
|
lambda: monkeypatch.setattr("os.path.qweqwe", None))
|
||||||
|
|
||||||
def test_unknown_attr_non_raising(self, monkeypatch):
|
def test_unknown_attr_non_raising(self, monkeypatch):
|
||||||
|
@ -283,7 +283,7 @@ def test_importerror(testdir):
|
||||||
"""))
|
"""))
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines("""
|
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 ""))
|
""".format("'" if sys.version_info > (3, 0) else ""))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue