Title: Allow returning `AnyOf` rather than `Any` for amiguous overloads. · Issue #2196 · python/typing · GitHub
Open Graph Title: Allow returning `AnyOf` rather than `Any` for amiguous overloads. · Issue #2196 · python/typing
X Title: Allow returning `AnyOf` rather than `Any` for amiguous overloads. · Issue #2196 · python/typing
Description: Currently, the typing spec for overloads, step 5, states that Once this filtering process is applied for all arguments, examine the return types of the remaining overloads. If these return types include type variables, they should be rep...
Open Graph Description: Currently, the typing spec for overloads, step 5, states that Once this filtering process is applied for all arguments, examine the return types of the remaining overloads. If these return types in...
X Description: Currently, the typing spec for overloads, step 5, states that Once this filtering process is applied for all arguments, examine the return types of the remaining overloads. If these return types in...
Opengraph URL: https://github.com/python/typing/issues/2196
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow returning `AnyOf` rather than `Any` for amiguous overloads.","articleBody":"Currently, the typing spec for overloads, [step 5](https://typing.python.org/en/latest/spec/overload.html#step-5), states that\n\n> Once this filtering process is applied for all arguments, examine the return types of the remaining overloads. If these return types include type variables, they should be replaced with their solved types. If the resulting return types for all remaining overloads are [equivalent](https://typing.python.org/en/latest/spec/glossary.html#term-equivalent), proceed to step 6.\n>\n> If the return types are not equivalent, overload matching is ambiguous. In this case, assume a return type of Any and stop.\n\nHowever, couldn't this rule be relaxed to returning the union of the return type of all matching overloads? For instance, consider this example derived from a real world use case (numpy scalar ops)\n\n```python\nfrom typing import Any, overload, assert_type\n\nclass A[T]: # covariant\n def get(self) -> T: ...\n\n@overload\ndef op(l: A[None], r: A[None]) -> A[None]: ...\n@overload\ndef op(l: A[None], r: A[Any]) -> A[None]: ...\n@overload\ndef op(l: A[Any], r: A[None]) -> A[None]: ...\n@overload\ndef op(l: A[Any], r: A[Any]) -> A[Any]: ...\n\ndef test(x: A[None], y: A[Any]) -> None:\n assert_type(op(x, x), A[None]) # spec: ✅️ \n assert_type(op(x, y), A[None]) # spec: ✅️\n assert_type(op(y, x), A[None]) # spec: ✅️\n assert_type(op(y, y), A[Any] | A[None]) # spec: ❌️ (expected Any)\n```\n\nAccording to the rule stated above, `op(A[Any], A[Any])` should be inferred to as `Any`, because the overloads are ambious and `A[Any]` is not [equivalent](https://typing.python.org/en/latest/spec/glossary.html#term-equivalent) to `A[None]` as not all materializations of `A[Any]` can be assigned to `A[None]`.\n\nHowever inferring `Any` loses deducible information: we know that no matter what, the return type must be an `A`. So in principle the return type should be `A[Any] | A[None]`, because if either the left argument or the right argument materializes to `A[None]`, then we get `A[None]` and otherwise if they materialize to something else we get `A[Any]`.","author":{"url":"https://github.com/randolf-scholz","@type":"Person","name":"randolf-scholz"},"datePublished":"2026-02-25T18:23:46Z","url":"https://github.com/python/typing/issues/2196"}
| 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:1e4e8bbc-46f2-4b92-3a01-58496092fa9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF4E:3E8A12:1B5B4A:2313D7:6AFF83B1 |
| html-safe-nonce | 7f40574ac5301c3f27bd265f20cbcd653e746a7af0fdfa7dce36a3b50b81ee48 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjRFOjNFOEExMjoxQjVCNEE6MjMxM0Q3OjZBRkY4M0IxIiwidmlzaXRvcl9pZCI6Ijg2ODIyNTQ5MzMwMDI1MTk0NzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 767e065527a67c97c171c09cd1853cb7c16e73dc6bb1fca553b2b311adaf1894 |
| hovercard-subject-tag | issue:3991025930 |
| 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/typing/2196/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0b0d57aecf17b014d2c9f7b572d0a14994b5b64a9845eb454af4309069a9a97f/python/typing/issues/2196 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0b0d57aecf17b014d2c9f7b572d0a14994b5b64a9845eb454af4309069a9a97f/python/typing/issues/2196 |
| og:image:alt | Currently, the typing spec for overloads, step 5, states that Once this filtering process is applied for all arguments, examine the return types of the remaining overloads. If these return types in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | randolf-scholz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 19488db4031f00b04ea79b4c70b5dd32cfa873a8d20b3877f3896b8e2d195946 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 7952f5285f4ba685d70c4c7540ad3cc9dc9746b8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width