Title: `IO[Any]` is not inferred as the supertype of `IO[str]` and `IO[bytes]` in condional expression `v = iostr if x else iobytes` · Issue #15808 · python/mypy · GitHub
Open Graph Title: `IO[Any]` is not inferred as the supertype of `IO[str]` and `IO[bytes]` in condional expression `v = iostr if x else iobytes` · Issue #15808 · python/mypy
X Title: `IO[Any]` is not inferred as the supertype of `IO[str]` and `IO[bytes]` in condional expression `v = iostr if x else iobytes` · Issue #15808 · python/mypy
Description: Bug Report Consider the following snippet, adapted from argparse.FileType: import sys def opener(path: str, mode: str = "r") -> None: if path and path != "-": fh = open(path, mode) else: if "r" in mode: fh = sys.stdin.buffer if 'b' in mo...
Open Graph Description: Bug Report Consider the following snippet, adapted from argparse.FileType: import sys def opener(path: str, mode: str = "r") -> None: if path and path != "-": fh = open(path, mode) else: if "r" in ...
X Description: Bug Report Consider the following snippet, adapted from argparse.FileType: import sys def opener(path: str, mode: str = "r") -> None: if path and path != "-": fh = open(path,...
Opengraph URL: https://github.com/python/mypy/issues/15808
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`IO[Any]` is not inferred as the supertype of `IO[str]` and `IO[bytes]` in condional expression `v = iostr if x else iobytes`","articleBody":"**Bug Report**\r\n\r\nConsider the following snippet, adapted from [argparse.FileType](https://github.com/python/cpython/blob/main/Lib/argparse.py#L1282):\r\n\r\n```python\r\nimport sys\r\n\r\ndef opener(path: str, mode: str = \"r\") -> None:\r\n if path and path != \"-\":\r\n fh = open(path, mode)\r\n else:\r\n if \"r\" in mode:\r\n fh = sys.stdin.buffer if 'b' in mode else sys.stdin\r\n elif any(c in mode for c in 'wax'):\r\n fh = sys.stdout.buffer if 'b' in mode else sys.stdout\r\n else:\r\n # intentionally not using custom exception\r\n raise ValueError(f\"Tried to open '-' (stdin/stdout) with mode {mode!r}\")\r\n ...\r\n```\r\n\r\n`mypy` gives errors for line 8 and 10 (the `fh` assignments using `sys.stdin` and `sys.stdout`):\r\n```\r\nerror: Incompatible types in assignment (expression has type \"object\", variable has type \"IO[Any]\") [assignment]\r\nerror: Incompatible types in assignment (expression has type \"object\", variable has type \"IO[Any]\") [assignment]\r\n```\r\n\r\nHow come the expressions `sys.stdin.buffer if 'b' in mode else sys.stdin` and `sys.stdout.buffer if 'b' in mode else sys.stdout` has type evaluated as `object`? Both `sys.stdin` and `sys.stdout` are strictly `IO[str]`, and their buffers are `IO[bytes]`, hence I expected both expressions to actually be `IO[Any]`, matching the inferred type of `open()`.\r\n\r\nIf I break the `x = a if y else b` into proper `if/else` statements, mypy does not complain:\r\n\r\n```python\r\n if \"r\" in mode:\r\n if 'b' in mode: \r\n fh = sys.stdin.buffer\r\n else:\r\n fh = sys.stdin\r\n else:\r\n if 'b' in mode: \r\n fh = sys.stdout,buffer\r\n else:\r\n fh = sys.stdout\r\n```\r\n\r\n**Your Environment**\r\n\r\nPython 3.8 from apt repository and pip-installed `mypy 1.4.1 (compiled: yes)` in a venv in Ubuntu 18.04","author":{"url":"https://github.com/MestreLion","@type":"Person","name":"MestreLion"},"datePublished":"2023-08-03T04:24:04Z","url":"https://github.com/python/mypy/issues/15808"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7352b325-21f3-41b1-a0c0-faf0a45882fe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D37E:3EE843:2123533:2D2CC53:6B0028AA |
| html-safe-nonce | efe2a428d3ed8643e15e3b43582fc4f2c2379af3b98cdfe0c55e7b48be009b8a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzdFOjNFRTg0MzoyMTIzNTMzOjJEMkNDNTM6NkIwMDI4QUEiLCJ2aXNpdG9yX2lkIjoiODI4MTEyMzgwODEwMjg1Mjc3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6cd941b8612f496740dcbf79f0733ff87c08a4851b64db0dfd22109845c05130 |
| hovercard-subject-tag | issue:1834245771 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/15808/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fc652c6bc33a064a5660f24ffd8822c7360990770be2d49c4d17e8e36fdc483e/python/mypy/issues/15808 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fc652c6bc33a064a5660f24ffd8822c7360990770be2d49c4d17e8e36fdc483e/python/mypy/issues/15808 |
| og:image:alt | Bug Report Consider the following snippet, adapted from argparse.FileType: import sys def opener(path: str, mode: str = "r") -> None: if path and path != "-": fh = open(path, mode) else: if "r" in ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MestreLion |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width