From dc3b3158eb5dd66e1c1e44138da7b66e5225c0f9 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 18 Jul 2017 18:00:10 +0200 Subject: [PATCH] Move some code from one compatibility file to another. --- parso/_compatibility.py | 32 ++++++++++++++++++++++++++++ test/_compatibility.py | 30 -------------------------- test/test_normalizer_issues_files.py | 2 +- 3 files changed, 33 insertions(+), 31 deletions(-) delete mode 100644 test/_compatibility.py diff --git a/parso/_compatibility.py b/parso/_compatibility.py index d8b10fb..d9432ca 100644 --- a/parso/_compatibility.py +++ b/parso/_compatibility.py @@ -66,3 +66,35 @@ def utf8_repr(func): return func else: return wrapper + + +try: + from functools import total_ordering +except ImportError: + # Python 2.6 + def total_ordering(cls): + """Class decorator that fills in missing ordering methods""" + convert = { + '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), + ('__le__', lambda self, other: self < other or self == other), + ('__ge__', lambda self, other: not self < other)], + '__le__': [('__ge__', lambda self, other: not self <= other or self == other), + ('__lt__', lambda self, other: self <= other and not self == other), + ('__gt__', lambda self, other: not self <= other)], + '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), + ('__ge__', lambda self, other: self > other or self == other), + ('__le__', lambda self, other: not self > other)], + '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), + ('__gt__', lambda self, other: self >= other and not self == other), + ('__lt__', lambda self, other: not self >= other)] + } + roots = set(dir(cls)) & set(convert) + if not roots: + raise ValueError('must define at least one ordering operation: < > <= >=') + root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ + for opname, opfunc in convert[root]: + if opname not in roots: + opfunc.__name__ = opname + opfunc.__doc__ = getattr(int, opname).__doc__ + setattr(cls, opname, opfunc) + return cls diff --git a/test/_compatibility.py b/test/_compatibility.py deleted file mode 100644 index fe81076..0000000 --- a/test/_compatibility.py +++ /dev/null @@ -1,30 +0,0 @@ -try: - from functools import total_ordering -except ImportError: - # Python 2.6 - def total_ordering(cls): - """Class decorator that fills in missing ordering methods""" - convert = { - '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), - ('__le__', lambda self, other: self < other or self == other), - ('__ge__', lambda self, other: not self < other)], - '__le__': [('__ge__', lambda self, other: not self <= other or self == other), - ('__lt__', lambda self, other: self <= other and not self == other), - ('__gt__', lambda self, other: not self <= other)], - '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), - ('__ge__', lambda self, other: self > other or self == other), - ('__le__', lambda self, other: not self > other)], - '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), - ('__gt__', lambda self, other: self >= other and not self == other), - ('__lt__', lambda self, other: not self >= other)] - } - roots = set(dir(cls)) & set(convert) - if not roots: - raise ValueError('must define at least one ordering operation: < > <= >=') - root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ - for opname, opfunc in convert[root]: - if opname not in roots: - opfunc.__name__ = opname - opfunc.__doc__ = getattr(int, opname).__doc__ - setattr(cls, opname, opfunc) - return cls diff --git a/test/test_normalizer_issues_files.py b/test/test_normalizer_issues_files.py index b0672c7..13b9c73 100644 --- a/test/test_normalizer_issues_files.py +++ b/test/test_normalizer_issues_files.py @@ -5,9 +5,9 @@ tests of pydocstyle. import difflib import re -from test._compatibility import total_ordering import parso +from parso._compatibility import total_ordering from parso.utils import source_to_unicode