Skip to content

Commit

Permalink
Do not rely on _makeitem when collecting instances
Browse files Browse the repository at this point in the history
SpecInstance relys on _makeitem to construct any instance, which was
removed in pytest 6.1. Instead of overriding that method, define a
collect method that knows how to recurse.

Fixes bitprophet#12
  • Loading branch information
s-t-e-v-e-n-k committed Mar 10, 2022
1 parent 2f6f83b commit bb373a8
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions pytest_relaxed/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,15 @@ def _getobj(self):
setattr(obj, name, value)
return obj

# Stub for pytest >=3.0,<3.3 where _makeitem did not exist
def makeitem(self, *args, **kwargs):
return self._makeitem(*args, **kwargs)

def _makeitem(self, name, obj):
# More pytestmark skipping.
if name == "pytestmark":
return
# NOTE: no need to modify collect() this time, just mutate item
# creation. TODO: but if collect() is still public, let's move to that
# sometime, if that'll work as well.
superb = super(SpecInstance, self)
attr = "_makeitem" if hasattr(superb, "_makeitem") else "makeitem"
item = getattr(superb, attr)(name, obj)
# Replace any Class objects with SpecClass; this will automatically
# recurse.
# TODO: can we unify this with SpecModule's same bits?
if isinstance(item, Class):
item = SpecClass(item.name, item.parent)
return item
def collect(self):
items = super(SpecInstance, self).collect()
collected = []
for item in items:
# Replace any Class objects with SpecClass, and recurse into it.
if isinstance(item, Class):
cls = SpecClass.from_parent(item.parent, name=item.name)
for item in cls.collect():
collected.append(item)
else:
collected.append(item)
return collected

0 comments on commit bb373a8

Please sign in to comment.