Title: Conditional expression in "with" statement inferred as 'object', lacking __enter__ and __exit__ · Issue #5512 · python/mypy · GitHub
Open Graph Title: Conditional expression in "with" statement inferred as 'object', lacking __enter__ and __exit__ · Issue #5512 · python/mypy
X Title: Conditional expression in "with" statement inferred as 'object', lacking __enter__ and __exit__ · Issue #5512 · python/mypy
Description: from tempfile import TemporaryDirectory, TemporaryFile import sys use_file = 'directory' not in sys.argv with (TemporaryFile() if use_file else TemporaryDirectory()) as thing: print(thing) Mypy infers the type of the conditional expressi...
Open Graph Description: from tempfile import TemporaryDirectory, TemporaryFile import sys use_file = 'directory' not in sys.argv with (TemporaryFile() if use_file else TemporaryDirectory()) as thing: print(thing) Mypy inf...
X Description: from tempfile import TemporaryDirectory, TemporaryFile import sys use_file = 'directory' not in sys.argv with (TemporaryFile() if use_file else TemporaryDirectory()) as thing: print(thing) ...
Opengraph URL: https://github.com/python/mypy/issues/5512
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Conditional expression in \"with\" statement inferred as 'object', lacking __enter__ and __exit__","articleBody":" from tempfile import TemporaryDirectory, TemporaryFile\r\n import sys\r\n \r\n use_file = 'directory' not in sys.argv\r\n \r\n with (TemporaryFile() if use_file else TemporaryDirectory()) as thing:\r\n print(thing)\r\n\r\nMypy infers the type of the conditional expression as `object` rather than `Union[IO,TemporaryDirectory]`\r\n\r\n $ mypy multi_with.py\r\n\r\n multi_with.py:6: error: \"object\" has no attribute \"__enter__\"\r\n multi_with.py:6: error: \"object\" has no attribute \"__exit__\"\r\n\r\nExplicitly typing the result of the conditional expression makes the error messages go away:\r\n\r\n named_intermediate: Union[IO, TemporaryDirectory] = (TemporaryFile() if use_file else TemporaryDirectory())\r\n\r\n with named_intermediate as thing:\r\n print(thing)\r\n\r\nI observe this in both mypy versions I tried: 0.600 and 0.620\r\n\r\nThese past issues and pull requests look relevant:\r\nhttps://github.com/python/mypy/issues/3487 Type of conditional expression is join instead of union\r\nhttps://github.com/python/mypy/pull/5041 Fix type of conditional statement to Union and not Join.\r\nhttps://github.com/python/mypy/pull/5095 Don't always infer unions for conditional expressions","author":{"url":"https://github.com/chkno","@type":"Person","name":"chkno"},"datePublished":"2018-08-20T18:08:50Z","url":"https://github.com/python/mypy/issues/5512"}
| 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:a133cbaf-4284-cc4c-cfa0-a2ca9f9e4d6c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC4E:142FA7:6FDF8F:95A00A:6B003858 |
| html-safe-nonce | 75c0fbc9faaeb2c62f791f4bbe4ca12524f115e05b4c057ea0c20f8ecf417bd1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzRFOjE0MkZBNzo2RkRGOEY6OTVBMDBBOjZCMDAzODU4IiwidmlzaXRvcl9pZCI6IjU4MTkwMTMwNDgwOTM5MTUyMjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2ee5e21ccfe548795bc3db2c656ff38a00b98267fa3d16f53f4436712c79b724 |
| hovercard-subject-tag | issue:352240107 |
| 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/5512/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b492167351a1812ed7db233acf9abaceec52dd290550c6eafd9c229343c8513c/python/mypy/issues/5512 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b492167351a1812ed7db233acf9abaceec52dd290550c6eafd9c229343c8513c/python/mypy/issues/5512 |
| og:image:alt | from tempfile import TemporaryDirectory, TemporaryFile import sys use_file = 'directory' not in sys.argv with (TemporaryFile() if use_file else TemporaryDirectory()) as thing: print(thing) Mypy inf... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chkno |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width