René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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![Domain_Modeling_Made_Functional](https://github.com/python/typing/assets/42989765/b70c43a2-fe00-4bf5-b0c3-a28dc1c92746)\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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4c1f3400-dc49-cf9c-ee4f-0ee2f4291518
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idEBF6:BACA4:2EF4D06:3E33CD2:6B002BEC
html-safe-noncedbc16bda6b49b0e985eb4994b646a71c194690f581572307d824392d0dae7f49
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkY2OkJBQ0E0OjJFRjREMDY6M0UzM0NEMjo2QjAwMkJFQyIsInZpc2l0b3JfaWQiOiI1NjQ4NTg2NzAxMzg0MTk5MTQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacf26cebebb2b5444fa29370f7044df1d37baf9e3ca7009c7d370630456986cc52
hovercard-subject-tagissue:1773618400
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typing/1419/issue_layout
twitter:imagehttps://opengraph.githubassets.com/fe40f49b5ec007bd7ee2eecbd9e98af78fcb647d8e51cbcafaa02f36755e9a35/python/typing/issues/1419
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/fe40f49b5ec007bd7ee2eecbd9e98af78fcb647d8e51cbcafaa02f36755e9a35/python/typing/issues/1419
og:image:altHello. 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameOlegAlexander
hostnamegithub.com
expected-hostnamegithub.com
None7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0
turbo-cache-controlno-preview
go-importgithub.com/python/typing git https://github.com/python/typing.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id24604008
octolytics-dimension-repository_nwopython/typing
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24604008
octolytics-dimension-repository_network_root_nwopython/typing
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef3f3a43ca095cdea3abbb394112e971f9fdb94fa
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typing/issues/1419#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F1419
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F1419
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Ftyping
Reloadhttps://github.com/python/typing/issues/1419
Reloadhttps://github.com/python/typing/issues/1419
Reloadhttps://github.com/python/typing/issues/1419
pythonhttps://github.com/python
typinghttps://github.com/python/typing
Codehttps://github.com/python/typing
Issues206 (206)https://github.com/python/typing/issues
Pull requests27 (27)https://github.com/python/typing/pulls
Discussionshttps://github.com/python/typing/discussions
Actionshttps://github.com/python/typing/actions
Security and qualityhttps://github.com/python/typing/security
Insightshttps://github.com/python/typing/insights
https://github.com/python/typing
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/1419
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/1419
Ability to use Callable type alias when annotating functionshttps://github.com/python/typing/issues/1419#top
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
https://github.com/OlegAlexander
OlegAlexanderhttps://github.com/OlegAlexander
on Jun 26, 2023https://github.com/python/typing/issues/1419#issue-1773618400
herehttps://github.com/python/mypy/issues/1641
herehttps://github.com/python/mypy/issues/2087
herehttps://github.com/python/mypy/pull/3291
https://private-user-images.githubusercontent.com/42989765/248602021-b70c43a2-fe00-4bf5-b0c3-a28dc1c92746.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3OTUxNzM2NTcsIm5iZiI6MTc5NTE3MzM1NywicGF0aCI6Ii80Mjk4OTc2NS8yNDg2MDIwMjEtYjcwYzQzYTItZmUwMC00YmY1LWIwYzMtYTI4ZGMxYzkyNzQ2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjExMjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYxMTIwVDExMTU1N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAwOWZiYjk4MWQ2N2QyODk3MWY5NzBlZWI2NDk2ODljMDYwN2U2NDIwZWI5MzQ0ZTcyZWU1YTc2YmZjMzgyMDMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnJlc3BvbnNlLWNvbnRlbnQtdHlwZT1pbWFnZSUyRnBuZyJ9.nSDlOvnGpk28rehCmnNpwEQWPAXbkK2TyWE188lzV1Q
OlegAlexanderhttps://github.com/OlegAlexander
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
on Jun 26, 2023https://github.com/python/typing/issues/1419#event-9631673478
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Jun 26, 2023https://github.com/python/typing/issues/1419#issuecomment-1606706969
https://github.com/OlegAlexander
OlegAlexanderhttps://github.com/OlegAlexander
on Jun 27, 2023https://github.com/python/typing/issues/1419#issuecomment-1608581063
https://github.com/OlegAlexander
OlegAlexanderhttps://github.com/OlegAlexander
on Sep 24, 2023https://github.com/python/typing/issues/1419#issuecomment-1732449288
@ilevkivskyihttps://github.com/ilevkivskyi
python/mypy#1641 (comment)https://github.com/python/mypy/issues/1641#issuecomment-325786575
nate-keanhttps://github.com/nate-kean
on Dec 18, 2024https://github.com/python/typing/issues/1419#event-15698505815
*sigh* type annotate these functionshttps://github.com/nate-kean/parrot/commit/13a32de4d8779cf28a19ab230c4c38c71e90da68
13a32dehttps://github.com/nate-kean/parrot/commit/13a32de4d8779cf28a19ab230c4c38c71e90da68
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typing/issues/1419
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typing/issues/1419
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.