-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
IO[Any]
is not inferred as the supertype of IO[str]
and IO[bytes]
in condional expression v = iostr if x else iobytes
#15808
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
The first assignment determines the implicit type. In the ternary case, there's just one. As for |
Yes, first assignment determines the implicit type, in this case, What do you mean by "In the ternary case, there's just one"?
Is there any workaround, besides breaking the ternary one-liners into multiple if / else? |
I don't think mypy combines two non-
|
The supertype of The supertype of Maintainers may want to add the "topic-join-v-union" label to this issue. |
So I'm not sure how to annotate
Ohh, thanks! So in this particular case the proper way to annotate |
Fixed in #17427 |
Bug Report
Consider the following snippet, adapted from argparse.FileType:
mypy
gives errors for line 8 and 10 (thefh
assignments usingsys.stdin
andsys.stdout
):How come the expressions
sys.stdin.buffer if 'b' in mode else sys.stdin
andsys.stdout.buffer if 'b' in mode else sys.stdout
has type evaluated asobject
? Bothsys.stdin
andsys.stdout
are strictlyIO[str]
, and their buffers areIO[bytes]
, hence I expected both expressions to actually beIO[Any]
, matching the inferred type ofopen()
.If I break the
x = a if y else b
into properif/else
statements, mypy does not complain:Your Environment
Python 3.8 from apt repository and pip-installed
mypy 1.4.1 (compiled: yes)
in a venv in Ubuntu 18.04The text was updated successfully, but these errors were encountered: