From b825af2e661a584b1dac1e6c4758d51cf9072657 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 23 Jan 2016 19:31:17 +0100 Subject: [PATCH] pass trough annotated exceptions --- _pytest/monkeypatch.py | 24 +++++++++++++++--------- testing/test_monkeypatch.py | 6 +++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/_pytest/monkeypatch.py b/_pytest/monkeypatch.py index 74e29d94f..475004539 100644 --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -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 diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py index 7270309a9..048c942c8 100644 --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -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 ""))