From e9668d75b80057441b53c65c84c9a6351b6c87cb Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 19 Oct 2016 08:57:24 +0200 Subject: [PATCH 1/2] turn RecordedWarning into a namedtuple fixes #2013 --- _pytest/recwarn.py | 15 +++++---------- testing/test_recwarn.py | 3 +++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/_pytest/recwarn.py b/_pytest/recwarn.py index a89474c03..0f6f93571 100644 --- a/_pytest/recwarn.py +++ b/_pytest/recwarn.py @@ -1,5 +1,4 @@ """ recording warnings during test function execution. """ - import inspect import _pytest._code @@ -7,6 +6,7 @@ import py import sys import warnings import pytest +from collections import namedtuple @pytest.yield_fixture @@ -110,15 +110,10 @@ def warns(expected_warning, *args, **kwargs): return func(*args[1:], **kwargs) -class RecordedWarning(object): - def __init__(self, message, category, filename, lineno, file, line): - self.message = message - self.category = category - self.filename = filename - self.lineno = lineno - self.file = file - self.line = line - +RecordedWarning = namedtuple('RecordedWarning', ( + 'message', 'category', 'filename', 'lineno', 'file', 'line', +)) + class WarningsRecorder(object): """A context manager to record raised warnings. diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 87e5846c2..36be5d0d2 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -203,6 +203,9 @@ class TestWarns(object): assert len(record) == 1 assert str(record[0].message) == "user" + print(repr(record[0])) + assert str(record[0].message) in repr(record[0]) + def test_record_only(self): with pytest.warns(None) as record: warnings.warn("user", UserWarning) From b3c337db005770d15f5717b4ec27b5ccbce751a3 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Mon, 24 Oct 2016 15:28:35 +0200 Subject: [PATCH 2/2] add changelog entry and documentation note about RecordedWarning --- CHANGELOG.rst | 4 ++++ doc/en/recwarn.rst | 3 +++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 29fba4ac0..b16f2c2cd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -28,6 +28,9 @@ Changes to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR. +* fix `#2013`_: turn RecordedWarning into namedtupe, + to give it a comprehensible repr while preventing unwarranted modification + .. _@davidszotten: https://github.com/davidszotten .. _@fushi: https://github.com/fushi .. _@mattduck: https://github.com/mattduck @@ -35,6 +38,7 @@ Changes .. _#1512: https://github.com/pytest-dev/pytest/issues/1512 .. _#1874: https://github.com/pytest-dev/pytest/pull/1874 .. _#1952: https://github.com/pytest-dev/pytest/pull/1952 +.. _#2013: https://github.com/pytest-dev/pytest/issues/2013 3.0.4.dev diff --git a/doc/en/recwarn.rst b/doc/en/recwarn.rst index 735006016..3eb333912 100644 --- a/doc/en/recwarn.rst +++ b/doc/en/recwarn.rst @@ -92,6 +92,9 @@ Each recorded warning has the attributes ``message``, ``category``, class of the warning. The ``message`` is the warning itself; calling ``str(message)`` will return the actual message of the warning. +.. note:: + :class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1 + .. note:: ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated differently; see :ref:`ensuring_function_triggers`.