mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Remove use of unicode_literals as it is an anti-pattern
from __future__ unicode_literals leads to developer confusion as developers no longer can tell whether a bare literal string is a byte string or a unicode string. Explicit marking as u"" or b"" is the way to solve the same problem in the Ansbile codebase.
This commit is contained in:
parent
362a2e523a
commit
ff13d58c14
3 changed files with 50 additions and 19 deletions
|
@ -0,0 +1,16 @@
|
||||||
|
Sanity Tests » no-unicode_literals
|
||||||
|
==================================
|
||||||
|
|
||||||
|
The use of :code:`from __future__ import unicode_literals` has been deemed an anti-pattern. The
|
||||||
|
problems with it are:
|
||||||
|
|
||||||
|
* It makes it so one can't jump into the middle of a file and know whether a bare literal string is
|
||||||
|
a byte string or text string. The programmer has to first check the top of the file to see if the
|
||||||
|
import is there.
|
||||||
|
* It removes the ability to define native strings (a string which should be a byte string on python2
|
||||||
|
and a text string on python3) via a string literal.
|
||||||
|
* It makes for more context switching. A programmer could be reading one file which has
|
||||||
|
`unicode_literals` and know that bare string literals are text strings but then switch to another
|
||||||
|
file (perhaps tracing program execution into a third party library) and have to switch their
|
||||||
|
understanding of what bare string literals are.
|
||||||
|
|
|
@ -88,7 +88,6 @@ EXAMPLES:
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
from __future__ import unicode_literals
|
|
||||||
from ansible.module_utils.six import string_types, integer_types
|
from ansible.module_utils.six import string_types, integer_types
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
@ -120,7 +119,7 @@ class LookupModule(LookupBase):
|
||||||
return sort_parameter
|
return sort_parameter
|
||||||
|
|
||||||
if not isinstance(sort_parameter, list):
|
if not isinstance(sort_parameter, list):
|
||||||
raise AnsibleError("Error. Sort parameters must be a list, not [ {} ]".format(sort_parameter))
|
raise AnsibleError(u"Error. Sort parameters must be a list, not [ {} ]".format(sort_parameter))
|
||||||
|
|
||||||
for item in sort_parameter:
|
for item in sort_parameter:
|
||||||
self._convert_sort_string_to_constant(item)
|
self._convert_sort_string_to_constant(item)
|
||||||
|
@ -130,9 +129,9 @@ class LookupModule(LookupBase):
|
||||||
def _convert_sort_string_to_constant(self, item):
|
def _convert_sort_string_to_constant(self, item):
|
||||||
original_sort_order = item[1]
|
original_sort_order = item[1]
|
||||||
sort_order = original_sort_order.upper()
|
sort_order = original_sort_order.upper()
|
||||||
if sort_order == "ASCENDING":
|
if sort_order == u"ASCENDING":
|
||||||
item[1] = ASCENDING
|
item[1] = ASCENDING
|
||||||
elif sort_order == "DESCENDING":
|
elif sort_order == u"DESCENDING":
|
||||||
item[1] = DESCENDING
|
item[1] = DESCENDING
|
||||||
# else the user knows what s/he is doing and we won't predict. PyMongo will return an error if necessary
|
# else the user knows what s/he is doing and we won't predict. PyMongo will return an error if necessary
|
||||||
|
|
||||||
|
@ -159,13 +158,13 @@ class LookupModule(LookupBase):
|
||||||
return (result - datetime.datetime(1970, 1, 1)). total_seconds()
|
return (result - datetime.datetime(1970, 1, 1)). total_seconds()
|
||||||
else:
|
else:
|
||||||
# failsafe
|
# failsafe
|
||||||
return "{}".format(result)
|
return u"{}".format(result)
|
||||||
|
|
||||||
def run(self, terms, variables, **kwargs):
|
def run(self, terms, variables, **kwargs):
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for term in terms:
|
for term in terms:
|
||||||
'''
|
u'''
|
||||||
Makes a MongoDB query and returns the output as a valid list of json.
|
Makes a MongoDB query and returns the output as a valid list of json.
|
||||||
Timestamps are converted to epoch integers/longs.
|
Timestamps are converted to epoch integers/longs.
|
||||||
|
|
||||||
|
@ -206,20 +205,20 @@ class LookupModule(LookupBase):
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
'''
|
'''
|
||||||
|
|
||||||
connection_string = term.get('connection_string', "mongodb://localhost")
|
connection_string = term.get(u'connection_string', u"mongodb://localhost")
|
||||||
database = term["database"]
|
database = term[u"database"]
|
||||||
collection = term['collection']
|
collection = term[u'collection']
|
||||||
extra_connection_parameters = term.get('extra_connection_parameters', {})
|
extra_connection_parameters = term.get(u'extra_connection_parameters', {})
|
||||||
|
|
||||||
if "extra_connection_parameters" in term:
|
if u"extra_connection_parameters" in term:
|
||||||
del term["extra_connection_parameters"]
|
del term[u"extra_connection_parameters"]
|
||||||
if "connection_string" in term:
|
if u"connection_string" in term:
|
||||||
del term["connection_string"]
|
del term[u"connection_string"]
|
||||||
del term["database"]
|
del term[u"database"]
|
||||||
del term["collection"]
|
del term[u"collection"]
|
||||||
|
|
||||||
if "sort" in term:
|
if u"sort" in term:
|
||||||
term["sort"] = self._fix_sort_parameter(term["sort"])
|
term[u"sort"] = self._fix_sort_parameter(term[u"sort"])
|
||||||
|
|
||||||
# all other parameters are sent to mongo, so we are future and past proof
|
# all other parameters are sent to mongo, so we are future and past proof
|
||||||
|
|
||||||
|
@ -232,6 +231,6 @@ class LookupModule(LookupBase):
|
||||||
ret.append(result)
|
ret.append(result)
|
||||||
|
|
||||||
except ConnectionFailure as e:
|
except ConnectionFailure as e:
|
||||||
raise AnsibleError('unable to connect to database: %s' % str(e))
|
raise AnsibleError(u'unable to connect to database: %s' % str(e))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
16
test/sanity/code-smell/no-unicode-literals.sh
Executable file
16
test/sanity/code-smell/no-unicode-literals.sh
Executable file
|
@ -0,0 +1,16 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
UNICODE_LITERALS_USERS=$(grep -r unicode_literals . \
|
||||||
|
--exclude-dir .git \
|
||||||
|
--exclude-dir .tox \
|
||||||
|
--exclude no-unicode-literals.sh \
|
||||||
|
--exclude no-unicode-literals.rst |
|
||||||
|
grep -v ./test/results \
|
||||||
|
)
|
||||||
|
|
||||||
|
if [ "${UNICODE_LITERALS_USERS}" ]; then
|
||||||
|
echo "${UNICODE_LITERALS_USERS}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit 0
|
Loading…
Reference in a new issue