-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[WIP] bytes/str/unicode #2203
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
[WIP] bytes/str/unicode #2203
Changes from 1 commit
1974c66
bfa0bac
44f5c95
71a9461
d5aea06
86e7da9
8c69076
cd6ca21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,18 +79,18 @@ x = 1.5 | |
[case testAnyStr_python2] | ||
from typing import AnyStr | ||
def f(x): # type: (AnyStr) -> AnyStr | ||
if isinstance(x, str): | ||
if isinstance(x, bytes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is necessary because AnyStr now has three possible types (bytes, str, unicode). Note that I must return 'foo' for bytes and str, because at runtime they are still indistinguishable! (And these tests actually run the Python code!) |
||
return b'foo' | ||
elif isinstance(x, str): | ||
return 'foo' | ||
elif isinstance(x, bytes): | ||
return b'qux' | ||
else: | ||
return u'zar' | ||
print f('') | ||
print f(b'') | ||
print f(u'') | ||
[out] | ||
foo | ||
qux | ||
foo | ||
zar | ||
|
||
[case testGenericPatterns_python2] | ||
|
@@ -156,13 +156,16 @@ f(**params) | |
|
||
[case testFromFutureImportUnicodeLiterals2_python2] | ||
from __future__ import unicode_literals | ||
def f(x: str) -> None: pass | ||
def f(x): | ||
# type: (str) -> None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (like a few others) was needed because we're now running these tests with |
||
pass | ||
f(b'') | ||
f(u'') | ||
f('') | ||
[out] | ||
_program.py:4: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" | ||
_program.py:5: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" | ||
_program.py:5: error: Argument 1 to "f" has incompatible type "bytes"; expected "str" | ||
_program.py:6: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" | ||
_program.py:7: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that all three calls are now in error! bytes is not compatible with str, and |
||
|
||
[case testStrUnicodeCompatibility_python2] | ||
import typing | ||
|
@@ -237,7 +240,7 @@ u'\x89' | |
import typing | ||
import io | ||
c = io.BytesIO() | ||
c.write('\x89') | ||
c.write(b'\x89') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one feels reasonable. |
||
print(repr(c.getvalue())) | ||
[out] | ||
'\x89' | ||
|
@@ -400,11 +403,12 @@ def f(x: unicode) -> int: pass | |
def f(x: bytearray) -> int: pass | ||
[out] | ||
_program.py:2: error: No overload variant of "f" matches argument types [builtins.int] | ||
_program.py:5: error: No overload variant of "f" matches argument types [builtins.bytes] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because there's no overload for bytes, and there's no equivalency between bytes and bytearray. Not sure I like this. |
||
|
||
[case testByteArrayStrCompatibility_python2] | ||
def f(x): # type: (str) -> None | ||
def f(x): # type: (bytes) -> None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that bytearray() is compatible with bytes, but not with str, hence the change here. |
||
pass | ||
f(bytearray('foo')) | ||
f(bytearray(b'foo')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument to the bytearray() constructor must be bytes. That's reasonable. |
||
|
||
[case testAbstractProperty_python2] | ||
from abc import abstractproperty, ABCMeta | ||
|
@@ -469,7 +473,8 @@ re.subn(upat, lambda m: u'', u'')[0] + u'' | |
|
||
[case testYieldRegressionTypingAwaitable_python2] | ||
# Make sure we don't reference typing.Awaitable in Python 2 mode. | ||
def g() -> int: | ||
def g(): | ||
# type: () -> int | ||
yield | ||
[out] | ||
_program.py: note: In function "g": | ||
|
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.
This is a bit of a problem, because it means that these tests can't be run on Windows any more (until typed_ast is finally ported there -- I think @ddfisher mentioned he was making progress so hopefully I can stop worrying about this). The checker tests (testcheck.py) allow specifying this flag per testcase, but these eval tests don't have that feature.