Skip to content

Commit 03ef546

Browse files
authored
Merge pull request #4893 from nicoddemus/simplify-obj-property
Simplify 'obj' property definition in PyobjMixin
2 parents 236bada + de5aa38 commit 03ef546

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/_pytest/python.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,24 @@ class PyobjMixin(PyobjContext):
243243
def __init__(self, *k, **kw):
244244
super(PyobjMixin, self).__init__(*k, **kw)
245245

246-
def obj():
247-
def fget(self):
248-
obj = getattr(self, "_obj", None)
249-
if obj is None:
250-
self._obj = obj = self._getobj()
251-
# XXX evil hack
252-
# used to avoid Instance collector marker duplication
253-
if self._ALLOW_MARKERS:
254-
self.own_markers.extend(get_unpacked_marks(self.obj))
255-
return obj
256-
257-
def fset(self, value):
258-
self._obj = value
259-
260-
return property(fget, fset, None, "underlying python object")
261-
262-
obj = obj()
246+
@property
247+
def obj(self):
248+
"""Underlying Python object."""
249+
obj = getattr(self, "_obj", None)
250+
if obj is None:
251+
self._obj = obj = self._getobj()
252+
# XXX evil hack
253+
# used to avoid Instance collector marker duplication
254+
if self._ALLOW_MARKERS:
255+
self.own_markers.extend(get_unpacked_marks(self.obj))
256+
return obj
257+
258+
@obj.setter
259+
def obj(self, value):
260+
self._obj = value
263261

264262
def _getobj(self):
263+
"""Gets the underlying Python object. May be overwritten by subclasses."""
265264
return getattr(self.parent.obj, self.name)
266265

267266
def getmodpath(self, stopatmodule=True, includemodule=False):

0 commit comments

Comments
 (0)