-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Simplify the unicode logic to avoid bare exception #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| unicode # Python 2 | ||
| except NameError: | ||
| unicode = str # Python 3 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try/except seems a bit odd given the fact that six is already being installed in setup.py.
Line 17 in 4625bc7
| install_requires = ['urllib3 >= 1.15', 'six >= 1.10', 'certifi', 'python-dateutil'], |
Is there any particular reason you didn't just import six and use six.text_type in the string function below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@delirious-lettuce Good to bump into you again on the interwebs... Perhaps your approach is acceptable to the maintainers and is clearly much cleaner but it slightly changes the logic in Python 2. To my mind, the current code reads: if item is unicode then return it, otherwise return str(item). This means that in Python 2 string(42) returns ‘42’, not u’42’. While type('42') == type(u'42') is True in Python 3, it is False in Python 2.
I hope the maintainers find your approach acceptable because then we could just eliminate string() which is currently masking the Python standard library string module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cclauss ,
Nice to bump into you again as well... but I'm not sure I understand your comment.
To my mind, the current code reads: if item is unicode then return it, otherwise return str(item). This means that in Python 2 string(42) returns ‘42’, not u’42’. While type('42') == type(u'42') is True in Python 3, it is False in Python 2.
>>> import six
...
... try:
... unicode # Python 2
... except NameError:
... unicode = str # Python 3
...
...
... def string(item):
... try:
... if isinstance(item, unicode):
... return item
... else:
... return str(item)
... except:
... return str(item)
...
...
... def string_six(item):
... return item if isinstance(item, six.text_type) else str(item)
...
# Python 2.7.14 (default, Dec 12 2017, 12:40:29)
# [GCC 5.4.0 20160609] on linux2
>>> string(42) == string_six(42)
True
>>> string(u'some unicode') == string_six(u'some unicode')
True
# Python 3.6.3 (default, Dec 12 2017, 12:53:45)
# [GCC 5.4.0 20160609] on linux
>>> string(42) == string_six(42)
True
>>> string(u'some unicode') == string_six(u'some unicode')
TrueIt seems like the same output but you get to remove the try/except at the top by simply replacing unicode with six.text_type inside of isinstance. Maybe I am missing something?
Also, I agree with you that the name of this function should be changed to something other than string.
| return str(item) | ||
| except: | ||
| return str(item) | ||
| return item if isinstance(item, unicode) else str(item) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.