Skip to content

Commit b052bec

Browse files
committed
Merge pull request #1121 from tomviner/issue1035-getattr
Issue1035 getattr
2 parents 1f6988b + 88c8dd9 commit b052bec

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
2.8.3.dev
22
---------
33

4+
- fix #1035: collecting tests if test module level obj has __getattr__().
5+
Thanks Suor for the report and Bruno Oliveira / Tom Viner for the PR.
46

57
2.8.2
68
-----

_pytest/python.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import functools
55
import py
66
import inspect
7+
import types
78
import sys
89
import pytest
910
from _pytest.mark import MarkDecorator, MarkerError
@@ -43,6 +44,14 @@ def _format_args(func):
4344
def _format_args(func):
4445
return inspect.formatargspec(*inspect.getargspec(func))
4546

47+
if sys.version_info[:2] == (2, 6):
48+
def isclass(object):
49+
""" Return true if the object is a class. Overrides inspect.isclass for
50+
python 2.6 because it will return True for objects which always return
51+
something on __getattr__ calls (see #1035).
52+
Backport of https://hg.python.org/cpython/rev/35bf8f7a8edc
53+
"""
54+
return isinstance(object, (type, types.ClassType))
4655

4756
def _has_positional_arg(func):
4857
return func.__code__.co_argcount
@@ -2137,7 +2146,7 @@ def num_mock_patch_args(function):
21372146

21382147
def getfuncargnames(function, startindex=None):
21392148
# XXX merge with main.py's varnames
2140-
#assert not inspect.isclass(function)
2149+
#assert not isclass(function)
21412150
realfunction = function
21422151
while hasattr(realfunction, "__wrapped__"):
21432152
realfunction = realfunction.__wrapped__

testing/python/collect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ def teardown_class(cls):
9595
"*1 passed*",
9696
])
9797

98+
def test_issue1035_obj_has_getattr(self, testdir):
99+
modcol = testdir.getmodulecol("""
100+
class Chameleon(object):
101+
def __getattr__(self, name):
102+
return True
103+
chameleon = Chameleon()
104+
""")
105+
colitems = modcol.collect()
106+
assert len(colitems) == 0
107+
98108

99109
class TestGenerator:
100110
def test_generative_functions(self, testdir):

0 commit comments

Comments
 (0)