Skip to content
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

--future-unicode breaks handling of unicode() calls. #133

Open
yacc143 opened this issue Nov 24, 2015 · 5 comments
Open

--future-unicode breaks handling of unicode() calls. #133

yacc143 opened this issue Nov 24, 2015 · 5 comments

Comments

@yacc143
Copy link

yacc143 commented Nov 24, 2015

$ cat t.py
   print(unicode("Hello World"))
$ python-modernize t.py
    -print(unicode("Hello World"))
    +from __future__ import print_function
    +from __future__ import absolute_import
    +import six
    +print((six.text_type("Hello World")))
$ python-modernize --future-unicode t.py
    -print(unicode("Hello World"))
    +from __future__ import print_function
    +from __future__ import unicode_literals
    +print((str("Hello World")))

Sadly, this translation breaks for anything requiring an unicode object in Python 2.x

@yacc143
Copy link
Author

yacc143 commented Nov 24, 2015

Same thing happens to unichr and chr => it seems like modernize "thinks" that unicode_literals change the semantics of chr/str to unichr/unicode in python 2 which it obviously does not.

@yacc143
Copy link
Author

yacc143 commented Nov 24, 2015

Interestingly 2to3 also changes unicode to str.

@yacc143
Copy link
Author

yacc143 commented Nov 24, 2015

Seems to be related that fix_unicode_future.py derives from lib2to3.fixes.fix_unicode

@takluyver
Copy link
Contributor

Yes, the --future-unicode option should probably enable a fix with a higher priority that will check for unicode being called on a string literal, and eliminate the call before it gets rewritten to str (i.e. unicode('foo') --> 'foo'.

Do you want to have a go at this? There's plenty of examples of fixes and tests to copy from.

@shaib
Copy link
Contributor

shaib commented Apr 24, 2018

Do you think it would be better to go raising the priority of libmodernize.fixes.fix_unicode_type (or maybe some other fixers), or just lower that of libmodernize.fixes.fix_unicode_future ?

For future reference, changing the priority is done by adding a run_order member on the Fixer class; the default value is 5, smaller numbers are higher priorities. See https://github.com/python/cpython/blob/master/Lib/lib2to3/fixer_base.py#L33

brondsem added a commit to switchboardpy/switchboard that referenced this issue Jul 12, 2019
* python-modernize -n -w --no-diffs -f unicode_type .  # avoid PyCQA/modernize#133
* python-modernize --future-unicode -n -w --no-diffs -f unicode_future .
* python-modernize -n -w --no-diffs -f default -f libmodernize.fixes.fix_open .
brondsem added a commit to switchboardpy/switchboard that referenced this issue Jul 12, 2019
* python-modernize -n -w --no-diffs -f unicode_type .  # avoid PyCQA/modernize#133
* python-modernize --future-unicode -n -w --no-diffs -f unicode_future .
* python-modernize -n -w --no-diffs -f default -f libmodernize.fixes.fix_open .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants