Skip to content

Commit a2b04d0

Browse files
authored
Merge pull request #1759 from Stranger6667/issue1579-invalid-class
Fixed collection of classes with custom ``__new__`` method. Fixes #1579.
2 parents d37af20 + f7ad173 commit a2b04d0

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ David Díaz-Barquero
3535
David Mohr
3636
David Vierra
3737
Diego Russo
38+
Dmitry Dygalo
3839
Edison Gustavo Muenz
3940
Eduardo Schettino
4041
Elizaveta Shashkova

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
* ImportErrors in plugins now are a fatal error instead of issuing a
5252
pytest warning (`#1479`_). Thanks to `@The-Compiler`_ for the PR.
5353

54+
* Fixed collection of classes with custom ``__new__`` method.
55+
Fixes `#1579`_. Thanks to `@Stranger6667`_ for the PR.
56+
57+
.. _#1579: https://github.com/pytest-dev/pytest/issues/1579
5458
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
5559
.. _#1605: https://github.com/pytest-dev/pytest/issues/1605
5660
.. _#1597: https://github.com/pytest-dev/pytest/pull/1597
@@ -72,6 +76,7 @@
7276
.. _@DRMacIver: https://github.com/DRMacIver
7377
.. _@BeyondEvil: https://github.com/BeyondEvil
7478
.. _@JonathonSonesen: https://github.com/JonathonSonesen
79+
.. _@Stranger6667: https://github.com/Stranger6667
7580

7681

7782
2.9.2

_pytest/python.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,10 @@ def collect(self):
662662
self.warn("C1", "cannot collect test class %r because it has a "
663663
"__init__ constructor" % self.obj.__name__)
664664
return []
665+
elif hasnew(self.obj):
666+
self.warn("C1", "cannot collect test class %r because it has a "
667+
"__new__ constructor" % self.obj.__name__)
668+
return []
665669
return [self._getcustomclass("Instance")(name="()", parent=self)]
666670

667671
def setup(self):
@@ -679,8 +683,7 @@ def setup(self):
679683

680684
class Instance(PyCollector):
681685
def _getobj(self):
682-
obj = self.parent.obj()
683-
return obj
686+
return self.parent.obj()
684687

685688
def collect(self):
686689
self.session._fixturemanager.parsefactories(self)
@@ -793,9 +796,13 @@ def getcallargs(self, obj):
793796
def hasinit(obj):
794797
init = getattr(obj, '__init__', None)
795798
if init:
796-
if init != object.__init__:
797-
return True
799+
return init != object.__init__
800+
798801

802+
def hasnew(obj):
803+
new = getattr(obj, '__new__', None)
804+
if new:
805+
return new != object.__new__
799806

800807

801808
def fillfixtures(function):

testing/python/collect.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ def __getattr__(self, name):
109109
colitems = modcol.collect()
110110
assert len(colitems) == 0
111111

112+
def test_issue1579_namedtuple(self, testdir):
113+
testdir.makepyfile("""
114+
import collections
115+
116+
TestCase = collections.namedtuple('TestCase', ['a'])
117+
""")
118+
result = testdir.runpytest('-rw')
119+
result.stdout.fnmatch_lines(
120+
"*cannot collect test class 'TestCase' "
121+
"because it has a __new__ constructor*"
122+
)
123+
112124

113125
class TestGenerator:
114126
def test_generative_functions(self, testdir):

0 commit comments

Comments
 (0)