-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Treat ASCII-only literals as str when using unicode_literals #3648
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
mypy/fastparse2.py
Outdated
@@ -637,6 +638,9 @@ def visit_ImportFrom(self, n: ast27.ImportFrom) -> ImportBase: | |||
n.level, | |||
[(a.name, a.asname) for a in n.names]) | |||
self.imports.append(i) | |||
if (n.module == '__future__' and len(n.names) == 1 and |
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.
Will this work if I do from __future__ import absolute_import, unicode_literals
? That works at runtime.
Also, I don't think the asname
check is necessary. This program:
from __future__ import unicode_literals as why_would_you_do_this
print(type(''))
prints "unicode".
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.
OK, good points. I will relax the check now.
I left some comments on the implementation. If it's not too hard to do in typed_ast, I think we should also make sure that strings with an explicit I'm also not entirely sure this is a good idea, since it does introduce a difference with how these things actually work at runtime. For example, perhaps somebody is passing |
|
||
[case testUnicodeLiteralsFutureImportAllASCII_python2] | ||
# coding=utf-8 | ||
from __future__ import unicode_literals |
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.
Could you add tests for some of the other syntactic variations, like from __future__ import absolute_import, unicode_literals
and from __future__ import unicode_literals as something_else
?
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.
OK, will do.
This is basically one-two lines (plus some boilerplate, but it is auto-generated). The main problem is that this will require a |
Are we going to push in this direction? If it is not that important, maybe better just close this? |
This requires more analysis/experimentation to proceed. Also, I think that we'd need to handle the explicit |
OK, I will then prepare a |
The corresponding |
This will help experimenting with python/mypy#3648 Currently we preserve only ``b`` string modifier on Python 2, this is a bit arbitrary and for the above mentioned PR we need to keep ``u``. Instead of adding another special-casing I just always preserve all string modifiers in a short string ``kind`` on ``Str`` node, for example: ```python >>> st = ast3.parse("u'hi'") >>> st.body[0].value.kind 'u' ```
I am closing this since it seems many people don't like |
Is that separate discussion somewhere? Because in the end we did release the typed_ast feature (with a lot of delays and some fumblings). Was it all for naught? Or is there actually some use for the |
I think there was an attempt to use |
Oh, that's a good enough use case for me! |
Fixes #3619
This PR only affects code that uses
from __future__ import unicode_literals
.It looks like there is a solution that will not require modifying
typed_ast
. Note that this will treat ASCII-only literals with an explicit prefixu'foo'
asstr
, notunicode
. This can be changed, but only by updatingtyped_ast
(the point is thatStr
has an attributehas_b
indicating the presence ofb
prefix, we will need alsohas_u
).I know
unicode_literals
are bad, but I have seen several people complaining about this recently.@JukkaL @JelleZijlstra what do you think?