Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Ran Benita <ran@unusedvar.com>
  • Loading branch information
RonnyPfannschmidt and bluetech committed May 24, 2021
1 parent d6a4127 commit b8cb4fa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions changelog/8447.deprecation.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Warn when inheritance is used to bring Collectors into Items,
it was never sanely supported and triggers hard to debug errors.
Defining a custom pytest node type which is both an item and a collector now issues a warning.
It was never sanely supported and triggers hard to debug errors.

Instead of inheritance composition should be used.
Instead, a separate collector node should be used, which collects the item. See :ref:`nonpython` for an example.
8 changes: 4 additions & 4 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,15 @@ class Item(Node):

nextitem = None

def __init_subclass__(cls):
def __init_subclass__(cls) -> None:
problems = ", ".join(
base.__name__ for base in cls.__bases__ if issubclass(base, Collector)
)
if problems:
warnings.warn(
f"{cls.__name__} is a Item subclass and should not be a collector.\n"
f"however its bases {problems} are collectors\n"
"please split the collection and the items into 2 node types\n"
f"{cls.__name__} is an Item subclass and should not be a collector, "
f"however its bases {problems} are collectors.\n"
"Please split the Collectors and the Item into separate node types.\n"
"TODO: doc link",
PytestWarning,
)
Expand Down
2 changes: 1 addition & 1 deletion testing/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_node_from_parent_disallowed_arguments() -> None:
nodes.Node.from_parent(None, config=None) # type: ignore[arg-type]


def test_subclassing_node_with_item_warns() -> None:
def test_subclassing_both_item_and_collector_warns() -> None:

with pytest.warns(
PytestWarning, match="SoWrong is a Item subclass and should not be a collector"
Expand Down

0 comments on commit b8cb4fa

Please sign in to comment.