Merge pull request #9494 from bluetech/instance-property
python: add back `instance` accessor to all python nodes, not just Function
This commit is contained in:
commit
a425f15330
|
@ -283,6 +283,16 @@ class PyobjMixin(nodes.Node):
|
||||||
node = self.getparent(Class)
|
node = self.getparent(Class)
|
||||||
return node.obj if node is not None else None
|
return node.obj if node is not None else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def instance(self):
|
||||||
|
"""Python instance object the function is bound to.
|
||||||
|
|
||||||
|
Returns None if not a test method, e.g. for a standalone test function,
|
||||||
|
a staticmethod, a class or a module.
|
||||||
|
"""
|
||||||
|
node = self.getparent(Function)
|
||||||
|
return getattr(node.obj, "__self__", None) if node is not None else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def obj(self):
|
def obj(self):
|
||||||
"""Underlying Python object."""
|
"""Underlying Python object."""
|
||||||
|
@ -1693,15 +1703,6 @@ class Function(PyobjMixin, nodes.Item):
|
||||||
"""Underlying python 'function' object."""
|
"""Underlying python 'function' object."""
|
||||||
return getimfunc(self.obj)
|
return getimfunc(self.obj)
|
||||||
|
|
||||||
@property
|
|
||||||
def instance(self):
|
|
||||||
"""Python instance object the function is bound to.
|
|
||||||
|
|
||||||
Returns None if not a test method, e.g. for a standalone test function
|
|
||||||
or a staticmethod.
|
|
||||||
"""
|
|
||||||
return getattr(self.obj, "__self__", None)
|
|
||||||
|
|
||||||
def _getobj(self):
|
def _getobj(self):
|
||||||
assert self.parent is not None
|
assert self.parent is not None
|
||||||
if isinstance(self.parent, Class):
|
if isinstance(self.parent, Class):
|
||||||
|
|
|
@ -64,7 +64,7 @@ class TestCollector:
|
||||||
|
|
||||||
assert pytester.collect_by_name(modcol, "doesnotexist") is None
|
assert pytester.collect_by_name(modcol, "doesnotexist") is None
|
||||||
|
|
||||||
def test_getparent(self, pytester: Pytester) -> None:
|
def test_getparent_and_accessors(self, pytester: Pytester) -> None:
|
||||||
modcol = pytester.getmodulecol(
|
modcol = pytester.getmodulecol(
|
||||||
"""
|
"""
|
||||||
class TestClass:
|
class TestClass:
|
||||||
|
@ -77,14 +77,21 @@ class TestCollector:
|
||||||
fn = pytester.collect_by_name(cls, "test_foo")
|
fn = pytester.collect_by_name(cls, "test_foo")
|
||||||
assert isinstance(fn, pytest.Function)
|
assert isinstance(fn, pytest.Function)
|
||||||
|
|
||||||
module_parent = fn.getparent(pytest.Module)
|
assert fn.getparent(pytest.Module) is modcol
|
||||||
assert module_parent is modcol
|
assert modcol.module is not None
|
||||||
|
assert modcol.cls is None
|
||||||
|
assert modcol.instance is None
|
||||||
|
|
||||||
function_parent = fn.getparent(pytest.Function)
|
assert fn.getparent(pytest.Class) is cls
|
||||||
assert function_parent is fn
|
assert cls.module is not None
|
||||||
|
assert cls.cls is not None
|
||||||
|
assert cls.instance is None
|
||||||
|
|
||||||
class_parent = fn.getparent(pytest.Class)
|
assert fn.getparent(pytest.Function) is fn
|
||||||
assert class_parent is cls
|
assert fn.module is not None
|
||||||
|
assert fn.cls is not None
|
||||||
|
assert fn.instance is not None
|
||||||
|
assert fn.function is not None
|
||||||
|
|
||||||
def test_getcustomfile_roundtrip(self, pytester: Pytester) -> None:
|
def test_getcustomfile_roundtrip(self, pytester: Pytester) -> None:
|
||||||
hello = pytester.makefile(".xxx", hello="world")
|
hello = pytester.makefile(".xxx", hello="world")
|
||||||
|
|
Loading…
Reference in New Issue