Title: Types as function namespaces · Issue #5980 · python/mypy · GitHub
Open Graph Title: Types as function namespaces · Issue #5980 · python/mypy
X Title: Types as function namespaces · Issue #5980 · python/mypy
Description: I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest solution is to write a module defining a ...
Open Graph Description: I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest s...
X Description: I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest s...
Opengraph URL: https://github.com/python/mypy/issues/5980
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Types as function namespaces","articleBody":"I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest solution is to write a module defining a `Pair` type along with the functions I need:\r\n\r\n```python\r\n# pair.py\r\nfrom typing import Tuple, TypeVar\r\n\r\nA = TypeVar('A')\r\nB = TypeVar('B')\r\n\r\nPair = Tuple[A, B]\r\n\r\ndef fst(pair: Pair[A, B]) -> A:\r\n a, _ = pair\r\n return a\r\n\r\ndef snd(pair: Pair[A, B]) -> B:\r\n _, b = pair\r\n return b\r\n```\r\n\r\nI would use my module like this:\r\n\r\n```python\r\n# test.py\r\nimport pair\r\nfrom pair import Pair\r\n\r\ndef f(p: Pair[int, float]) -> float:\r\n return pair.fst(p) + pair.snd(p)\r\n\r\np: Pair[int, float] = (1, 1.5)\r\nassert f(p) == 2.5\r\n```\r\n\r\nThis works and it typechecks with mypy. However, naming is a bit cumbersome with this approach. I can't use the `pair` identifier for `Pair` variables because I've already used it for my module, and I'm forced to resort to something like `p` instead. Also, it would be nicer if the functions `fst` and `snd` had a clearer association with the type `Pair`.\r\n\r\nI'd much rather write something like this for my test:\r\n\r\n```python\r\n# test.py\r\nfrom pair import Pair\r\n\r\ndef f(pair: Pair[int, float]) -> float:\r\n return Pair.fst(pair) + Pair.snd(pair)\r\n\r\npair: Pair[int, float] = (1, 1.5)\r\nassert f(pair) == 2.5\r\n```\r\n\r\nwhere `Pair` is now both a type *and* a namespace for associated functions.\r\n\r\nIs it possible for mypy to support this?\r\n\r\nI'm not making any syntax proposals here, but I'll note that the following does work at runtime:\r\n\r\n```python\r\nfrom typing import Tuple, TypeVar\r\n\r\nA = TypeVar('A')\r\nB = TypeVar('B')\r\n\r\nclass Pair(Tuple[A, B]):\r\n\r\n def fst(pair: Pair[A, B]) -> A:\r\n a, _ = pair\r\n return a\r\n\r\n def snd(pair: Pair[A, B]) -> B:\r\n _, b = pair\r\n return b\r\n\r\n\r\ndef f(pair: Pair[int, float]) -> float:\r\n return Pair.fst(pair) + Pair.snd(pair)\r\n\r\npair: Pair[int, float] = (1, 1.5)\r\nassert f(pair) == 2.5\r\n```\r\n\r\nUnfortunately, it does *not* typecheck with mypy, first of all because generic tuples are not allowed (https://github.com/python/mypy/issues/685). Second, even if generic tuples *were* supported, my assignment statement for `pair` would fail with an \"Incompatible type\" error because the expression has type `Tuple[int, float]` while the variable has type `Pair[int, float]`, and mypy sees `Pair` as a *subtype* of `Tuple` due to the class definition.\r\n\r\nI'm really curious to hear people's thoughts on this.\r\n\r\nThanks.","author":{"url":"https://github.com/jesaerys","@type":"Person","name":"jesaerys"},"datePublished":"2018-11-30T17:09:40Z","url":"https://github.com/python/mypy/issues/5980"}
| 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:d88adc29-086b-e37d-41dd-9c172f72081c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 90A4:115983:511C7D:6CC238:6B002FEF |
| html-safe-nonce | 4e541b87bf0e431e49df92c19b44b89b258dcd03e920922fc906d93db3db7b38 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MEE0OjExNTk4Mzo1MTFDN0Q6NkNDMjM4OjZCMDAyRkVGIiwidmlzaXRvcl9pZCI6IjIwNjIzMjE3OTI3MDk1NzA1NDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0cc0d89ed1be2eacc00a009985ac60f1af997e58f8fbf315dbb02c6ec57072dd |
| hovercard-subject-tag | issue:386274371 |
| 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/5980/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2c8345842a1cdc3436e64925c4f800d0b1e946488ae01e0be155f2a76d61bc64/python/mypy/issues/5980 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2c8345842a1cdc3436e64925c4f800d0b1e946488ae01e0be155f2a76d61bc64/python/mypy/issues/5980 |
| og:image:alt | I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jesaerys |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width