Inline FunctionMixin with Function (#6664)

`Generator` was removed in 7eb28f9eb, and this pleases mypy to correctly
complain that `FunctionMixin` has no `config` (within
`_prunetraceback`).

* typing: _prunetraceback

* minor: imports, typing
This commit is contained in:
Daniel Hahler 2020-02-03 10:25:32 +01:00 committed by GitHub
parent 39b25ddcf3
commit 8ec4d03c91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 47 deletions

View File

@ -21,6 +21,7 @@ import _pytest
from _pytest import fixtures from _pytest import fixtures
from _pytest import nodes from _pytest import nodes
from _pytest._code import filter_traceback from _pytest._code import filter_traceback
from _pytest._code.code import ExceptionInfo
from _pytest.compat import ascii_escaped from _pytest.compat import ascii_escaped
from _pytest.compat import get_default_arg_names from _pytest.compat import get_default_arg_names
from _pytest.compat import get_real_func from _pytest.compat import get_real_func
@ -503,9 +504,7 @@ class Module(nodes.File, PyCollector):
try: try:
mod = self.fspath.pyimport(ensuresyspath=importmode) mod = self.fspath.pyimport(ensuresyspath=importmode)
except SyntaxError: except SyntaxError:
raise self.CollectError( raise self.CollectError(ExceptionInfo.from_current().getrepr(style="short"))
_pytest._code.ExceptionInfo.from_current().getrepr(style="short")
)
except self.fspath.ImportMismatchError: except self.fspath.ImportMismatchError:
e = sys.exc_info()[1] e = sys.exc_info()[1]
raise self.CollectError( raise self.CollectError(
@ -518,8 +517,6 @@ class Module(nodes.File, PyCollector):
"unique basename for your test file modules" % e.args "unique basename for your test file modules" % e.args
) )
except ImportError: except ImportError:
from _pytest._code.code import ExceptionInfo
exc_info = ExceptionInfo.from_current() exc_info = ExceptionInfo.from_current()
if self.config.getoption("verbose") < 2: if self.config.getoption("verbose") < 2:
exc_info.traceback = exc_info.traceback.filter(filter_traceback) exc_info.traceback = exc_info.traceback.filter(filter_traceback)
@ -773,45 +770,6 @@ class Instance(PyCollector):
return self.obj 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): def hasinit(obj):
init = getattr(obj, "__init__", None) init = getattr(obj, "__init__", None)
if init: if init:
@ -1397,7 +1355,7 @@ def write_docstring(tw, doc, indent=" "):
tw.write(indent + line + "\n") 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 """ a Function Item is responsible for setting up and executing a
Python test function. Python test function.
""" """
@ -1501,10 +1459,40 @@ class Function(FunctionMixin, nodes.Item):
""" execute the underlying test function. """ """ execute the underlying test function. """
self.ihook.pytest_pyfunc_call(pyfuncitem=self) self.ihook.pytest_pyfunc_call(pyfuncitem=self)
def setup(self): def setup(self) -> None:
super().setup() if isinstance(self.parent, Instance):
self.parent.newinstance()
self.obj = self._getobj()
fixtures.fillfixtures(self) 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): class FunctionDefinition(Function):
""" """