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

Issue1035 getattr #1121

Merged
merged 2 commits into from
Oct 9, 2015
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2.8.3.dev
---------

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

2.8.2
-----
Expand Down
11 changes: 10 additions & 1 deletion _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import py
import inspect
import types
import sys
import pytest
from _pytest.mark import MarkDecorator, MarkerError
Expand Down Expand Up @@ -43,6 +44,14 @@ def _format_args(func):
def _format_args(func):
return inspect.formatargspec(*inspect.getargspec(func))

if sys.version_info[:2] == (2, 6):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonnyPfannschmidt @hpk42 how about creating a _pytest._compat module to host this kind of "compatibility workaround" into a single place? There are other cases through the code where we do stuff differently based on python version. This would make simpler to remove code once support for a python version is dropped.

(this is out of the scope of this PR, just thought I would take this opportunity to mention it)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, please open a issue :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool: #1125

def isclass(object):
""" Return true if the object is a class. Overrides inspect.isclass for
python 2.6 because it will return True for objects which always return
something on __getattr__ calls (see #1035).
Backport of https://hg.python.org/cpython/rev/35bf8f7a8edc
"""
return isinstance(object, (type, types.ClassType))

def _has_positional_arg(func):
return func.__code__.co_argcount
Expand Down Expand Up @@ -2137,7 +2146,7 @@ def num_mock_patch_args(function):

def getfuncargnames(function, startindex=None):
# XXX merge with main.py's varnames
#assert not inspect.isclass(function)
#assert not isclass(function)
realfunction = function
while hasattr(realfunction, "__wrapped__"):
realfunction = realfunction.__wrapped__
Expand Down
10 changes: 10 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ def teardown_class(cls):
"*1 passed*",
])

def test_issue1035_obj_has_getattr(self, testdir):
modcol = testdir.getmodulecol("""
class Chameleon(object):
def __getattr__(self, name):
return True
chameleon = Chameleon()
""")
colitems = modcol.collect()
assert len(colitems) == 0


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