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

Fix TypeError when first item in MarkInfo list of marks is a MarkDecorator instance #3575

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions changelog/3501.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow to insert ``pytest.mark.NAME`` (``MarkDecorator``) objects at the start of an item's marks.
18 changes: 12 additions & 6 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from operator import attrgetter

import attr
import six

from ..deprecated import MARK_PARAMETERSET_UNPACKING, MARK_INFO_ATTRIBUTE
from ..compat import NOTSET, getfslineno, MappingMixin
Expand Down Expand Up @@ -282,12 +283,17 @@ class MarkInfo(object):
""" Marking object created by :class:`MarkDecorator` instances. """

_marks = attr.ib()
combined = attr.ib(
repr=False,
default=attr.Factory(
lambda self: reduce(Mark.combined_with, self._marks), takes_self=True
),
)
combined = attr.ib(repr=False)

@combined.default
def _combined_default(self):
Copy link
Member

Choose a reason for hiding this comment

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

this is ABSOLUTELY not the place not fix - in addition the python version check makes absolutely no sense

_marks should get a validator that requires its a list of mark objects

Copy link
Member Author

Choose a reason for hiding this comment

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

No need to shout. ;)

if six.PY2:
marks = [
(x.mark if isinstance(x, MarkDecorator) else x) for x in self._marks
]
else:
marks = self._marks
return reduce(Mark.combined_with, marks)

name = alias("combined.name", warning=MARK_INFO_ATTRIBUTE)
args = alias("combined.args", warning=MARK_INFO_ATTRIBUTE)
Expand Down
10 changes: 10 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ def g():
assert "reason" not in g.some.kwargs
assert g.some.kwargs["reason2"] == "456"

@ignore_markinfo
def test_pytest_mark_decorator_first_item(self):
"""Check that inserting a MarkDecorator as the first element of MarkInfo works (#3501)"""
from _pytest.mark.structures import MarkInfo, Mark

mark_info = MarkInfo(
[pytest.mark.slow(3), Mark(name="slow", args=(1,), kwargs={})]
)
assert mark_info.args == (3, 1)


def test_marked_class_run_twice(testdir, request):
"""Test fails file is run twice that contains marked class.
Expand Down