From 47bdf9f26fdd499c00a25efa4c0229c544cf682d Mon Sep 17 00:00:00 2001 From: cclauss Date: Fri, 25 Jan 2019 09:44:29 +0100 Subject: [PATCH] _compat.py: Use feature detection instead of version detection When run on Python 3, linters such as Pylint and Flake8 will correctly flag __unicode__ and __long__ as _undefined names_ because _compat.py does not currently follow the Python porting best practice [___use feature detection instead of version detection___](https://docs.python.org/3/howto/pyporting.html#use-feature-detection-instead-of-version-detection). This PR allows this project to pass those tests without adding any linter directives. --- flask/_compat.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/flask/_compat.py b/flask/_compat.py index b20e8abf..85aeee01 100644 --- a/flask/_compat.py +++ b/flask/_compat.py @@ -16,12 +16,16 @@ import sys PY2 = sys.version_info[0] == 2 _identity = lambda x: x - -if not PY2: +try: # Python 2 + text_type = unicode + string_types = (str, unicode) + integer_types = (int, long) +except NameError: # Python 3 text_type = str string_types = (str,) integer_types = (int,) +if not PY2: iterkeys = lambda d: iter(d.keys()) itervalues = lambda d: iter(d.values()) iteritems = lambda d: iter(d.items()) @@ -38,10 +42,6 @@ if not PY2: implements_to_string = _identity else: - text_type = unicode - string_types = (str, unicode) - integer_types = (int, long) - iterkeys = lambda d: d.iterkeys() itervalues = lambda d: d.itervalues() iteritems = lambda d: d.iteritems()