fix #2118 - rework Node._getcustomclass and Node compat properties

This commit is contained in:
Ronny Pfannschmidt 2016-12-06 09:13:25 +01:00
parent 3a0a1d2de3
commit 9c285dfc1d
2 changed files with 33 additions and 18 deletions

View File

@ -1,7 +1,9 @@
3.0.6.dev0 3.0.6.dev0
========== ==========
* * fix issue #2118 - protect against internal deprecationerrors by changing the code path of Node._getcustomclass.
This also turns a internal deprecation into a real deprecation.
Thanks to `@nicoddemus`_ for the report and `@RonnyPfannschmidt`_ for the PR.
* *

View File

@ -3,6 +3,8 @@ import functools
import os import os
import sys import sys
import warnings
import _pytest import _pytest
import _pytest._code import _pytest._code
import py import py
@ -190,14 +192,21 @@ class FSHookProxy:
self.__dict__[name] = x self.__dict__[name] = x
return x return x
def compatproperty(name): class _CompatProperty(object):
def fget(self): def __init__(self, name):
import warnings self.name = name
warnings.warn("This usage is deprecated, please use pytest.{0} instead".format(name),
PendingDeprecationWarning, stacklevel=2) def __get__(self, obj, owner):
return getattr(pytest, name) if obj is None:
return self
warnings.warn(
"usage of {owner!r}.{name} is deprecated, please use pytest.{name} instead".format(
name=self.name, owner=type(owner).__name__),
PendingDeprecationWarning, stacklevel=2)
return getattr(pytest, self.name)
return property(fget)
class NodeKeywords(MappingMixin): class NodeKeywords(MappingMixin):
def __init__(self, node): def __init__(self, node):
@ -269,19 +278,23 @@ class Node(object):
""" fspath sensitive hook proxy used to call pytest hooks""" """ fspath sensitive hook proxy used to call pytest hooks"""
return self.session.gethookproxy(self.fspath) return self.session.gethookproxy(self.fspath)
Module = compatproperty("Module") Module = _CompatProperty("Module")
Class = compatproperty("Class") Class = _CompatProperty("Class")
Instance = compatproperty("Instance") Instance = _CompatProperty("Instance")
Function = compatproperty("Function") Function = _CompatProperty("Function")
File = compatproperty("File") File = _CompatProperty("File")
Item = compatproperty("Item") Item = _CompatProperty("Item")
def _getcustomclass(self, name): def _getcustomclass(self, name):
maybe_compatprop = getattr(type(self), name)
if isinstance(maybe_compatprop, _CompatProperty):
return getattr(pytest, name)
else:
cls = getattr(self, name) cls = getattr(self, name)
if cls != getattr(pytest, name):
py.log._apiwarn("2.0", "use of node.%s is deprecated, " warnings.warn("use of node.%s is deprecated, "
"use pytest_pycollect_makeitem(...) to create custom " "use pytest_pycollect_makeitem(...) to create custom "
"collection nodes" % name) "collection nodes" % name, category=DeprecationWarning)
return cls return cls
def __repr__(self): def __repr__(self):