Title: Ability to use Callable type alias when annotating functions · Issue #1419 · python/typing · GitHub
Open Graph Title: Ability to use Callable type alias when annotating functions · Issue #1419 · python/typing
X Title: Ability to use Callable type alias when annotating functions · Issue #1419 · python/typing
Description: Hello. I would like to revive the discussion about Callable type aliases. The original discussion is here, and the related discussion about @declared_type is here and here. The original thread contains a great example of the problem and ...
Open Graph Description: Hello. I would like to revive the discussion about Callable type aliases. The original discussion is here, and the related discussion about @declared_type is here and here. The original thread cont...
X Description: Hello. I would like to revive the discussion about Callable type aliases. The original discussion is here, and the related discussion about @declared_type is here and here. The original thread cont...
Opengraph URL: https://github.com/python/typing/issues/1419
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Ability to use Callable type alias when annotating functions","articleBody":"Hello. I would like to revive the discussion about Callable type aliases. The original discussion is [here](https://github.com/python/mypy/issues/1641), and the related discussion about `@declared_type` is [here](https://github.com/python/mypy/issues/2087) and [here](https://github.com/python/mypy/pull/3291).\r\n\r\nThe original thread contains a great example of the problem and a proposed solution:\r\n\r\n```python\r\nfrom typing import Callable\r\nimport math\r\n\r\nActivationFunction = Callable[[float], float]\r\n\r\nsigmoid: ActivationFunction\r\ndef sigmoid(x):\r\n return 1 / (1 + math.exp(-x))\r\n\r\nrelu: ActivationFunction\r\ndef relu(x):\r\n return max(0, x)\r\n\r\n```\r\n\r\nAnother prosed solution is to use the `@declared_type` decorator:\r\n\r\n```python\r\n@declared_type(ActivationFunction)\r\ndef sigmoid(x):\r\n return 1 / (1 + math.exp(-x))\r\n\r\n@declared_type(ActivationFunction)\r\ndef relu(x):\r\n return max(0, x)\r\n```\r\n\r\nCurrently, this can be done with Protocols, but it's too verbose:\r\n\r\n```python\r\nfrom typing import Protocol\r\nimport math\r\n\r\nclass ActivationFunction(Protocol):\r\n def __call__(self, x: float) -> float:\r\n ...\r\n\r\nclass Sigmoid(ActivationFunction):\r\n def __call__(self, x: float) -> float:\r\n return 1 / (1 + math.exp(-x))\r\n\r\nclass Relu(ActivationFunction):\r\n def __call__(self, x: float) -> float:\r\n return max(0, x)\r\n\r\nsigmoid = Sigmoid()\r\nrelu = Relu()\r\n\r\nprint(sigmoid(0.5))\r\nprint(relu(0.5))\r\n```\r\n\r\nIf the function is a single expression, it can be done very nicely with lambdas:\r\n\r\n```python\r\nfrom typing import Callable\r\nimport math\r\n\r\nActivationFunction = Callable[[float], float]\r\n\r\nsigmoid: ActivationFunction = lambda x: 1 / (1 + math.exp(-x))\r\nrelu: ActivationFunction = lambda x: max(0, x)\r\n\r\nprint(sigmoid(0.5))\r\nprint(relu(0.5))\r\n```\r\n\r\nHere's the equivalent FSharp code, with the only difference being that FSharp supports multiline lambdas:\r\n\r\n```fsharp\r\ntype ActivationFunction = float -> float\r\n\r\nlet sigmoid: ActivationFunction = fun x -> 1.0 / (1.0 + exp(-x))\r\nlet relu: ActivationFunction = fun x -> max 0.0 x\r\n\r\nprintfn \"%f\" (sigmoid 0.5)\r\nprintfn \"%f\" (relu 0.5)\r\n```\r\n\r\nI'm asking about this feature because I'm reading a book called Domain Modeling Made Functional by Scott Wlaschin. In this book, the author recommends modeling the domain with types, including function type aliases, and focusing on the implementation details later. Here's an example from Chapter 9:\r\n\r\n\r\n\r\nI'd like to try a similar approach in Python. Has there been any further discussion about this topic, or is there a workaround I'm unaware of? Thank you!\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/OlegAlexander","@type":"Person","name":"OlegAlexander"},"datePublished":"2023-06-26T01:06:28Z","url":"https://github.com/python/typing/issues/1419"}
| 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:4c1f3400-dc49-cf9c-ee4f-0ee2f4291518 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBF6:BACA4:2EF4D06:3E33CD2:6B002BEC |
| html-safe-nonce | dbc16bda6b49b0e985eb4994b646a71c194690f581572307d824392d0dae7f49 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkY2OkJBQ0E0OjJFRjREMDY6M0UzM0NEMjo2QjAwMkJFQyIsInZpc2l0b3JfaWQiOiI1NjQ4NTg2NzAxMzg0MTk5MTQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f26cebebb2b5444fa29370f7044df1d37baf9e3ca7009c7d370630456986cc52 |
| hovercard-subject-tag | issue:1773618400 |
| 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/1419/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fe40f49b5ec007bd7ee2eecbd9e98af78fcb647d8e51cbcafaa02f36755e9a35/python/typing/issues/1419 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fe40f49b5ec007bd7ee2eecbd9e98af78fcb647d8e51cbcafaa02f36755e9a35/python/typing/issues/1419 |
| og:image:alt | Hello. I would like to revive the discussion about Callable type aliases. The original discussion is here, and the related discussion about @declared_type is here and here. The original thread cont... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | OlegAlexander |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width