Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turn RecordedWarning into a namedtuple #2014

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ 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

.. _#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
Expand Down
15 changes: 5 additions & 10 deletions _pytest/recwarn.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
""" recording warnings during test function execution. """

import inspect

import _pytest._code
import py
import sys
import warnings
import pytest
from collections import namedtuple


@pytest.yield_fixture
Expand Down Expand Up @@ -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', (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a small side effect here, RecordedWarning attributes are now read-only.

The docs themselves have this to say about it:

Each recorded warning has the attributes message, category, filename, lineno, file, and line. The category is the class of the warning. The message is the warning itself; calling str(message) will return the actual message of the warning.

IMO your change is good and I doubt there are people writing to the attributes, but I thought I would mention this anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this also means this should go to features

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel this way, perhaps you should just add a __repr__ method then? This would solve the issue and could go into master.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, i just reduced the amount of boilerplate to maintain, i wont add it back to add even more

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no problem

'message', 'category', 'filename', 'lineno', 'file', 'line',
))


class WarningsRecorder(object):
"""A context manager to record raised warnings.
Expand Down
3 changes: 3 additions & 0 deletions doc/en/recwarn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 3 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def test_record(self):
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)
Expand Down