diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 5e3b5f286..466f260f7 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -21,6 +21,7 @@ import _pytest from _pytest import fixtures from _pytest import nodes from _pytest._code import filter_traceback +from _pytest._code.code import ExceptionInfo from _pytest.compat import ascii_escaped from _pytest.compat import get_default_arg_names from _pytest.compat import get_real_func @@ -503,9 +504,7 @@ class Module(nodes.File, PyCollector): try: mod = self.fspath.pyimport(ensuresyspath=importmode) except SyntaxError: - raise self.CollectError( - _pytest._code.ExceptionInfo.from_current().getrepr(style="short") - ) + raise self.CollectError(ExceptionInfo.from_current().getrepr(style="short")) except self.fspath.ImportMismatchError: e = sys.exc_info()[1] raise self.CollectError( @@ -518,8 +517,6 @@ class Module(nodes.File, PyCollector): "unique basename for your test file modules" % e.args ) except ImportError: - from _pytest._code.code import ExceptionInfo - exc_info = ExceptionInfo.from_current() if self.config.getoption("verbose") < 2: exc_info.traceback = exc_info.traceback.filter(filter_traceback) @@ -773,45 +770,6 @@ class Instance(PyCollector): return self.obj -class FunctionMixin(PyobjMixin): - """ mixin for the code common to Function and Generator. - """ - - def setup(self): - """ perform setup for this test function. """ - if isinstance(self.parent, Instance): - self.parent.newinstance() - self.obj = self._getobj() - - def _prunetraceback(self, excinfo): - if hasattr(self, "_obj") and not self.config.getoption("fulltrace", False): - code = _pytest._code.Code(get_real_func(self.obj)) - path, firstlineno = code.path, code.firstlineno - traceback = excinfo.traceback - ntraceback = traceback.cut(path=path, firstlineno=firstlineno) - if ntraceback == traceback: - ntraceback = ntraceback.cut(path=path) - if ntraceback == traceback: - ntraceback = ntraceback.filter(filter_traceback) - if not ntraceback: - ntraceback = traceback - - excinfo.traceback = ntraceback.filter() - # issue364: mark all but first and last frames to - # only show a single-line message for each frame - if self.config.getoption("tbstyle", "auto") == "auto": - if len(excinfo.traceback) > 2: - for entry in excinfo.traceback[1:-1]: - entry.set_repr_style("short") - - def repr_failure(self, excinfo, outerr=None): - assert outerr is None, "XXX outerr usage is deprecated" - style = self.config.getoption("tbstyle", "auto") - if style == "auto": - style = "long" - return self._repr_failure_py(excinfo, style=style) - - def hasinit(obj): init = getattr(obj, "__init__", None) if init: @@ -1397,7 +1355,7 @@ def write_docstring(tw, doc, indent=" "): tw.write(indent + line + "\n") -class Function(FunctionMixin, nodes.Item): +class Function(PyobjMixin, nodes.Item): """ a Function Item is responsible for setting up and executing a Python test function. """ @@ -1501,10 +1459,40 @@ class Function(FunctionMixin, nodes.Item): """ execute the underlying test function. """ self.ihook.pytest_pyfunc_call(pyfuncitem=self) - def setup(self): - super().setup() + def setup(self) -> None: + if isinstance(self.parent, Instance): + self.parent.newinstance() + self.obj = self._getobj() fixtures.fillfixtures(self) + def _prunetraceback(self, excinfo: ExceptionInfo) -> None: + if hasattr(self, "_obj") and not self.config.getoption("fulltrace", False): + code = _pytest._code.Code(get_real_func(self.obj)) + path, firstlineno = code.path, code.firstlineno + traceback = excinfo.traceback + ntraceback = traceback.cut(path=path, firstlineno=firstlineno) + if ntraceback == traceback: + ntraceback = ntraceback.cut(path=path) + if ntraceback == traceback: + ntraceback = ntraceback.filter(filter_traceback) + if not ntraceback: + ntraceback = traceback + + excinfo.traceback = ntraceback.filter() + # issue364: mark all but first and last frames to + # only show a single-line message for each frame + if self.config.getoption("tbstyle", "auto") == "auto": + if len(excinfo.traceback) > 2: + for entry in excinfo.traceback[1:-1]: + entry.set_repr_style("short") + + def repr_failure(self, excinfo, outerr=None): + assert outerr is None, "XXX outerr usage is deprecated" + style = self.config.getoption("tbstyle", "auto") + if style == "auto": + style = "long" + return self._repr_failure_py(excinfo, style=style) + class FunctionDefinition(Function): """