Skip to content

Commit

Permalink
Fixed collection of classes with custom __new__ method
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Jul 23, 2016
1 parent 38e0e08 commit c1bcf19
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Dave Hunt
David Díaz-Barquero
David Mohr
David Vierra
Dmitry Dygalo
Edison Gustavo Muenz
Eduardo Schettino
Elizaveta Shashkova
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
* ImportErrors in plugins now are a fatal error instead of issuing a
pytest warning (`#1479`_). Thanks to `@The-Compiler`_ for the PR.

* Fixed collection of classes with custom ``__new__`` method.
Fixes (`#1579`_). Thanks to `@Stranger6667`_ for the PR.

.. _#1579: https://github.com/pytest-dev/pytest/issues/1579
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
.. _#1605: https://github.com/pytest-dev/pytest/issues/1605
.. _#1597: https://github.com/pytest-dev/pytest/pull/1597
Expand All @@ -67,7 +71,7 @@
.. _@bagerard: https://github.com/bagerard
.. _@davehunt: https://github.com/davehunt
.. _@DRMacIver: https://github.com/DRMacIver

.. _@Stranger6667: https://github.com/Stranger6667

2.9.2
=====
Expand Down
15 changes: 11 additions & 4 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ def collect(self):
self.warn("C1", "cannot collect test class %r because it has a "
"__init__ constructor" % self.obj.__name__)
return []
elif hasnew(self.obj):
self.warn("C1", "cannot collect test class %r because it has a "
"__new__ constructor" % self.obj.__name__)
return []
return [self._getcustomclass("Instance")(name="()", parent=self)]

def setup(self):
Expand All @@ -679,8 +683,7 @@ def setup(self):

class Instance(PyCollector):
def _getobj(self):
obj = self.parent.obj()
return obj
return self.parent.obj()

def collect(self):
self.session._fixturemanager.parsefactories(self)
Expand Down Expand Up @@ -793,9 +796,13 @@ def getcallargs(self, obj):
def hasinit(obj):
init = getattr(obj, '__init__', None)
if init:
if init != object.__init__:
return True
return init != object.__init__


def hasnew(obj):
new = getattr(obj, '__new__', None)
if new:
return new != object.__new__


def fillfixtures(function):
Expand Down
12 changes: 12 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ def __getattr__(self, name):
colitems = modcol.collect()
assert len(colitems) == 0

def test_issue1579_namedtuple(self, testdir):
testdir.makepyfile("""
import collections
TestCase = collections.namedtuple('TestCase', ['a'])
""")
result = testdir.runpytest('-rw')
result.stdout.fnmatch_lines(
"*cannot collect test class 'TestCase' "
"because it has a __new__ constructor*"
)


class TestGenerator:
def test_generative_functions(self, testdir):
Expand Down

0 comments on commit c1bcf19

Please sign in to comment.