Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/RFC: remove pytest.python.Instance [ci skip] #4357

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ def safe_str(v):
"Module",
"Generator",
"Function",
"Instance",
"Session",
"Item",
"Class",
Expand Down
5 changes: 1 addition & 4 deletions src/_pytest/mark/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ def from_item(cls, item):
mapped_names = set()

# Add the names of the current item and any parent items
import pytest

for item in item.listchain():
if not isinstance(item, pytest.Instance):
mapped_names.add(item.name)
mapped_names.add(item.name)

# Add the names added as extra keywords to current or parent items
for name in item.listextrakeywords():
Expand Down
1 change: 0 additions & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def ihook(self):

Module = _CompatProperty("Module")
Class = _CompatProperty("Class")
Instance = _CompatProperty("Instance")
Function = _CompatProperty("Function")
File = _CompatProperty("File")
Item = _CompatProperty("Item")
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def pytest_runtest_setup(item):
gen = item.parent
if not hasattr(gen, "_nosegensetup"):
call_optional(gen.obj, "setup")
if isinstance(gen.parent, python.Instance):
call_optional(gen.parent.obj, "setup")
# XXX
# if isinstance(gen.parent, python.Instance):
# call_optional(gen.parent.obj, "setup")
gen._nosegensetup = True
if not call_optional(item.obj, "setup"):
# call module level setup if there is no object level one
Expand Down
34 changes: 7 additions & 27 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def pytest_make_parametrize_id(config, val, argname=None):
class PyobjContext(object):
module = pyobj_property("Module")
cls = pyobj_property("Class")
instance = pyobj_property("Instance")


class PyobjMixin(PyobjContext):
Expand Down Expand Up @@ -268,8 +267,6 @@ def getmodpath(self, stopatmodule=True, includemodule=False):
chain.reverse()
parts = []
for node in chain:
if isinstance(node, Instance):
continue
name = node.name
if isinstance(node, Module):
name = os.path.splitext(name)[0]
Expand Down Expand Up @@ -629,26 +626,30 @@ def _get_xunit_func(obj, name):
class Class(PyCollector):
""" Collector for test methods. """

def _getobj(self):
return getattr(self.parent.obj, self.name)()

def collect(self):
if not safe_getattr(self.obj, "__test__", True):
return []
if hasinit(self.obj):
if hasinit(self.obj.__class__):
self.warn(
PytestWarning(
"cannot collect test class %r because it has a "
"__init__ constructor" % self.obj.__name__
)
)
return []
elif hasnew(self.obj):
elif hasnew(self.obj.__class__):
self.warn(
PytestWarning(
"cannot collect test class %r because it has a "
"__new__ constructor" % self.obj.__name__
)
)
return []
return [self._getcustomclass("Instance")(name="()", parent=self)]
self.session._fixturemanager.parsefactories(self)
return super(Class, self).collect()

def setup(self):
setup_class = _get_xunit_func(self.obj, "setup_class")
Expand All @@ -662,24 +663,6 @@ def setup(self):
self.addfinalizer(lambda: fin_class(self.obj))


class Instance(PyCollector):
_ALLOW_MARKERS = False # hack, destroy later
# instances share the object with their parents in a way
# that duplicates markers instances if not taken out
# can be removed at node structure reorganization time

def _getobj(self):
return self.parent.obj()

def collect(self):
self.session._fixturemanager.parsefactories(self)
return super(Instance, self).collect()

def newinstance(self):
self.obj = self._getobj()
return self.obj


class FunctionMixin(PyobjMixin):
""" mixin for the code common to Function and Generator.
"""
Expand All @@ -688,9 +671,6 @@ def setup(self):
""" perform setup for this test function. """
if hasattr(self, "_preservedparent"):
obj = self._preservedparent
elif isinstance(self.parent, Instance):
obj = self.parent.newinstance()
self.obj = self._getobj()
else:
obj = self.parent.obj
if inspect.ismethod(self.obj):
Expand Down
2 changes: 0 additions & 2 deletions src/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from _pytest.python import Class
from _pytest.python import Function
from _pytest.python import Generator
from _pytest.python import Instance
from _pytest.python import Module
from _pytest.python import Package
from _pytest.python_api import approx
Expand Down Expand Up @@ -61,7 +60,6 @@
"hookimpl",
"hookspec",
"importorskip",
"Instance",
"Item",
"main",
"mark",
Expand Down
9 changes: 2 additions & 7 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,6 @@ def test_collector_attributes(testdir):
def pytest_pycollect_makeitem(collector):
assert collector.Function == pytest.Function
assert collector.Class == pytest.Class
assert collector.Instance == pytest.Instance
assert collector.Module == pytest.Module
"""
)
Expand All @@ -1390,10 +1389,8 @@ def test_customize_through_attributes(testdir):
import pytest
class MyFunction(pytest.Function):
pass
class MyInstance(pytest.Instance):
Function = MyFunction
class MyClass(pytest.Class):
Instance = MyInstance
Function = MyFunction

def pytest_pycollect_makeitem(collector, name, obj):
if name.startswith("MyTestClass"):
Expand All @@ -1408,9 +1405,7 @@ def test_hello(self):
"""
)
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines(
["*MyClass*", "*MyInstance*", "*MyFunction*test_hello*"]
)
result.stdout.fnmatch_lines(["*MyClass*", "*MyFunction*test_hello*"])


def test_unorderable_types(testdir):
Expand Down