-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
read of SpooledTemporaryFile returns bytes, mypy assumes str #3360
Comments
Unfortunately, there is no good way to express this in typeshed at this point. I have filed python/typing#680, but I don't foresee that this will be implemented any time soon. The best you could do is to use an explicit type annotation: from tempfile import SpooledTemporaryFile
stderrfile: SpooledTemporaryFile[bytes] = SpooledTemporaryFile()
sterr_output = stderrfile.read().decode() |
@srittau ah, okay. Now I understand the problem. If I open this in Yeah, the question is whether the annotation is correct in giving the restricted type |
@srittau If I'd write a function with multiple return types, I would annotate it this way. |
A |
I think this is correct here and not in the
mypy
repo (if I'm wrong, I am happy to open a ticket there):following code
works fine with python3.7 . Using mypy (also with python 3.7) I get the following error:
Mypy incorrectly assumes that the result of the read is a
str
but using the interactive interpreter you'll see that theread
method actually returns abyte
object. The type annotations (https://github.com/python/typeshed/blob/master/stdlib/3/tempfile.pyi) sayAnyStr
as return type which is too narrow in this case.The text was updated successfully, but these errors were encountered: