Skip to content

Commit

Permalink
Merge pull request #1759 from Stranger6667/issue1579-invalid-class
Browse files Browse the repository at this point in the history
Fixed collection of classes with custom ``__new__`` method. Fixes #1579.
  • Loading branch information
The-Compiler authored Jul 25, 2016
2 parents d37af20 + f7ad173 commit a2b04d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ David Díaz-Barquero
David Mohr
David Vierra
Diego Russo
Dmitry Dygalo
Edison Gustavo Muenz
Eduardo Schettino
Elizaveta Shashkova
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -72,6 +76,7 @@
.. _@DRMacIver: https://github.com/DRMacIver
.. _@BeyondEvil: https://github.com/BeyondEvil
.. _@JonathonSonesen: https://github.com/JonathonSonesen
.. _@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 a2b04d0

Please sign in to comment.