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"]: def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]:
"""Return fixturemarker or None if it doesn't exist or raised """Return fixturemarker or None if it doesn't exist or raised
exceptions.""" exceptions."""
return cast( if type(obj) is FixtureFunctionDefinition:
Optional[FixtureFunctionMarker], return obj._fixture_function_marker
safe_getattr(obj, "_fixture_function_marker", None), return None
)
# Algorithm for sorting on a per-parametrized resource setup basis. # 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 = fixture_function_marker.name or function.__name__
self.__name__ = self.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. # Using isinstance on every object in code might execute code that is not intended to be executed.
# Like lazy loaded classes. # Like lazy loaded classes.
self._fixture_function_marker = fixture_function_marker self._fixture_function_marker = fixture_function_marker
@ -1744,15 +1743,9 @@ class FixtureManager:
self._holderobjseen.add(holderobj) self._holderobjseen.add(holderobj)
for name in dir(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) obj = safe_getattr(holderobj, name, None)
marker = getfixturemarker(obj) if type(obj) is FixtureFunctionDefinition:
if not isinstance(marker, FixtureFunctionMarker): marker = obj._fixture_function_marker
# Magic globals with __getattr__ might have got us a wrong
# fixture attribute.
continue
if isinstance(obj, FixtureFunctionDefinition):
if marker.name: if marker.name:
name = marker.name name = marker.name
func = obj._get_wrapped_function() func = obj._get_wrapped_function()