refactor: replace attribute check with type check

This commit is contained in:
Glyphack 2024-06-20 16:39:39 +02:00
parent d25a8d9d46
commit 3512997e76
1 changed files with 6 additions and 13 deletions

View File

@ -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()