-
Notifications
You must be signed in to change notification settings - Fork 259
Text
, on Python 2, includes bytes
?
#418
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
Comments
@matthiaskramm There was #208 with lots of ideas, none of them were found satisfactory. |
Right, so Jukka's proposal #208 (comment) mentions something along the same lines:
This reads to me as if he was going by the assumption that that's what |
By the way, at some point I modified the Python 2 parser in |
IIUC that code is still there, but we ended up not using it because things got too complicated. The situation is still imperfect, but none of the alternatives seems to really solve everything, so we're keeping the status quo. It's perhaps unfortunate that (in mypy's PY2 mode) you can't distinguish between a function that takes only |
No, I was aware of mypy treats |
We just encountered an issue where code like
silently passed type-checking, even though at runtime, this happens:
StringIO.write() is defined as (https://github.com/python/typeshed/blob/master/stdlib/2/_io.pyi#L135):
and Should we delete "unicode" from the PEP sentence "For parameters typed as unicode or Text, arguments of type str should be acceptable", to allow us to declare parameters as "pure" unicode? |
@matthiaskramm I believe it's a minor class of false negative errors we could ignore. In #208 we have discussed several ideas of making str / bytes / unicode checking stricter in Python 2. None of them turned out to be satisfactory. |
Right, I remember #208. But that one discusses a much broader topic. |
Yes, it's come up for us too occasionally. I'm not sure there's an easy
solution -- I suppose the best approach may be to separate Text from
unicode and make it so that Text is compatible with both str and unicode
while unicode is more constrained, but that would still be a complex change
to mypy, and we'd probably need to revise lots of typeshed stubs to declare
their arguments and return types properly as one or the other (since
currently everyone assumes Text and unicode are interchangeable, and quite
a few places assume str is compatible with both). (One problem IIUC is that
mypy "forgets" aliases so you can't have a type that's an alias for another
type but also endow it with some special meaning.)
|
@matthiaskramm Ok, got it. You mean "false negatives", don't you? A checker doesn't find the error even though it's present at run-time. |
Right, I meant "false negative". I don't like kicking cans down the road, but how about we change the PEP sentence to not mention |
OK, that sounds like a reasonable compromise. Can you propose a PR for PEP 484? |
Maybe it was like this but now mypy is quite flexible with aliases, so this can be implemented if needed. |
See python/typing#418 (comment). Let's leave it up to type-checkers whether they want `unicode`, in Python 2 code, to mean "only unicode" or "str or unicode".
) See python/typing#418 (comment). Let's leave it up to type-checkers whether they want `unicode`, in Python 2 code, to mean "only unicode" or "str or unicode".
I think this has been resolved with additions to PEP 484. |
Uh oh!
There was an error while loading. Please reload this page.
typing.Text
, on Python 2, maps tounicode
.unicode
is automatically promoted toUnion[str, unicode]
, see special rules in typeshed pyi typeshed#270The combination of these two rules means that an annotation
Text
now includesbytes
, on Python 2.And indeed, the below type-checks, with both
pytype
andmypy --python-version=2.7
:This seems a bit odd to me. It also means that we don't have a cross-version way to express "unicode string". Should
Text
not do the auto-promotion, so that it only stands forunicode
itself, on Python 2?The text was updated successfully, but these errors were encountered: