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
Domain: github.com
{"@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-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:afd0d186-4611-a05a-57ac-5be5854912bd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B30:251AC0:5372A1:6F49DE:6B002F64 |
| html-safe-nonce | 9126e0b3674c5c4bc0ba4a79e80839138712a38b5d075172d894e1a64e5f7636 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjMwOjI1MUFDMDo1MzcyQTE6NkY0OURFOjZCMDAyRjY0IiwidmlzaXRvcl9pZCI6IjQ0NjQ3NTIxODE1MjU5OTM5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6bc20b8b59fcc4e88414750f9be0cdfbfdbaac61771bf89b9ae468d976154ace |
| hovercard-subject-tag | issue:2724838536 |
| 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/13218/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8a324f79dfabafe3e89cd84ca14eae0cf063ba7a0afde067a996ed0c0045e268/python/typeshed/issues/13218 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8a324f79dfabafe3e89cd84ca14eae0cf063ba7a0afde067a996ed0c0045e268/python/typeshed/issues/13218 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bluenote10 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width