Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid getattr when dispatching pytest_runtest_* hooks #6743

Merged
merged 2 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ exclude_lines =
\#\s*pragma: no cover
^\s*raise NotImplementedError\b
^\s*return NotImplemented\b
^\s*assert False(,|$)

^\s*if TYPE_CHECKING:
17 changes: 13 additions & 4 deletions src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

if TYPE_CHECKING:
from typing import Type
from typing_extensions import Literal

#
# pytest plugin hooks
Expand Down Expand Up @@ -181,7 +182,9 @@ def pytest_report_teststatus(report):
# Implementation


def call_and_report(item, when, log=True, **kwds):
def call_and_report(
item, when: "Literal['setup', 'call', 'teardown']", log=True, **kwds
):
call = call_runtest_hook(item, when, **kwds)
hook = item.ihook
report = hook.pytest_runtest_makereport(item=item, call=call)
Expand All @@ -200,9 +203,15 @@ def check_interactive_exception(call, report):
)


def call_runtest_hook(item, when, **kwds):
hookname = "pytest_runtest_" + when
ihook = getattr(item.ihook, hookname)
def call_runtest_hook(item, when: "Literal['setup', 'call', 'teardown']", **kwds):
if when == "setup":
ihook = item.ihook.pytest_runtest_setup
elif when == "call":
ihook = item.ihook.pytest_runtest_call
elif when == "teardown":
ihook = item.ihook.pytest_runtest_teardown
else:
assert False, "Unhandled runtest hook case: {}".format(when)
bluetech marked this conversation as resolved.
Show resolved Hide resolved
reraise = (Exit,) # type: Tuple[Type[BaseException], ...]
if not item.config.getoption("usepdb", False):
reraise += (KeyboardInterrupt,)
Expand Down