René's URL Explorer Experiment


Title: Issue · GitHub

Open Graph Title: Issue · python/mypy

X Title: Issue · python/mypy

Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.

Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.

X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.

Opengraph URL: https://github.com/python/mypy

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support function decorators excellently","articleBody":"Decorators currently stress mypy's support for functional programming to the point that many decorators are impossible to type.  I'm intending this issue as a project plan and exploration of the issue of decorators and how to type them. It's a collection point for problems or incompletenesses that keep decorators from being fully supported, plans for solutions to those problems, and fully-implemented solutions to those problems.\r\n\r\n# Decorators that can only decorate a function of a fixed signature\r\n\r\nYou can define your decorator's signature explicitly, and it's completely supported:\r\n\r\n```python\r\nfrom typing import Callable, Tuple\r\n\r\ndef with_strlen(f: Callable[[int], str]) -\u003e Callable[[int], Tuple[str, int]]:\r\n    def ret(__i: int) -\u003e Tuple[str, int]:\r\n        r = f(__i)\r\n        return r, len(r)\r\n    return ret\r\n\r\n@with_strlen\r\ndef lol(x: int) -\u003e str:\r\n    return \"lol\"*x\r\n\r\nreveal_type(lol)  # E: Revealed type is 'def (builtins.int) -\u003e Tuple[builtins.str, builtins.int]\r\n ```\r\n Notes:\r\n* You can even use type variables to get some flexibility in argument and return types, but this falls over as soon as you don't know the exact number of arguments to expect.\r\n* Your decorated function's arguments can't be called by name.\r\n  - [ ] https://github.com/python/mypy/pull/2607 enables that\r\n\r\n# Decorators that do not change the signature of the function\r\n\r\nNearly fully supported. Here's how you do it:\r\n\r\n```python\r\nfrom typing import TypeVar, Callable, cast\r\n\r\nT = TypeVar('T')\r\n\r\ndef print_callcount(f: T) -\u003e T:\r\n    x = 0\r\n    def ret(*args, **kwargs):\r\n        nonlocal x\r\n        x += 1\r\n        print(\"%d calls so far\" % x)\r\n        return f(*args, **kwargs)\r\n\r\n    return cast(T, ret)\r\n\r\n@print_callcount\r\ndef lol(x: int) -\u003e str:\r\n    return \"lol\"*x\r\n\r\nreveal_type(lol)  # E: Revealed type is 'def (x: builtins.int) -\u003e builtins.str'\r\n```\r\n\r\nNotes: \r\n* Mypy trusts that you can call `f`.  You can set a bound on `T` to be `Callable`, but you don't need to for it to typecheck.\r\n* This business doesn't typecheck without the cast. That's a symptom of the fact that mypy doesn't understand the function nature of the argument to `print_callcount`, and there's no way to declare that argument as a `Callable` explicitly without losing the argument type of `lol` later. We'd like to minimize the places where casts are required. Doing so requires something along the lines of \"variadic argument variables\", discussed below.\r\n\r\n# Decorators that take arguments (\"second-order\"?)\r\n\r\nPlenty of decorators \"take arguments\" by *actually* being functions that return a decorator. For example, we'd like to be able to do this:\r\n\r\n```python\r\nfrom typing import Any, TypeVar, Callable, cast\r\n\r\nT = TypeVar('T')\r\n\r\ndef callback_callcount(cb: Callable[[int], None]) -\u003e Callable[[T], T]:\r\n    def outer(f: T) -\u003e T:\r\n        x = 0\r\n        def inner(*args, **kwargs):\r\n            nonlocal x\r\n            x += 1\r\n            cb(x)\r\n            return f(*args, **kwargs)\r\n        return cast(T, inner)\r\n    return outer\r\n\r\ndef print_int(x: int) -\u003e None:\r\n    print(x)\r\n\r\n@callback_callcount(print_int)\r\ndef lol(x: int) -\u003e str:\r\n    return \"lol\"*x\r\n\r\nreveal_type(lol)  # E: Revealed type is 'def (x: builtins.int) -\u003e builtins.str'\r\n```\r\nNotes:\r\n* This does not typecheck yet -- errors on calling the decorator, and `lol` ends up typed as `None`\r\n* Relevant issue: https://github.com/python/mypy/issues/1551\r\n  - [ ] https://github.com/python/mypy/issues/3028 fixes this.\r\n* Still has a non-ideal cast...\r\n\r\n# Mess with the return type or with arguments\r\n\r\nFor an arbitrary function you can't do this at all yet -- there isn't even a syntax.  Here's me making up some syntax for it.\r\n\r\n## Messing with the return type\r\n```python\r\nfrom typing import Any, Dict, Callable\r\n\r\nfrom mypy_extensions import SomeArguments\r\n\r\ndef reprify(f: Callable[[SomeArguments], Any]) -\u003e Callable[[SomeArguments], str]:\r\n    def ret(*args: SomeArguments.positional, **kwargs: SomeArguments.keyword):\r\n        return repr(f(*args, **kwargs))\r\n    return ret\r\n\r\n@reprify\r\ndef lol(x: int) -\u003e Dict[str, int]:\r\n    return {\"lol\": x}\r\n\r\nreveal_type(lol)  # E: Revealed type is 'def (x: builtins.int) -\u003e builtins.str'\r\n```\r\n\r\n## Messing with the arguments\r\n\r\n```python\r\nfrom typing import Any, Callable, TypeVar\r\n\r\nfrom mypy_extensions import SomeArguments\r\n\r\nR = TypeVar('R')\r\n\r\ndef supply_zero(f: Callable[[int, SomeArguments], R]) -\u003e Callable[[SomeArguments], R]:\r\n    def ret(*args: SomeArguments.positional, **kwargs: SomeArguments.keyword):\r\n        return f(0, *args, **kwargs)\r\n    return ret\r\n\r\n@supply_zero\r\ndef lol(x: int, y: str) -\u003e str:\r\n    return \"%d and %s\" % (x, y)\r\n\r\nreveal_type(lol)  # E: Revealed type is 'def (y: builtins.str) -\u003e builtins.str'\r\n```\r\n\r\nThe syntax here is fungible, but we would need a way to do approximately this thing -- capture the types and kinds of all a function's arguments in some kind of variation on a type variable. \r\n\r\nRelevant issues and discussions:\r\n* https://github.com/python/mypy/issues/1927\r\n* https://github.com/python/mypy/issues/3028\r\n* https://github.com/python/typing/issues/239\r\n* https://github.com/python/typing/issues/193\r\n\r\nVariadic type variables alone (https://github.com/python/typing/issues/193) get you some of the way there, but lose all keyword arguments of the decorated function. \r\n\r\nThings to do:\r\n* [ ] Implement variadic type variables (fill in PR when I have it)\r\n* [ ] Write up detailed proposal for semantics of argument variables\r\n  - [ ] ... and how they interact with `*args` and `**kwargs`\r\n  - [ ] ... and their relationship to variadic type variables and the *expand* operation\r\n  - [ ] ... and the semantics of an easy-to-use `SomeArguments`-style alias, so nobody has to actually engage with the details of the above when writing normal decorators.\r\n* [ ] Come to some kind of mypy-community consensus or near-consensus on that proposal. It'll be in `mypy_extensions` not `typing` at first -- this can be fodder for the future of PEP484, but while we're playing in such experimental land, not yet.\r\n* [ ] PR to implment the `SomeArguments` thing.\r\n","author":{"url":"https://github.com/sixolet","@type":"Person","name":"sixolet"},"datePublished":"2017-04-12T00:52:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":22},"url":"https://github.com/3157/mypy/issues/3157"}

route-pattern/:user_id/:repository/issues/:id(.:format)
route-controllerissues
route-actionshow
fetch-noncev2:5c42b3cd-6ea1-89d5-5343-5797484810ea
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA07C:281B19:3088D7:45191F:6A4C9D95
html-safe-nonce4234c1d59a4fb3f437567b40e37a91eeb337ea08327eaf44a3f73a8815612a80
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDdDOjI4MUIxOTozMDg4RDc6NDUxOTFGOjZBNEM5RDk1IiwidmlzaXRvcl9pZCI6IjM5OTQ3NTUxNjExNzE3Mjk4MTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac54196c92d96c4801bdd28da7a02cbc5d9ee600b36e600376b10a5dc3224df67f
hovercard-subject-tagrepository:7053637
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///issues/show
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/mypy/issues/3157
twitter:imagehttps://opengraph.githubassets.com/b873c613c4257466b8aaf0d129f0885412c8fec8c6f9280a555e768fe0dc302d/python/mypy
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/b873c613c4257466b8aaf0d129f0885412c8fec8c6f9280a555e768fe0dc302d/python/mypy
og:image:altOptional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c
turbo-cache-controlno-cache
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
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
releaseae90d426644ca15e89bacceb72e51f4e9dbf85f7
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/mypy/issues/3157#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F3157
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F3157
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fissues%2Fshow&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/3157
Reloadhttps://github.com/python/mypy/issues/3157
Reloadhttps://github.com/python/mypy/issues/3157
Please reload this pagehttps://github.com/python/mypy/issues/3157
python https://github.com/python
mypyhttps://github.com/python/mypy
Please reload this pagehttps://github.com/python/mypy/issues/3157
Notifications https://github.com/login?return_to=%2Fpython%2Fmypy
Fork 3.2k https://github.com/login?return_to=%2Fpython%2Fmypy
Star 20.5k https://github.com/login?return_to=%2Fpython%2Fmypy
Code https://github.com/python/mypy
Issues 2.7k https://github.com/python/mypy/issues
Pull requests 460 https://github.com/python/mypy/pulls
Actions https://github.com/python/mypy/actions
Projects https://github.com/python/mypy/projects
Wiki https://github.com/python/mypy/wiki
Security and quality 0 https://github.com/python/mypy/security
Insights https://github.com/python/mypy/pulse
Code https://github.com/python/mypy
Issues https://github.com/python/mypy/issues
Pull requests https://github.com/python/mypy/pulls
Actions https://github.com/python/mypy/actions
Projects https://github.com/python/mypy/projects
Wiki https://github.com/python/mypy/wiki
Security and quality https://github.com/python/mypy/security
Insights https://github.com/python/mypy/pulse
Support function decorators excellentlyhttps://github.com/python/mypy/issues/3157#top
needs discussionhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22needs%20discussion%22
priority-1-normalhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-1-normal%22
https://github.com/sixolet
sixolethttps://github.com/sixolet
on Apr 12, 2017https://github.com/python/mypy/issues/3157#issue-221120895
Better callable: Callable[[Arg('x', int), VarArg(str)], int] now a thing you can do #2607https://github.com/python/mypy/pull/2607
Function returning a generic function #1551https://github.com/python/mypy/issues/1551
TypeVar to represent a Callable's arguments #3028https://github.com/python/mypy/issues/3028
Making a decorator which preserves function signature #1927https://github.com/python/mypy/issues/1927
TypeVar to represent a Callable's arguments #3028https://github.com/python/mypy/issues/3028
Make Callable more flexible typing#239https://github.com/python/typing/issues/239
Allow variadic generics typing#193https://github.com/python/typing/issues/193
python/typing#193https://github.com/python/typing/issues/193
needs discussionhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22needs%20discussion%22
priority-1-normalhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-1-normal%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.