From 940ed7e943c63e2eb9df1d537775692021ff0a53 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Wed, 21 Sep 2016 17:44:25 -0700 Subject: [PATCH] Fix `DeprecationWarnings` found when running py.test in Python 2.7 with the -3 flag. Running through some of my tests with the `-3` flag in python2.7 I encountered some errors within py.test itself. This fixes those errors so we can use py.test in order to identify problems with Python 3. --- _pytest/_code/code.py | 4 ++++ _pytest/_code/source.py | 3 +++ _pytest/assertion/util.py | 2 +- _pytest/python.py | 6 ++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/_pytest/_code/code.py b/_pytest/_code/code.py index 9a6d32665..21e1460fd 100644 --- a/_pytest/_code/code.py +++ b/_pytest/_code/code.py @@ -12,6 +12,7 @@ if sys.version_info[0] >= 3: else: from ._py2traceback import format_exception_only + class Code(object): """ wrapper around Python code objects """ def __init__(self, rawcode): @@ -28,6 +29,9 @@ class Code(object): def __eq__(self, other): return self.raw == other.raw + def __hash__(self): + return hash(self.raw) + def __ne__(self, other): return not self == other diff --git a/_pytest/_code/source.py b/_pytest/_code/source.py index a1521f8a2..ae9f0463f 100644 --- a/_pytest/_code/source.py +++ b/_pytest/_code/source.py @@ -52,6 +52,9 @@ class Source(object): return str(self) == other return False + def __hash__(self): + return hash(self.lines) + def __getitem__(self, key): if isinstance(key, int): return self.lines[key] diff --git a/_pytest/assertion/util.py b/_pytest/assertion/util.py index 2481cf34c..4a0a4e431 100644 --- a/_pytest/assertion/util.py +++ b/_pytest/assertion/util.py @@ -105,7 +105,7 @@ except NameError: def assertrepr_compare(config, op, left, right): """Return specialised explanations for some operators/operands""" width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op - left_repr = py.io.saferepr(left, maxsize=int(width/2)) + left_repr = py.io.saferepr(left, maxsize=int(width//2)) right_repr = py.io.saferepr(right, maxsize=width-len(left_repr)) summary = u('%s %s %s') % (ecu(left_repr), op, ecu(right_repr)) diff --git a/_pytest/python.py b/_pytest/python.py index f26c128ca..ebfb8e1d1 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1356,6 +1356,9 @@ class approx(object): return False return all(a == x for a, x in zip(actual, self.expected)) + def __hash__(self): + return hash(self.expected) + def __ne__(self, actual): return not (actual == self) @@ -1435,6 +1438,9 @@ class ApproxNonIterable(object): # Return true if the two numbers are within the tolerance. return abs(self.expected - actual) <= self.tolerance + def __hash__(self): + return hash((self.expected, self.tolerance)) + def __ne__(self, actual): return not (actual == self)