Title: TypeGuard type narrowing on returning False · Issue #996 · python/typing · GitHub
Open Graph Title: TypeGuard type narrowing on returning False · Issue #996 · python/typing
X Title: TypeGuard type narrowing on returning False · Issue #996 · python/typing
Description: I've faced limitation of not narrowing types when trying to add types in https://github.com/openai/gym/blob/8e5ae02ab13a89c976ce7b1278c21f755dfa4bd2/gym/spaces/box.py#L33 - (np.isscalar)[https://numpy.org/doc/stable/reference/generated/n...
Open Graph Description: I've faced limitation of not narrowing types when trying to add types in https://github.com/openai/gym/blob/8e5ae02ab13a89c976ce7b1278c21f755dfa4bd2/gym/spaces/box.py#L33 - (np.isscalar)[https://nu...
X Description: I've faced limitation of not narrowing types when trying to add types in https://github.com/openai/gym/blob/8e5ae02ab13a89c976ce7b1278c21f755dfa4bd2/gym/spaces/box.py#L33 - (np.isscalar)[https:...
Opengraph URL: https://github.com/python/typing/issues/996
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeGuard type narrowing on returning False","articleBody":"\u003c!-- Please describe the problem you face as well as your idea below, ideally using examples for both. -->\r\n\r\nI've faced limitation of not narrowing types when trying to add types in https://github.com/openai/gym/blob/8e5ae02ab13a89c976ce7b1278c21f755dfa4bd2/gym/spaces/box.py#L33 - (np.isscalar)[https://numpy.org/doc/stable/reference/generated/numpy.isscalar.html] returning False rules out `SupportsFloat` type, and given input type `SupportsFloat | np.ndarray` I now know it was narrowed down to np.ndarray.\r\n\r\nNumpy has not added type guard yet in its stub, but this is the logical type for this function once all supported python versions will have it. Yet with current syntax it's not possible to describe the function narrowing type by returning False.\r\n\r\nProposal: give TypeGuard second optional argument: TypeGuard[A, B] should narrow argument type A | B to A if returning True, and to B if returning False. Old syntax should still work, where one can have TypeGuard[A] work as it does currently.\r\n\r\nI didn't find this in rejected ideas of https://www.python.org/dev/peps/pep-0647/, so wanted to share. I'm not sure how much of stdlib could use this, but it definitely helps to be able to write function in the most semantically convenient way. I can express both \"is_x\" and \"is_not_x\" function in typesystem if this is added. This could have synergy with NonType currently being discussed.\r\n\r\nUnclear yet: how best to describe situation where no type narrowing occurs on True return, but only on False. Maybe `TypeGuard[Any, B] ` could have this semantics.\r\n","author":{"url":"https://github.com/ikamensh","@type":"Person","name":"ikamensh"},"datePublished":"2021-12-27T15:44:57Z","url":"https://github.com/python/typing/issues/996"}
| 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:ef5fbb64-1ba8-592d-c28a-ffc1ffc7f824 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D436:16AB79:4F12DCB:6431E4D:6B026E41 |
| html-safe-nonce | 5daf99801c2c8c60937abfb3a7b6504b9f02d5db697d7368a10c4e05472809d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDM2OjE2QUI3OTo0RjEyRENCOjY0MzFFNEQ6NkIwMjZFNDEiLCJ2aXNpdG9yX2lkIjoiNzEwNzY4MTM4MzU0NzU2MzU4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f5f6c2b6d7fea69ceb43ea55d2e3509957e6687f9be5e4a245b0cc4c471efb06 |
| hovercard-subject-tag | issue:1089322627 |
| 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/996/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d133522eeaf556ff4d0520d551b5e128d29a3e0830358ac59a3d81768e6190d/python/typing/issues/996 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d133522eeaf556ff4d0520d551b5e128d29a3e0830358ac59a3d81768e6190d/python/typing/issues/996 |
| og:image:alt | I've faced limitation of not narrowing types when trying to add types in https://github.com/openai/gym/blob/8e5ae02ab13a89c976ce7b1278c21f755dfa4bd2/gym/spaces/box.py#L33 - (np.isscalar)[https://nu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ikamensh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width