Title: `typing.IO` and `io.BaseIO` type hierarchies are incompatible · Issue #6077 · python/typeshed · GitHub
Open Graph Title: `typing.IO` and `io.BaseIO` type hierarchies are incompatible · Issue #6077 · python/typeshed
X Title: `typing.IO` and `io.BaseIO` type hierarchies are incompatible · Issue #6077 · python/typeshed
Description: Python documentation for the open() builtin indicates that open() returns types from the io.IOBase hierarchy: The type of file object returned by the open() function depends on the mode. When open() is used to open a file in a text mode ...
Open Graph Description: Python documentation for the open() builtin indicates that open() returns types from the io.IOBase hierarchy: The type of file object returned by the open() function depends on the mode. When open(...
X Description: Python documentation for the open() builtin indicates that open() returns types from the io.IOBase hierarchy: The type of file object returned by the open() function depends on the mode. When open(...
Opengraph URL: https://github.com/python/typeshed/issues/6077
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`typing.IO` and `io.BaseIO` type hierarchies are incompatible","articleBody":"Python documentation for the [`open()`][python/open] builtin indicates that `open()` returns types from the [`io.IOBase`][python/io.IOBase] hierarchy:\r\n\r\n> The type of file object returned by the open() function depends on the mode. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a subclass of `io.TextIOBase` (specifically `io.TextIOWrapper`). When used to open a file in a binary mode with buffering, the returned class is a subclass of `io.BufferedIOBase`. The exact class varies: in read binary mode, it returns an `io.BufferedReader`; in write binary and append binary modes, it returns an `io.BufferedWriter`, and in read/write mode, it returns an `io.BufferedRandom`. When buffering is disabled, the raw stream, a subclass of `io.RawIOBase`, `io.FileIO`, is returned.\r\n\r\nPython documentation for [`typing.IO[...]`][python/typing.IO] suggests that objects returned by the `open()` builtin are of type [`typing.IO[...]`][python/typing.IO].\r\n\r\n> `class typing.IO`\r\n>\r\n> `class typing.TextIO`\r\n>\r\n> `class typing.BinaryIO`\r\n>\r\n> Generic type `IO[AnyStr]` and its subclasses `TextIO(IO[str])` and `BinaryIO(IO[bytes])` represent the types of I/O streams such as returned by open().\r\n\r\nFrom this, I deduce that if I type a function as accepting either `typing.IO[typing.AnyStr]` or `io.IOBase`, it should be able to accept any object returned from `open()`. And following from this, I would assume that it should be possible to have an object that is of both `typing.IO` and `io.IOBase` type, because if objects that are of `typing.IO` type could not be of `typing.IOBase` type, and objects of `typing.IOBase` type could not be of `io.IOBase` type, then objects returned from `open()` could not be in line with the documentation I quoted above.\r\n\r\nGiven this, I would expect the following code to pass type checking without error:\r\n\r\n```python\r\nimport io\r\nimport typing\r\n\r\n\r\ndef accept_text_io(obj: typing.TextIO) -> None:\r\n assert isinstance(obj, io.TextIOBase)\r\n\r\n\r\ndef accept_binary_io(obj: typing.BinaryIO) -> None:\r\n assert isinstance(obj, io.RawIOBase) or isinstance(obj, io.BufferedIOBase)\r\n```\r\n\r\nHowever, when I run mypy on it, I get the following errors.\r\n\r\n```python\r\n$ poetry run mypy --show-error-codes --show-error-context --strict --warn-unreachable --warn-unused-configs --python-version 3.7 test_iotypes_000.py\r\ntest_iotypes_000.py: note: In function \"accept_text_io\":\r\ntest_iotypes_000.py:6: error: Subclass of \"TextIO\" and \"TextIOBase\" cannot exist: would have incompatible method signatures [unreachable]\r\ntest_iotypes_000.py: note: In function \"accept_binary_io\":\r\ntest_iotypes_000.py:10: error: Subclass of \"BinaryIO\" and \"RawIOBase\" cannot exist: would have incompatible method signatures [unreachable]\r\ntest_iotypes_000.py:10: error: Subclass of \"BinaryIO\" and \"BufferedIOBase\" cannot exist: would have incompatible method signatures [unreachable]\r\nFound 3 errors in 1 file (checked 1 source file)\r\n```\r\n\r\nTo further determine what is going wrong here, why something can't be of both `typing.IO` types and `io.IOBase` types, I ran the following code through mypy, which again I would expect to pass without any errors:\r\n\r\n```python\r\nimport io\r\nimport typing\r\n\r\n\r\nclass DummyBaseIO0(typing.IO[typing.AnyStr], io.IOBase):\r\n pass\r\n\r\n\r\nclass DummyBaseIO1(typing.IO[typing.Any], io.IOBase):\r\n pass\r\n\r\n\r\nclass DummyTextIO(typing.TextIO, io.TextIOBase):\r\n pass\r\n\r\n\r\nclass DummyRawIO(typing.BinaryIO, io.RawIOBase):\r\n pass\r\n\r\n\r\nclass DummyRawIO(typing.BinaryIO, io.BufferedIOBase):\r\n pass\r\n```\r\n\r\nAnd for this code mypy reports the following errors, and specifically highlights the incompatibilities:\r\n\r\n```python\r\n$ poetry run mypy --show-error-codes --show-error-context --strict --warn-unreachable --warn-unused-configs --python-version 3.7 test_iotypes_010.py\r\ntest_iotypes_010.py: note: In class \"DummyBaseIO0\":\r\ntest_iotypes_010.py:5: error: Definition of \"readline\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"__iter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"readlines\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"__enter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"writelines\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"__next__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"__iter__\" in base class \"Iterator\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"__next__\" in base class \"Iterator\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:5: error: Definition of \"__iter__\" in base class \"Iterable\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py: note: In class \"DummyBaseIO1\":\r\ntest_iotypes_010.py:9: error: Definition of \"readline\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:9: error: Definition of \"__enter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py: note: In class \"DummyTextIO\":\r\ntest_iotypes_010.py:13: error: Definition of \"__enter__\" in base class \"TextIO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"newlines\" in base class \"TextIO\" is incompatible with definition in base class \"TextIOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"errors\" in base class \"TextIO\" is incompatible with definition in base class \"TextIOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"encoding\" in base class \"TextIO\" is incompatible with definition in base class \"TextIOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"readline\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"__iter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"readlines\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"__enter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"writelines\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"read\" in base class \"IO\" is incompatible with definition in base class \"TextIOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"__next__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"__iter__\" in base class \"Iterator\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"__next__\" in base class \"Iterator\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:13: error: Definition of \"__iter__\" in base class \"Iterable\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py: note: In class \"DummyRawIO\":\r\ntest_iotypes_010.py:17: error: Definition of \"__enter__\" in base class \"BinaryIO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:17: error: Definition of \"readline\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:17: error: Definition of \"write\" in base class \"IO\" is incompatible with definition in base class \"RawIOBase\" [misc]\r\ntest_iotypes_010.py:17: error: Definition of \"__enter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:17: error: Definition of \"writelines\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py: note: At top level:\r\ntest_iotypes_010.py:21: error: Name \"DummyRawIO\" already defined on line 17 [no-redef]\r\ntest_iotypes_010.py: note: In class \"DummyRawIO\":\r\ntest_iotypes_010.py:21: error: Definition of \"__enter__\" in base class \"BinaryIO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:21: error: Definition of \"readline\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:21: error: Definition of \"write\" in base class \"IO\" is incompatible with definition in base class \"BufferedIOBase\" [misc]\r\ntest_iotypes_010.py:21: error: Definition of \"__enter__\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:21: error: Definition of \"writelines\" in base class \"IO\" is incompatible with definition in base class \"IOBase\" [misc]\r\ntest_iotypes_010.py:21: error: Definition of \"read\" in base class \"IO\" is incompatible with definition in base class \"BufferedIOBase\" [misc]\r\nFound 37 errors in 1 file (checked 1 source file)\r\n```\r\n\r\n## Summary\r\n\r\nSo in summary, I would expect the `io.IOBase` and `typing.IO[...]` hierarchies to be compatible, and all the code in here to pass mypy validation, but it does not. I'm not sure if there is a good explanation for why the two type hierarchies are incompatible.\r\n\r\nPractically, to work around this, I have to now type variables as `typing.Enum[typing.TextIO,io.TextIOBase]` if it should work with `TextIO` like things. This is maybe partly due to some other issues (see https://github.com/python/mypy/issues/11193, https://github.com/python/typeshed/issues/6061, https://github.com/python/typeshed/issues/6076), but definitely these incompatibilities do make things harder, and seem wrong to me.\r\n\r\n## Versions\r\n\r\nThis issue is written for:\r\n\r\n - Python 3.7\r\n - mypy 0.910\r\n - [Typeshed from mypy 0.910](https://github.com/python/mypy/tree/v0.910/mypy/typeshed)\r\n\r\n\r\nAll code can be found [here](https://gitlab.com/aucampia/issues/202109-pythonio/-/tree/issue-typeshed-incompatible_io).\r\n\r\n\r\n [python/io.IOBase]: https://docs.python.org/3.7/library/io.html#io.IOBase\r\n [python/io.RawIOBase]: https://docs.python.org/3.7/library/io.html#io.RawIOBase\r\n [python/io.BufferedIOBase]: https://docs.python.org/3.7/library/io.html#io.BufferedIOBase\r\n [python/io.TextIOBase]: https://docs.python.org/3.7/library/io.html#io.TextIOBase\r\n\r\n [python/io.FileIO]: https://docs.python.org/3.7/library/io.html#io.FileIO\r\n\r\n [python/typing.IO]: https://docs.python.org/3.7/library/typing.html#typing.IO\r\n [python/typing.TextIO]: https://docs.python.org/3.7/library/typing.html#typing.TextIO\r\n [python/typing.BinaryIO]: https://docs.python.org/3.7/library/typing.html#typing.BinaryIO\r\n\r\n [python/open]: https://docs.python.org/3.7/library/functions.html#open","author":{"url":"https://github.com/aucampia","@type":"Person","name":"aucampia"},"datePublished":"2021-09-26T14:41:06Z","url":"https://github.com/python/typeshed/issues/6077"}
| 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:246f791a-cb16-5321-86a5-72a9377ed5ec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C6A:2A4B31:12DE24:19885A:6B001EDE |
| html-safe-nonce | 3b91621fd787e6abca6d8f2be92fca7b7f8d3811513d8a0cefe405578c1bffef |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzZBOjJBNEIzMToxMkRFMjQ6MTk4ODVBOjZCMDAxRURFIiwidmlzaXRvcl9pZCI6IjQ1ODQ1Mzc3MDM3NDMyMzM3NTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ea8372e757416fb0787f0a753cfe77793e0b964ca25eeb6a81635fc6727353cb |
| hovercard-subject-tag | issue:1007410514 |
| 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/typeshed/6077/issue_layout |
| twitter:image | https://opengraph.githubassets.com/82f6580eb41ed16621432afeb21edacc72d07cc53f00cbf6f57589a23afdde6f/python/typeshed/issues/6077 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/82f6580eb41ed16621432afeb21edacc72d07cc53f00cbf6f57589a23afdde6f/python/typeshed/issues/6077 |
| og:image:alt | Python documentation for the open() builtin indicates that open() returns types from the io.IOBase hierarchy: The type of file object returned by the open() function depends on the mode. When open(... |
| 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/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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