From 8ee6d0a8666f91c9c537afacbe3c61f54e342f28 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 26 Nov 2020 14:50:44 +0200 Subject: [PATCH] Always use getfixturemarker() to access _pytestfixturefunction Keep knowledge of how the marker is stored encapsulated in one place. --- src/_pytest/nose.py | 24 +++++++++++++++--------- testing/python/integration.py | 4 +++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/_pytest/nose.py b/src/_pytest/nose.py index bb8f99772..de91af85a 100644 --- a/src/_pytest/nose.py +++ b/src/_pytest/nose.py @@ -2,11 +2,12 @@ from _pytest import python from _pytest import unittest from _pytest.config import hookimpl +from _pytest.fixtures import getfixturemarker from _pytest.nodes import Item @hookimpl(trylast=True) -def pytest_runtest_setup(item): +def pytest_runtest_setup(item) -> None: if is_potential_nosetest(item): if not call_optional(item.obj, "setup"): # Call module level setup if there is no object level one. @@ -15,7 +16,7 @@ def pytest_runtest_setup(item): item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item) -def teardown_nose(item): +def teardown_nose(item) -> None: if is_potential_nosetest(item): if not call_optional(item.obj, "teardown"): call_optional(item.parent.obj, "teardown") @@ -29,11 +30,16 @@ def is_potential_nosetest(item: Item) -> bool: ) -def call_optional(obj, name): +def call_optional(obj: object, name: str) -> bool: method = getattr(obj, name, None) - isfixture = hasattr(method, "_pytestfixturefunction") - if method is not None and not isfixture and callable(method): - # If there's any problems allow the exception to raise rather than - # silently ignoring them. - method() - return True + if method is None: + return False + is_fixture = getfixturemarker(method) is not None + if is_fixture: + return False + if not callable(method): + return False + # If there are any problems allow the exception to raise rather than + # silently ignoring it. + method() + return True diff --git a/testing/python/integration.py b/testing/python/integration.py index 5dce6bdca..8576fcee3 100644 --- a/testing/python/integration.py +++ b/testing/python/integration.py @@ -3,6 +3,7 @@ from typing import Any import pytest from _pytest import runner from _pytest._code import getfslineno +from _pytest.fixtures import getfixturemarker from _pytest.pytester import Pytester @@ -334,7 +335,8 @@ class TestReRunTests: def test_pytestconfig_is_session_scoped() -> None: from _pytest.fixtures import pytestconfig - marker = pytestconfig._pytestfixturefunction # type: ignore + marker = getfixturemarker(pytestconfig) + assert marker is not None assert marker.scope == "session"