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

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

Merged
merged 4 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
3.0.6.dev0
==========

*
* fix issue #2118 - protect against internal deprecationerrors by changing the code path of Node._getcustomclass.
Copy link
Member

@nicoddemus nicoddemus Dec 6, 2016

Choose a reason for hiding this comment

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

Could you please add a link to #2118?

Copy link
Member

Choose a reason for hiding this comment

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

@RonnyPfannschmidt besides adding the link, I think it needs rewording to make more sense to users (which don't know about pytest's internals). Suggestion:

- pytest no longer generates ``PendingDeprecationWarning`` from its own operations, which was introduced by mistake in version ``3.0.5`` (`#2118`_).
  Thanks to `@nicoddemus`_ for the report and `@RonnyPfannschmidt`_ for the PR.

This also turns a internal deprecation into a real deprecation.
Thanks to `@nicoddemus`_ for the report and `@RonnyPfannschmidt`_ for the PR.

*

Expand Down
47 changes: 30 additions & 17 deletions _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import sys

import warnings

import _pytest
import _pytest._code
import py
Expand Down Expand Up @@ -190,14 +192,21 @@ def __getattr__(self, name):
self.__dict__[name] = x
return x

def compatproperty(name):
def fget(self):
import warnings
warnings.warn("This usage is deprecated, please use pytest.{0} instead".format(name),
PendingDeprecationWarning, stacklevel=2)
return getattr(pytest, name)
class _CompatProperty(object):
def __init__(self, name):
self.name = name

def __get__(self, obj, owner):
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):
def __init__(self, node):
Expand Down Expand Up @@ -269,19 +278,23 @@ def ihook(self):
""" fspath sensitive hook proxy used to call pytest hooks"""
return self.session.gethookproxy(self.fspath)

Module = compatproperty("Module")
Class = compatproperty("Class")
Instance = compatproperty("Instance")
Function = compatproperty("Function")
File = compatproperty("File")
Item = compatproperty("Item")
Module = _CompatProperty("Module")
Class = _CompatProperty("Class")
Instance = _CompatProperty("Instance")
Function = _CompatProperty("Function")
File = _CompatProperty("File")
Item = _CompatProperty("Item")

def _getcustomclass(self, name):
cls = getattr(self, name)
if cls != getattr(pytest, name):
py.log._apiwarn("2.0", "use of node.%s is deprecated, "
maybe_compatprop = getattr(type(self), name)
if isinstance(maybe_compatprop, _CompatProperty):
return getattr(pytest, name)
else:
cls = getattr(self, name)

warnings.warn("use of node.%s is deprecated, "
Copy link
Member

Choose a reason for hiding this comment

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

Isn't node.Something the only usage which has been "pending-deprecated" by _CompatProperty? AFAICT this is the only user of the _CompatProperty class, so it seems this will generate two warnings (a PendingDeprecationWarning and a DeprecationWarning?

Copy link
Member Author

Choose a reason for hiding this comment

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

there is different use-cases there

one warns about using the attributes, the other warns about custom declarations while still supporting them

custom declarations will override the properties

the coode i posted ensures that

  • internal usage as planned does not warn
  • overriding the compatproperties will warn on internal usage
  • using the properties in external code will warn

Copy link
Member

Choose a reason for hiding this comment

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

What's a "custom declaration" in this context exactly? Subclassing Node and creating a property named Function (for example)?

Copy link
Member Author

Choose a reason for hiding this comment

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

correct, py.test previously used classes and attributes in the 1.x series
basically this is code that should have created actual warnings in 2.x and be removed in 3.x

"use pytest_pycollect_makeitem(...) to create custom "
"collection nodes" % name)
"collection nodes" % name, category=DeprecationWarning)
return cls

def __repr__(self):
Expand Down