Title: Supporting Ellipsis as a value · Issue #7818 · python/mypy · GitHub
Open Graph Title: Supporting Ellipsis as a value · Issue #7818 · python/mypy
X Title: Supporting Ellipsis as a value · Issue #7818 · python/mypy
Description: We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean "everything else". But it's not possible to use fully with type checking, since mypy doesn't exclude it from a type when it's been excluded with an if, as...
Open Graph Description: We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean "everything else". But it's not possible to use fully with type checking, since mypy doesn't exclude it from a typ...
X Description: We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean "everything else". But it's not possible to use fully with type checking, since mypy doesn't exc...
Opengraph URL: https://github.com/python/mypy/issues/7818
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Supporting Ellipsis as a value","articleBody":"We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean \"everything else\". \r\n\r\nBut it's not possible to use fully with type checking, since mypy doesn't exclude it from a type when it's been excluded with an `if`, as it does for `None`, or `Enum`s per https://github.com/python/typing/pull/240).\r\n\r\nI've included below a MCVE of the behavior below, and here's an example of how we use it, given @gvanrossum has [already asked](https://github.com/python/mypy/issues/2180) whether it's important to use an Ellipsis:\r\n\r\n\r\n\u003cdetails>\r\n\r\nFor example, to transpose an array, these are equivalent:\r\n\r\n```python\r\n\r\nIn [1]: import xarray as xr\r\n\r\nIn [2]: ds = xr.tutorial.scatter_example_dataset()\r\n\r\nIn [3]: ds\r\nOut[3]:\r\n\u003cxarray.Dataset>\r\nDimensions: (w: 4, x: 3, y: 11, z: 4)\r\nCoordinates:\r\n * x (x) int64 0 1 2\r\n * y (y) float64 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0\r\n * z (z) int64 0 1 2 3\r\n * w (w) \u003cU5 'one' 'two' 'three' 'five'\r\nData variables:\r\n A (x, y, z, w) float64 0.02074 0.04807 -0.1059 ... -0.1809 -0.04862\r\n B (x, y, z, w) float64 0.0 0.0 0.0 0.0 ... 1.406 1.414 1.368 1.408\r\n\r\nIn [4]: ds.transpose('w','x','y','z')\r\nOut[4]:\r\n\u003cxarray.Dataset>\r\nDimensions: (w: 4, x: 3, y: 11, z: 4)\r\nCoordinates:\r\n * x (x) int64 0 1 2\r\n * y (y) float64 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0\r\n * z (z) int64 0 1 2 3\r\n * w (w) \u003cU5 'one' 'two' 'three' 'five'\r\nData variables:\r\n A (w, x, y, z) float64 0.02074 0.02074 0.02074 ... -0.03076 -0.04862\r\n B (w, x, y, z) float64 0.0 0.002074 0.004147 ... 1.403 1.405 1.408\r\n\r\n\r\nIn [5]: ds.transpose('w',...) # use an Ellipsis to indicate 'all other dimensions'\r\nOut[5]:\r\n\u003cxarray.Dataset>\r\nDimensions: (w: 4, x: 3, y: 11, z: 4)\r\nCoordinates:\r\n * x (x) int64 0 1 2\r\n * y (y) float64 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0\r\n * z (z) int64 0 1 2 3\r\n * w (w) \u003cU5 'one' 'two' 'three' 'five'\r\nData variables:\r\n A (w, x, y, z) float64 0.02074 0.02074 0.02074 ... -0.03076 -0.04862\r\n B (w, x, y, z) float64 0.0 0.002074 0.004147 ... 1.403 1.405 1.408\r\n```\r\n\r\n\u003c/details>\r\n\r\n\r\n* Are you reporting a bug, or opening a feature request?\r\nI think it's a bug but https://github.com/python/mypy/issues/2180 suggests it may be a \"Will not fix\". Wanted to repost it here given the logic above\r\n* Please insert below the code you are checking with mypy,\r\n or a mock-up repro if the source is private. We would appreciate\r\n if you try to simplify your case to a minimal repro.\r\n\r\n```python\r\nimport builtins\r\nfrom typing import List, Union\r\n\r\n\r\ndef fun(x: Union[builtins.ellipsis, List], y: List):\r\n if x is not Ellipsis:\r\n y = x # error: Incompatible types in assignment (expression has type \"Union[ellipsis, List[Any]]\", variable has type \"List[Any]\")\r\n\r\n return y\r\n\r\n# another attempt:\r\ndef fun2(x: Union[Ellipsis, List], y: List): # error: Variable \"builtins.Ellipsis\" is not valid as a type\r\n if x is not Ellipsis:\r\n y = x\r\n\r\n return y\r\n\r\n```\r\n* What is the actual behavior/output?\r\n```\r\ntest.py:7: error: Incompatible types in assignment (expression has type \"Union[ellipsis, List[Any]]\", variable has type \"List[Any]\")\r\ntest.py:12: error: Variable \"builtins.Ellipsis\" is not valid as a type\r\n```\r\n\r\n* What are the versions of mypy and Python you are using?\r\n Do you see the same issue after installing mypy from Git master?\r\n`mypy 0.740`\r\n\r\n\r\nThank you!","author":{"url":"https://github.com/max-sixty","@type":"Person","name":"max-sixty"},"datePublished":"2019-10-30T01:01:15Z","url":"https://github.com/python/mypy/issues/7818"}
| 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:fd4f10cf-58e0-db99-7e92-1bf4dfcaf327 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D51E:1F7AC:36A7A5:4CF0D8:6B005E3A |
| html-safe-nonce | bd88083df911443be821bdae962a84691903276d10d32ff674c552da9bc225a9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTFFOjFGN0FDOjM2QTdBNTo0Q0YwRDg6NkIwMDVFM0EiLCJ2aXNpdG9yX2lkIjoiNDYzMzM1NjY2ODU1NjU2NjA3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9336a6ff2c8c94f66353416f66adb0a39a906ad24b022f242c9cfe40dc34ca1d |
| hovercard-subject-tag | issue:514326044 |
| 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/mypy/7818/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ae64c701e0fa24f96a6dde20014667141705eab050342c2ad9f6f84c5a00e8d7/python/mypy/issues/7818 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ae64c701e0fa24f96a6dde20014667141705eab050342c2ad9f6f84c5a00e8d7/python/mypy/issues/7818 |
| og:image:alt | We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean "everything else". But it's not possible to use fully with type checking, since mypy doesn't exclude it from a typ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | max-sixty |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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 | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width