René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:246f791a-cb16-5321-86a5-72a9377ed5ec
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8C6A:2A4B31:12DE24:19885A:6B001EDE
html-safe-nonce3b91621fd787e6abca6d8f2be92fca7b7f8d3811513d8a0cefe405578c1bffef
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzZBOjJBNEIzMToxMkRFMjQ6MTk4ODVBOjZCMDAxRURFIiwidmlzaXRvcl9pZCI6IjQ1ODQ1Mzc3MDM3NDMyMzM3NTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacea8372e757416fb0787f0a753cfe77793e0b964ca25eeb6a81635fc6727353cb
hovercard-subject-tagissue:1007410514
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/6077/issue_layout
twitter:imagehttps://opengraph.githubassets.com/82f6580eb41ed16621432afeb21edacc72d07cc53f00cbf6f57589a23afdde6f/python/typeshed/issues/6077
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/82f6580eb41ed16621432afeb21edacc72d07cc53f00cbf6f57589a23afdde6f/python/typeshed/issues/6077
og:image:altPython 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameaucampia
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
turbo-cache-controlno-preview
go-importgithub.com/python/typeshed git https://github.com/python/typeshed.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id31696383
octolytics-dimension-repository_nwopython/typeshed
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id31696383
octolytics-dimension-repository_network_root_nwopython/typeshed
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typeshed/issues/6077#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F6077
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F6077
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Ftypeshed
Reloadhttps://github.com/python/typeshed/issues/6077
Reloadhttps://github.com/python/typeshed/issues/6077
Reloadhttps://github.com/python/typeshed/issues/6077
pythonhttps://github.com/python
typeshedhttps://github.com/python/typeshed
Codehttps://github.com/python/typeshed
Issues196 (196)https://github.com/python/typeshed/issues
Pull requests251 (251)https://github.com/python/typeshed/pulls
Actionshttps://github.com/python/typeshed/actions
Security and qualityhttps://github.com/python/typeshed/security
Insightshttps://github.com/python/typeshed/insights
https://github.com/python/typeshed
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/6077
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/6077
typing.IO and io.BaseIO type hierarchies are incompatiblehttps://github.com/python/typeshed/issues/6077#top
https://github.com/aucampia
aucampiahttps://github.com/aucampia
on Sep 26, 2021https://github.com/python/typeshed/issues/6077#issue-1007410514
open()https://docs.python.org/3.7/library/functions.html#open
io.IOBasehttps://docs.python.org/3.7/library/io.html#io.IOBase
typing.IO[...]https://docs.python.org/3.7/library/typing.html#typing.IO
typing.IO[...]https://docs.python.org/3.7/library/typing.html#typing.IO
python/mypy#11193https://github.com/python/mypy/issues/11193
#6061https://github.com/python/typeshed/issues/6061
#6076https://github.com/python/typeshed/issues/6076
Typeshed from mypy 0.910https://github.com/python/mypy/tree/v0.910/mypy/typeshed
herehttps://gitlab.com/aucampia/issues/202109-pythonio/-/tree/issue-typeshed-incompatible_io
https://github.com/aucampia
aucampiahttps://github.com/aucampia
on Sep 26, 2021https://github.com/python/typeshed/issues/6077#issuecomment-927377192
Why does mypy complain about TextIOWrapper receiving GzipFile as argument 1?https://stackoverflow.com/questions/58394410/why-does-mypy-complain-about-textiowrapper-receiving-gzipfile-as-argument-1
https://github.com/srittau
srittauhttps://github.com/srittau
on Sep 27, 2021https://github.com/python/typeshed/issues/6077#issuecomment-927598835
srittauhttps://github.com/srittau
completedhttps://github.com/python/typeshed/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 27, 2021https://github.com/python/typeshed/issues/6077#event-5363441289
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typeshed/issues/6077
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typeshed/issues/6077
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.