Skip to content

Commit d49e9e5

Browse files
authored
Merge pull request #2014 from RonnyPfannschmidt/warning-record-namedtuple
turn RecordedWarning into a namedtuple
2 parents 377e649 + b3c337d commit d49e9e5

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ Changes
2828
to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR.
2929

3030

31+
* fix `#2013`_: turn RecordedWarning into namedtupe,
32+
to give it a comprehensible repr while preventing unwarranted modification
33+
3134
.. _@davidszotten: https://github.com/davidszotten
3235
.. _@fushi: https://github.com/fushi
3336
.. _@mattduck: https://github.com/mattduck
3437

3538
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
3639
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
3740
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
41+
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
3842

3943

4044
3.0.4.dev

_pytest/recwarn.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
""" recording warnings during test function execution. """
2-
32
import inspect
43

54
import _pytest._code
65
import py
76
import sys
87
import warnings
98
import pytest
9+
from collections import namedtuple
1010

1111

1212
@pytest.yield_fixture
@@ -110,15 +110,10 @@ def warns(expected_warning, *args, **kwargs):
110110
return func(*args[1:], **kwargs)
111111

112112

113-
class RecordedWarning(object):
114-
def __init__(self, message, category, filename, lineno, file, line):
115-
self.message = message
116-
self.category = category
117-
self.filename = filename
118-
self.lineno = lineno
119-
self.file = file
120-
self.line = line
121-
113+
RecordedWarning = namedtuple('RecordedWarning', (
114+
'message', 'category', 'filename', 'lineno', 'file', 'line',
115+
))
116+
122117

123118
class WarningsRecorder(object):
124119
"""A context manager to record raised warnings.

doc/en/recwarn.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Each recorded warning has the attributes ``message``, ``category``,
9292
class of the warning. The ``message`` is the warning itself; calling
9393
``str(message)`` will return the actual message of the warning.
9494

95+
.. note::
96+
:class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1
97+
9598
.. note::
9699
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
97100
differently; see :ref:`ensuring_function_triggers`.

testing/test_recwarn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def test_record(self):
203203
assert len(record) == 1
204204
assert str(record[0].message) == "user"
205205

206+
print(repr(record[0]))
207+
assert str(record[0].message) in repr(record[0])
208+
206209
def test_record_only(self):
207210
with pytest.warns(None) as record:
208211
warnings.warn("user", UserWarning)

0 commit comments

Comments
 (0)