Skip to content

Commit 88cbe22

Browse files
authored
Merge pull request #40 from bluetech/no-pycollector
Avoid using `PyCollector` - inherit from `Module` instead
2 parents 2b76f43 + a4618a9 commit 88cbe22

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

pytest_describe/plugin.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import sys
22
import types
3-
from _pytest.python import PyCollector
3+
import pytest
4+
5+
6+
PYTEST_GTE_7_0 = (
7+
hasattr(pytest, 'version_tuple') and pytest.version_tuple >= (7, 0)
8+
)
9+
PYTEST_GTE_5_4 = hasattr(pytest.Collector, 'from_parent')
410

511

612
def trace_function(funcobj, *args, **kwargs):
@@ -55,19 +61,24 @@ def evaluate_shared_behavior(funcobj):
5561
return funcobj._shared_functions
5662

5763

58-
class DescribeBlock(PyCollector):
64+
class DescribeBlock(pytest.Module):
5965
"""Module-like object representing the scope of a describe block"""
6066

6167
@classmethod
6268
def from_parent(cls, parent, obj):
63-
name = obj.__name__
64-
try:
65-
from_parent_super = super().from_parent
66-
except AttributeError: # PyTest < 5.4
67-
self = cls(name, parent)
69+
name = getattr(obj, '_mangled_name', obj.__name__)
70+
nodeid = parent.nodeid + '::' + name
71+
if PYTEST_GTE_7_0:
72+
self = super().from_parent(
73+
parent=parent, path=parent.path, nodeid=nodeid,
74+
)
75+
elif PYTEST_GTE_5_4:
76+
self = super().from_parent(
77+
parent=parent, fspath=parent.fspath, nodeid=nodeid,
78+
)
6879
else:
69-
self = from_parent_super(parent, name=name)
70-
self._name = getattr(obj, '_mangled_name', name)
80+
self = cls(parent=parent, fspath=parent.fspath, nodeid=nodeid)
81+
self.name = name
7182
self.funcobj = obj
7283
return self
7384

@@ -78,10 +89,6 @@ def collect(self):
7889
def _getobj(self):
7990
return self._importtestmodule()
8091

81-
def _makeid(self):
82-
"""Magic that makes fixtures local to each scope"""
83-
return f'{self.parent.nodeid}::{self._name}'
84-
8592
def _importtestmodule(self):
8693
"""Import a describe block as if it was a module"""
8794
module = make_module_from_function(self.funcobj)
@@ -100,7 +107,7 @@ def classnamefilter(self, name):
100107
return False
101108

102109
def __repr__(self):
103-
return f"<{self.__class__.__name__} {self._name!r}>"
110+
return f"<{self.__class__.__name__} {self.name!r}>"
104111

105112

106113
def pytest_pycollect_makeitem(collector, name, obj):

0 commit comments

Comments
 (0)