Title: mypy does not honour `open()` overrides from typeshed. · Issue #11193 · python/mypy · GitHub
Open Graph Title: mypy does not honour `open()` overrides from typeshed. · Issue #11193 · python/mypy
X Title: mypy does not honour `open()` overrides from typeshed. · Issue #11193 · python/mypy
Description: Bug Report It seems like mypy is not honoring the typeshed Path.open overrides exactly, as from these I would expect mypy to think the values returned by open was: io.TextIOWrapper for .open("w") io.BufferedWriter for .open("wb") io.File...
Open Graph Description: Bug Report It seems like mypy is not honoring the typeshed Path.open overrides exactly, as from these I would expect mypy to think the values returned by open was: io.TextIOWrapper for .open("w") i...
X Description: Bug Report It seems like mypy is not honoring the typeshed Path.open overrides exactly, as from these I would expect mypy to think the values returned by open was: io.TextIOWrapper for .open("...
Opengraph URL: https://github.com/python/mypy/issues/11193
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy does not honour `open()` overrides from typeshed.","articleBody":"\u003c!--\r\n If you're new to mypy and you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form\r\n instead.\r\n-->\r\n\r\n**Bug Report**\r\n\r\n\u003c!--\r\nNote: If the problem you are reporting is about a specific library function, then the typeshed tracker is better suited\r\nfor this report: https://github.com/python/typeshed/issues\r\n-->\r\n\r\nIt seems like mypy is not honoring the [typeshed Path.open overrides](https://github.com/python/mypy/blob/v0.910/mypy/typeshed/stdlib/pathlib.pyi#L83-L139) exactly, as from these I would expect mypy to think the values returned by open was:\r\n- `io.TextIOWrapper` for `.open(\"w\")`\r\n- `io.BufferedWriter` for `.open(\"wb\")`\r\n- `io.FileIO` for `.open(\"wb\", buffering=0)`\r\nBut instead, mypy thinks the values returned are:\r\n- `typing.TextIO` for `.open(\"w\")`\r\n- `typing.BinaryIO` for `.open(\"wb\")`\r\n- `typing.BinaryIO` for `.open(\"wb\", buffering=0)`\r\n\r\nThe same is the case for the `open()` builtin.\r\n\r\nI'm reporting this as a mypy issue, because the typing in typeshed looks right to me for the most part, and I can't even tell from typeshed where mypy would be getting `typing.TextIO` from. I will however report a seperate issue against typeshed for mixing the `io.` and `typing.IO` heirarchies in return types, as this is maybe where `typing.BinaryIO` comes from, but even so the other overrides should take precedence as far as I can tell.\r\n\r\n**To Reproduce**\r\n\r\nThe problem can be seen when running mypy on the following code which contain unit tests which run with no errors:\r\n\r\n```python\r\nimport io\r\nimport pathlib\r\nimport tempfile\r\nimport typing\r\nimport unittest\r\n\r\n\r\nclass TestOpen(unittest.TestCase):\r\n def setUp(self) -> None:\r\n self._tmp_path = tempfile.TemporaryDirectory()\r\n self.tmp_path = pathlib.Path(self._tmp_path.name)\r\n self.tmp_file = self.tmp_path / \"file\"\r\n\r\n def tearDown(self) -> None:\r\n self._tmp_path.cleanup()\r\n\r\n def test_open_text_stream(self) -> None:\r\n with self.tmp_file.open(\"w\") as text_stream:\r\n text_io: typing.TextIO = text_stream # noqa: F841\r\n text_io_base: io.TextIOBase = text_stream # noqa: F841\r\n assert isinstance(text_stream, io.TextIOBase)\r\n\r\n def test_open_buffered_stream(self) -> None:\r\n with self.tmp_file.open(\"wb\") as buffered_stream:\r\n binary_io: typing.BinaryIO = buffered_stream # noqa: F841\r\n buffered_io_base: io.BufferedIOBase = buffered_stream # noqa: F841\r\n assert isinstance(buffered_stream, io.BufferedIOBase)\r\n\r\n def test_open_raw_stream(self) -> None:\r\n with self.tmp_file.open(\"wb\", buffering=0) as raw_stream:\r\n binary_io: typing.BinaryIO = raw_stream # noqa: F841\r\n raw_io_base: io.RawIOBase = raw_stream # noqa: F841\r\n assert isinstance(binary_io, io.RawIOBase)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n unittest.main()\r\n```\r\n\r\n**Expected Behavior**\r\n\r\n\u003c!--\r\n How did you expect your project to behave?\r\n It’s fine if you’re not sure your understanding is correct.\r\n Write down what you thought would happen. If you just expected no errors, you can delete this section.\r\n-->\r\n\r\nI expect mypy to not find any errors in the shared code.\r\n\r\n\r\n**Actual Behavior**\r\n\r\n\u003c!--\r\n Did something go wrong?\r\n Is something broken, or not behaving as you expected?\r\n-->\r\n\r\nMypy reports the following type errors:\r\n\r\n```\r\n$ poetry run mypy --show-error-codes --show-error-context test_open_ut.py\r\ntest_open_ut.py: note: In member \"test_open_text_stream\" of class \"TestOpen\":\r\ntest_open_ut.py:20: error: Incompatible types in assignment (expression has type \"TextIO\", variable has type \"TextIOBase\") [assignment]\r\ntest_open_ut.py:21: error: Subclass of \"TextIO\" and \"TextIOBase\" cannot exist: would have incompatible method signatures [unreachable]\r\ntest_open_ut.py: note: In member \"test_open_buffered_stream\" of class \"TestOpen\":\r\ntest_open_ut.py:26: error: Incompatible types in assignment (expression has type \"BinaryIO\", variable has type \"BufferedIOBase\") [assignment]\r\ntest_open_ut.py:27: error: Subclass of \"BinaryIO\" and \"BufferedIOBase\" cannot exist: would have incompatible method signatures [unreachable]\r\ntest_open_ut.py: note: In member \"test_open_raw_stream\" of class \"TestOpen\":\r\ntest_open_ut.py:32: error: Incompatible types in assignment (expression has type \"BinaryIO\", variable has type \"RawIOBase\") [assignment]\r\ntest_open_ut.py:33: error: Subclass of \"BinaryIO\" and \"RawIOBase\" cannot exist: would have incompatible method signatures [unreachable]\r\n```\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n - Mypy version used: 0.910\r\n - Mypy command-line flags: `--show-error-codes --show-error-context`\r\n - Mypy configuration options from `mypy.ini` (and other config files):\r\n ```\r\n [mypy]\r\n # Kept as seperate config file as some plugins don't support pyproject.toml\r\n # (e.g pydantic.mypy)\r\n # https://mypy.readthedocs.io/en/stable/config_file.html\r\n python_version = 3.7\r\n strict = True\r\n warn_unreachable = True\r\n warn_unused_configs = True\r\n ```\r\n - Python version used: 3.7\r\n - Operating system and version: Fedora 34\r\n\r\nThe code for this can be found [here](https://gitlab.com/aucampia/issues/202109-pythonio/-/tree/issue-mypy_open_typing)\r\n\r\n\r\n\u003c!--\r\nYou can freely edit this text, please remove all the lines\r\nyou believe are unnecessary.\r\n-->","author":{"url":"https://github.com/aucampia","@type":"Person","name":"aucampia"},"datePublished":"2021-09-25T16:28:16Z","url":"https://github.com/python/mypy/issues/11193"}
| 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:e0779fbe-4799-d91a-0483-90c18f30600b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 92B0:145630:2D08871:3B889BB:6B002070 |
| html-safe-nonce | 0520fabe22de40fc0463ece3dd39cba7fdf873c0a4e4765e45110f6c20776142 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MkIwOjE0NTYzMDoyRDA4ODcxOjNCODg5QkI6NkIwMDIwNzAiLCJ2aXNpdG9yX2lkIjoiMjUxODY1ODYzMTkzNTcyOTc3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fe750ad92b7998b406af19e577133f943317929669883088347de95159804ad0 |
| hovercard-subject-tag | issue:1007136974 |
| 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/11193/issue_layout |
| twitter:image | https://opengraph.githubassets.com/321c1c85d1f225c967e5f9f1446ebc3c9e412025e664abb50c15bca31ff44626/python/mypy/issues/11193 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/321c1c85d1f225c967e5f9f1446ebc3c9e412025e664abb50c15bca31ff44626/python/mypy/issues/11193 |
| og:image:alt | Bug Report It seems like mypy is not honoring the typeshed Path.open overrides exactly, as from these I would expect mypy to think the values returned by open was: io.TextIOWrapper for .open("w") i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aucampia |
| 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