René's URL Explorer Experiment


Title: Shouldn't the return type of `__reversed__` be an `Iterable[T]` instead of `Iterator[T]`? · Issue #13218 · python/typeshed · GitHub

Open Graph Title: Shouldn't the return type of `__reversed__` be an `Iterable[T]` instead of `Iterator[T]`? · Issue #13218 · python/typeshed

X Title: Shouldn't the return type of `__reversed__` be an `Iterable[T]` instead of `Iterator[T]`? · Issue #13218 · python/typeshed

Description: Currently the return type of Reversible.__reversed__ is typed as Iterator[T], which suggests that the returned instance must have both an __next__ and an __iter__ method. typeshed/stdlib/typing.pyi Lines 446 to 448 in 0e9c9e1 class Rever...

Open Graph Description: Currently the return type of Reversible.__reversed__ is typed as Iterator[T], which suggests that the returned instance must have both an __next__ and an __iter__ method. typeshed/stdlib/typing.pyi...

X Description: Currently the return type of Reversible.__reversed__ is typed as Iterator[T], which suggests that the returned instance must have both an __next__ and an __iter__ method. typeshed/stdlib/typing.pyi...

Opengraph URL: https://github.com/python/typeshed/issues/13218

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Shouldn't the return type of `__reversed__` be an `Iterable[T]` instead of `Iterator[T]`?","articleBody":"Currently the return type of `Reversible.__reversed__` is typed as `Iterator[T]`, which suggests that the returned instance must have both an `__next__` and an `__iter__` method.\r\n\r\nhttps://github.com/python/typeshed/blob/0e9c9e1362959512a880abbf1275471b0d76924f/stdlib/typing.pyi#L446-L448\r\n\r\nHowever, the following code seems to work fine at runtime:\r\n\r\n```py\r\nfrom dataclasses import dataclass\r\nfrom typing import Iterator, Iterable\r\n\r\n@dataclass\r\nclass MyIter:\r\n    _values: list[int]\r\n    \r\n    def __iter__(self) -> Iterator[int]:\r\n        return iter(self._values)\r\n    \r\n    def __reversed__(self) -> Iterator[int]:\r\n        return MyIter(self._values[::-1])\r\n\r\nmy_iter = MyIter([1, 2, 3])\r\nfor x in my_iter:\r\n    print(x)\r\nfor x in reversed(my_iter):\r\n    print(x)\r\n```\r\n\r\nNote that `__reversed__` here returns a `MyIter` instance, which has an `__iter__` method, but **no** `__next__`, i.e., it is an `Iterable` but not an `Iterator`. The Python interpreter seems to deal with that fine at runtime, i.e., it doesn't actually seem to need the `__next__` method. This is slightly surprising, because the [docs](https://docs.python.org/3/reference/datamodel.html#object.__reversed__) specify:\r\n\r\n> It should return a new iterator object that iterates over all the objects in the container in reverse order.\r\n\r\nI.e., it doesn't use the word \"iterable object\".\r\n\r\nUnfortunately, the current signature of `__reversed__` means that this example does not type check: Obviously the type checker has to complain about the `return MyIter(...)` line, because `MyIter` is indeed only an `Iterable` ([example on mypy playground](https://mypy-play.net/?mypy=latest&python=3.12&gist=aa4a4f62b913e5e4ae5de35cc4051439)):\r\n\r\n```\r\nmain.py:13: error: Incompatible return value type (got \"MyIter\", expected \"Iterator[int]\")  [return-value]\r\nmain.py:13: note: \"MyIter\" is missing following \"Iterator\" protocol member:\r\nmain.py:13: note:     __next__\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\nNow I'm wondering if the signature should actually be `def __reversed__(self) -> Iterable[int]` to lessen the requirement and match the runtime behavior?\r\n\r\nNote that simply changing the return type to `Iterable` on user side means that the return statement now type checks, but then all usages (`reversed(my_iter)`) stop to type check because the type checker will no longer consider `MyIter` as a valid `Reversible` ([modified example on mypy playground](https://mypy-play.net/?mypy=latest&python=3.12&gist=769a9f913f01f7f8cb325278dd00b958)):\r\n\r\n```\r\nmain.py:18: error: No overload variant of \"reversed\" matches argument type \"MyIter\"  [call-overload]\r\nmain.py:18: note: Possible overload variants:\r\nmain.py:18: note:     def [_T] __new__(cls, Reversible[_T], /) -> reversed[_T]\r\nmain.py:18: note:     def [_T] __new__(cls, SupportsLenAndGetItem[_T], /) -> reversed[_T]\r\nFound 1 error in 1 file (checked 1 source file)\r\n```","author":{"url":"https://github.com/bluenote10","@type":"Person","name":"bluenote10"},"datePublished":"2024-12-07T22:29:08Z","url":"https://github.com/python/typeshed/issues/13218"}

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:afd0d186-4611-a05a-57ac-5be5854912bd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8B30:251AC0:5372A1:6F49DE:6B002F64
html-safe-nonce9126e0b3674c5c4bc0ba4a79e80839138712a38b5d075172d894e1a64e5f7636
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjMwOjI1MUFDMDo1MzcyQTE6NkY0OURFOjZCMDAyRjY0IiwidmlzaXRvcl9pZCI6IjQ0NjQ3NTIxODE1MjU5OTM5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac6bc20b8b59fcc4e88414750f9be0cdfbfdbaac61771bf89b9ae468d976154ace
hovercard-subject-tagissue:2724838536
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/13218/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8a324f79dfabafe3e89cd84ca14eae0cf063ba7a0afde067a996ed0c0045e268/python/typeshed/issues/13218
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8a324f79dfabafe3e89cd84ca14eae0cf063ba7a0afde067a996ed0c0045e268/python/typeshed/issues/13218
og:image:altCurrently the return type of Reversible.__reversed__ is typed as Iterator[T], which suggests that the returned instance must have both an __next__ and an __iter__ method. typeshed/stdlib/typing.pyi...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamebluenote10
hostnamegithub.com
expected-hostnamegithub.com
None7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0
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
releasef3f3a43ca095cdea3abbb394112e971f9fdb94fa
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typeshed/issues/13218#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F13218
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%2F13218
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/13218
Reloadhttps://github.com/python/typeshed/issues/13218
Reloadhttps://github.com/python/typeshed/issues/13218
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/13218
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/13218
Shouldn't the return type of __reversed__ be an Iterable[T] instead of Iterator[T]?https://github.com/python/typeshed/issues/13218#top
https://github.com/bluenote10
bluenote10https://github.com/bluenote10
on Dec 7, 2024https://github.com/python/typeshed/issues/13218#issue-2724838536
typeshed/stdlib/typing.pyihttps://github.com/python/typeshed/blob/0e9c9e1362959512a880abbf1275471b0d76924f/stdlib/typing.pyi#L446-L448
0e9c9e1https://github.com/python/typeshed/commit/0e9c9e1362959512a880abbf1275471b0d76924f
docshttps://docs.python.org/3/reference/datamodel.html#object.__reversed__
example on mypy playgroundhttps://mypy-play.net/?mypy=latest&python=3.12&gist=aa4a4f62b913e5e4ae5de35cc4051439
modified example on mypy playgroundhttps://mypy-play.net/?mypy=latest&python=3.12&gist=769a9f913f01f7f8cb325278dd00b958
https://github.com/tungol
tungolhttps://github.com/tungol
on Dec 19, 2024https://github.com/python/typeshed/issues/13218#issuecomment-2553132291
https://github.com/tungol
tungolhttps://github.com/tungol
on Dec 19, 2024https://github.com/python/typeshed/issues/13218#issuecomment-2553143865
https://github.com/TeamSpen210
TeamSpen210https://github.com/TeamSpen210
on Dec 19, 2024https://github.com/python/typeshed/issues/13218#issuecomment-2553184798
https://github.com/python/cpython/blob/48c70b8f7dfd00a018abbac50ea987f54fa4db51/Objects/enumobject.c#L369-373]https://github.com/python/cpython/blob/48c70b8f7dfd00a018abbac50ea987f54fa4db51/Objects/enumobject.c#L369-373%5D
https://github.com/tungol
tungolhttps://github.com/tungol
on Dec 19, 2024https://github.com/python/typeshed/issues/13218#issuecomment-2553185428
#11645https://github.com/python/typeshed/issues/11645
https://github.com/srittau
srittauhttps://github.com/srittau
on Dec 19, 2024https://github.com/python/typeshed/issues/13218#issuecomment-2553223196
https://github.com/jorenham
jorenhamhttps://github.com/jorenham
on Dec 28, 2024https://github.com/python/typeshed/issues/13218#issuecomment-2564334030
#11646https://github.com/python/typeshed/pull/11646
https://github.com/eschalkargans
eschalkarganshttps://github.com/eschalkargans
on Mar 14, 2025https://github.com/python/typeshed/issues/13218#issuecomment-2724530320
https://github.com/Akuli
Akulihttps://github.com/Akuli
on Mar 16, 2025https://github.com/python/typeshed/issues/13218#issuecomment-2727277003
@eschalkarganshttps://github.com/eschalkargans
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typeshed/issues/13218
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typeshed/issues/13218
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.