From 1c94b1af52f0c621bde69851b63bdda302420495 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Tue, 18 May 2021 15:05:38 +0200 Subject: [PATCH] Make tests compatible with Python 3.10 --- tests/test_pysnooper.py | 10 +++++++ tests/utils.py | 63 ++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 882b5eab..a131fbf3 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -140,6 +140,7 @@ def qux(): # In with in recursive call LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), ElapsedTimeEntry(0.4), # Call to bar3 from after with @@ -168,6 +169,7 @@ def qux(): # In with in first call LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), ElapsedTimeEntry(0.7), # Call to bar3 from after with @@ -1086,6 +1088,7 @@ def qux(): # In with in recursive call LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), ElapsedTimeEntry(), # Call to bar3 from after with @@ -1114,6 +1117,7 @@ def qux(): # In with in first call LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), ElapsedTimeEntry(), # Call to bar3 from after with @@ -1183,6 +1187,8 @@ def f1(x1): LineEntry(), ReturnEntry(), ReturnValueEntry('20'), + VariableEntry(min_python_version=(3, 10)), + LineEntry(source_regex="with pysnooper.snoop.*", min_python_version=(3, 10)), ElapsedTimeEntry(), ), normalize=normalize, @@ -1250,6 +1256,8 @@ def f1(a): ReturnValueEntry(), ReturnEntry(), ReturnValueEntry(), + VariableEntry(min_python_version=(3, 10)), + LineEntry(source_regex="with pysnooper.snoop.*", min_python_version=(3, 10)), ElapsedTimeEntry(), ), normalize=normalize, @@ -1298,6 +1306,8 @@ def f(one, two, three, four): VariableEntry("seven", "7"), ReturnEntry(), ReturnValueEntry(), + VariableEntry("result", "None", min_python_version=(3, 10)), + LineEntry(source_regex="with pysnooper.snoop.*", min_python_version=(3, 10)), ElapsedTimeEntry(), ), normalize=normalize, diff --git a/tests/utils.py b/tests/utils.py index 6260cdce..03dd32c9 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -4,6 +4,7 @@ import re import abc import inspect +import sys from pysnooper.utils import DEFAULT_REPR_RE @@ -30,13 +31,24 @@ def get_function_arguments(function, exclude=()): class _BaseEntry(pysnooper.pycompat.ABC): - def __init__(self, prefix=''): + def __init__(self, prefix='', min_python_version=None, max_python_version=None): self.prefix = prefix + self.min_python_version = min_python_version + self.max_python_version = max_python_version @abc.abstractmethod def check(self, s): pass + def is_compatible_with_current_python_version(self): + compatible = True + if self.min_python_version and self.min_python_version > sys.version_info: + compatible = False + if self.max_python_version and self.max_python_version < sys.version_info: + compatible = False + + return compatible + def __repr__(self): init_arguments = get_function_arguments(self.__init__, exclude=('self',)) @@ -53,8 +65,11 @@ def __repr__(self): class _BaseValueEntry(_BaseEntry): - def __init__(self, prefix=''): - _BaseEntry.__init__(self, prefix=prefix) + def __init__(self, prefix='', min_python_version=None, + max_python_version=None): + _BaseEntry.__init__(self, prefix=prefix, + min_python_version=min_python_version, + max_python_version=max_python_version) self.line_pattern = re.compile( r"""^%s(?P(?: {4})*)(?P[^:]*):""" r"""\.{2,7} (?P.*)$""" % (re.escape(self.prefix),) @@ -78,8 +93,11 @@ def check(self, s): class ElapsedTimeEntry(_BaseEntry): - def __init__(self, elapsed_time_value=None, tolerance=0.2, prefix=''): - _BaseEntry.__init__(self, prefix=prefix) + def __init__(self, elapsed_time_value=None, tolerance=0.2, prefix='', + min_python_version=None, max_python_version=None): + _BaseEntry.__init__(self, prefix=prefix, + min_python_version=min_python_version, + max_python_version=max_python_version) self.line_pattern = re.compile( r"""^%s(?P(?: {4})*)Elapsed time: (?P