From 3512997e76c33861aed5dfc8df6958cb815636c1 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 20 Jun 2024 16:39:39 +0200 Subject: [PATCH] refactor: replace attribute check with type check --- src/_pytest/fixtures.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 7e81b4293..d8024800e 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -153,10 +153,9 @@ def get_scope_node(node: nodes.Node, scope: Scope) -> Optional[nodes.Node]: def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]: """Return fixturemarker or None if it doesn't exist or raised exceptions.""" - return cast( - Optional[FixtureFunctionMarker], - safe_getattr(obj, "_fixture_function_marker", None), - ) + if type(obj) is FixtureFunctionDefinition: + return obj._fixture_function_marker + return None # Algorithm for sorting on a per-parametrized resource setup basis. @@ -1193,7 +1192,7 @@ class FixtureFunctionDefinition: ): self.name = fixture_function_marker.name or function.__name__ self.__name__ = self.name - # This attribute is only used to check if an arbitrary python object is a fixture. + # This attribute is used to check if an arbitrary python object is a fixture. # Using isinstance on every object in code might execute code that is not intended to be executed. # Like lazy loaded classes. self._fixture_function_marker = fixture_function_marker @@ -1744,15 +1743,9 @@ class FixtureManager: self._holderobjseen.add(holderobj) for name in dir(holderobj): - # The attribute can be an arbitrary descriptor, so the attribute - # access below can raise. safe_getattr() ignores such exceptions. obj = safe_getattr(holderobj, name, None) - marker = getfixturemarker(obj) - if not isinstance(marker, FixtureFunctionMarker): - # Magic globals with __getattr__ might have got us a wrong - # fixture attribute. - continue - if isinstance(obj, FixtureFunctionDefinition): + if type(obj) is FixtureFunctionDefinition: + marker = obj._fixture_function_marker if marker.name: name = marker.name func = obj._get_wrapped_function()