Title: incorrect annotations for `builtins.reversed` · Issue #11645 · python/typeshed · GitHub
Open Graph Title: incorrect annotations for `builtins.reversed` · Issue #11645 · python/typeshed
X Title: incorrect annotations for `builtins.reversed` · Issue #11645 · python/typeshed
Description: The builtins.reversed.__new__ signature is currently annotated like (Reversible[T] | SupportsLenAndGetItem[T]) -> Iterator[T]: typeshed/stdlib/builtins.pyi Lines 1639 to 1646 in 3d26992 class reversed(Iterator[_T]): @overload def __new__...
Open Graph Description: The builtins.reversed.__new__ signature is currently annotated like (Reversible[T] | SupportsLenAndGetItem[T]) -> Iterator[T]: typeshed/stdlib/builtins.pyi Lines 1639 to 1646 in 3d26992 class rever...
X Description: The builtins.reversed.__new__ signature is currently annotated like (Reversible[T] | SupportsLenAndGetItem[T]) -> Iterator[T]: typeshed/stdlib/builtins.pyi Lines 1639 to 1646 in 3d26992 class re...
Opengraph URL: https://github.com/python/typeshed/issues/11645
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"incorrect annotations for `builtins.reversed`","articleBody":"The `builtins.reversed.__new__` signature is currently annotated like `(Reversible[T] | SupportsLenAndGetItem[T]) -> Iterator[T]`:\r\nhttps://github.com/python/typeshed/blob/3d26992d1562a5175fb8fd91ca63817e55feee56/stdlib/builtins.pyi#L1639-L1646\r\n\r\nIt has two problems:\r\n\r\n## argument of type `SupportsLenAndGetItem[T]` \r\n\r\nThis should return a `reversed[T]` instance (i.e. `Self`), instead of \"just\" an `Iterable[T]`, which is unnecessarily loose.\r\n\r\n## argument with type that implement `__reversed__`\r\n\r\nThis is only handled if the argument's `__reversed__` returns an `Iterator[T]`, i.e. arguments that are `Reversible[T]`. But in reality, `reversed(x)` is equivalent to `x.__reversed__()` (if `__reversed__` exists). To illustrate:\r\n\r\n```pycon\r\n>>> class Revver[X]:\r\n... x: X\r\n... def __init__(self, x: X):\r\n... self.x = x\r\n... def __reversed__(self) -> X:\r\n... return self.x\r\n... \r\n>>> reversed(Revver(42))\r\n42\r\n```\r\n\r\nHowever, this case isn't handled because `Revver[X]` cannot be expressed as a `Reversible[?]`.\r\n\r\n---\r\n\r\nSo instead, it should look something like this:\r\n\r\n```python\r\nclass SupportsReversed[T]:\r\n def __reversed__(self) -> T: ...\r\n\r\nclass reversed[T](Iterator[T]):\r\n @overload\r\n def __new__[R](cls, sequence: SupportsReversed[R], /) -> R: ...\r\n @overload\r\n def __new__(cls, sequence: SupportsLenAndGetItem[T], /) -> Self: ...\r\n def __length_hint__(self) -> int: ...\r\n # btw, this is why protocols shouldn't be mixed with abstract classes:\r\n def __iter__(self) -> Self: ...\r\n def __next__(self) -> T: ...\r\n```\r\n\r\n---\r\n\r\nLet me know if I should make this into PR :)","author":{"url":"https://github.com/jorenham","@type":"Person","name":"jorenham"},"datePublished":"2024-03-21T16:30:51Z","url":"https://github.com/python/typeshed/issues/11645"}
| 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:8b6d5392-c4b4-9fd3-e448-e40eda14075a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 92BC:33DDEF:1EE8699:2A42017:6B0021F0 |
| html-safe-nonce | 1371989afe4d59798354462628b2ad0eb804bca632c768c9a93fff5ab270fc84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MkJDOjMzRERFRjoxRUU4Njk5OjJBNDIwMTc6NkIwMDIxRjAiLCJ2aXNpdG9yX2lkIjoiMjMzODY2OTY1MjIxMDYyMjk2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b3c61dcdbc4eb1f6ed18426234e9152ece6aef4f0d661a9282a5438fb36a49cb |
| hovercard-subject-tag | issue:2200643493 |
| 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/11645/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fe631ae87c2db961d03440c0c7f4fe2b20a16152f043eab32d520ff5aec30cf9/python/typeshed/issues/11645 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fe631ae87c2db961d03440c0c7f4fe2b20a16152f043eab32d520ff5aec30cf9/python/typeshed/issues/11645 |
| og:image:alt | The builtins.reversed.__new__ signature is currently annotated like (Reversible[T] | SupportsLenAndGetItem[T]) -> Iterator[T]: typeshed/stdlib/builtins.pyi Lines 1639 to 1646 in 3d26992 class rever... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jorenham |
| 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