Title: Introduce a Not type · Issue #801 · python/typing · GitHub
Open Graph Title: Introduce a Not type · Issue #801 · python/typing
X Title: Introduce a Not type · Issue #801 · python/typing
Description: This is a continuation of this discussion on the Intersection issue. I have a different use case for the Not[...] type, but it is related to the original discussion. My usecase is to be able to tell the type checker that two types can ne...
Open Graph Description: This is a continuation of this discussion on the Intersection issue. I have a different use case for the Not[...] type, but it is related to the original discussion. My usecase is to be able to tel...
X Description: This is a continuation of this discussion on the Intersection issue. I have a different use case for the Not[...] type, but it is related to the original discussion. My usecase is to be able to tel...
Opengraph URL: https://github.com/python/typing/issues/801
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Introduce a Not type","articleBody":"This is a continuation of this [discussion on the Intersection issue](https://github.com/python/typing/issues/213#issuecomment-344132917).\r\n\r\nI have a different use case for the `Not[...]` type, but it is related to the original discussion. My usecase is to be able to tell the type checker that two types can never have an intersection. The specific case is for the [phantom-types](https://github.com/antonagestam/phantom-types/blob/main/phantom/sized.py#L80) library where there are two types `NonEmpty` and `Empty`.\r\n\r\nCurrently there is no way to tell mypy that a variable can't both be `NonEmpty` and `Empty`, so this code passes without error:\r\n\r\n```python\r\nfrom __future__ import annotations\r\nfrom phantom.sized import NonEmpty, Empty\r\n\r\ni: tuple[int, ...] = ()\r\n\r\nassert isinstance(i, Empty)\r\nassert isinstance(i, NonEmpty)\r\n\r\nreveal_type(i)\r\n```\r\n\r\nThe output shows that mypy interprets `i` as an intersection between `tuple`, `Empty` and `NonEmpty`:\r\n\r\n```\r\nintx.py:9: note: Revealed type is 'intx.\u003csubclass of \"tuple\", \"Empty\", and \"NonEmpty\">'\r\n```\r\n\r\nOf course this code would error at runtime, but it would be preferable if this could be expressible so that type checkers can give an error here, similarly to how mypy gives errors for unreachable code.\r\n\r\nA suggestion for how this could be expressed is to allow subclassing a `Not` type, so simplified the definitions of the two mentioned types would like this:\r\n\r\n```python\r\nclass NonEmpty:\r\n ...\r\n\r\nclass Empty(Not[NonEmpty]):\r\n ...\r\n```\r\n\r\nThere are also other types in the library that would benefit from this like [`TZAware` and `TZNaive`](https://github.com/antonagestam/phantom-types/blob/main/phantom/datetime.py).\r\n\r\n**Edit**: To clarify, I think that this type would be valuable even without an `Intersection` type which is why I opened a separate issue. This is because (as showed in the example) there already _implicit_ intersections in mypy. I don't know how the other type checkers treats this.","author":{"url":"https://github.com/antonagestam","@type":"Person","name":"antonagestam"},"datePublished":"2021-04-22T08:09:41Z","url":"https://github.com/python/typing/issues/801"}
| 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:6a71d6df-7f5b-e02c-1064-cfd1570128b8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0EA:D064:2243A3A:2E853C5:6B002518 |
| html-safe-nonce | 7d0795b91c7947cee9402abd3066399a43758ce6b4ef05313f24822fb57f6bdc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMEVBOkQwNjQ6MjI0M0EzQToyRTg1M0M1OjZCMDAyNTE4IiwidmlzaXRvcl9pZCI6Ijg4NjQwOTM2NjQ5NDc3NDE5NzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2d3dd58119f817cf1b098b3064c265173e9affebbedea4a2a531f67b8f264b7e |
| hovercard-subject-tag | issue:864669499 |
| 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/801/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6091db1c2818caabab5344853c39b99acc0720b8fd586c085ecbce5360cb0a5f/python/typing/issues/801 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6091db1c2818caabab5344853c39b99acc0720b8fd586c085ecbce5360cb0a5f/python/typing/issues/801 |
| og:image:alt | This is a continuation of this discussion on the Intersection issue. I have a different use case for the Not[...] type, but it is related to the original discussion. My usecase is to be able to tel... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | antonagestam |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width