René's URL Explorer Experiment


Title: Overhaul the python typing system to make typing a 'first class citizen' with a dedicated typing syntax, like match patterns · Issue #953 · python/typing · GitHub

Open Graph Title: Overhaul the python typing system to make typing a 'first class citizen' with a dedicated typing syntax, like match patterns · Issue #953 · python/typing

X Title: Overhaul the python typing system to make typing a 'first class citizen' with a dedicated typing syntax, like match patterns · Issue #953 · python/typing

Description: Pythons implementation of typing at the language level has been very limited, with only the absolute minimal changes to the grammar being implemented: type annotations: a: int return type annotations: def () -> int: Both of these syntaxe...

Open Graph Description: Pythons implementation of typing at the language level has been very limited, with only the absolute minimal changes to the grammar being implemented: type annotations: a: int return type annotatio...

X Description: Pythons implementation of typing at the language level has been very limited, with only the absolute minimal changes to the grammar being implemented: type annotations: a: int return type annotatio...

Opengraph URL: https://github.com/python/typing/issues/953

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Overhaul the python typing system to make typing a 'first class citizen' with a dedicated typing syntax, like match patterns","articleBody":"Pythons implementation of typing at the language level has been very limited, with only the absolute minimal changes to the grammar being implemented:\r\n- type annotations: `a: int`\r\n- return type annotations: `def () -> int:`\r\n\r\nBoth of these syntaxes are just normal expressions that are evaluated (or are just strings if `__future__.annotations` is on).\r\n\r\nThat has meant that the entirety of the typing landscape has been implemented 'in the language' using existing syntax and functionality to try and emulate typing as a first class language feature.\r\n\r\nThis has lead to modifications to the standard library to attempt to better support these usages (eg: [special casing `type[...]`](https://github.com/gvanrossum/cpython/blob/4575c01b750cd26377e803247c38d65dad15e26a/Objects/abstract.c#L182), defining `__or__` on `type` and the introduction of `__class_getitem__` [among](https://www.innersloth.com/games/among-us/) others). So all these objects now have a dual nature, they have functions to support their 'normal' usages, and also a bunch of functions to support building type annotations.\r\n\r\nThe authors of PEP-586 [have commented](https://www.python.org/dev/peps/pep-0586/#adding-more-concise-syntax) on this problem(thanks @sobolevn):\r\n> we feel overhauling the syntax of types in Python is not within the scope of this PEP: it would be best to have that discussion in a separate PEP\r\n\r\nDue to the nature of typing being 'implemented in the language' instead of in the grammar/interpreter has lead to a myriad of complexities, edge cases, limitations and confusion. I will list a few of these side effect here. also showing a more optimal syntax that would be possible if typing were to be overhauled:\r\n\r\n### Fake types and type alias\r\nFake types such as `None` and `ForwardRef`s lead to runtime errors(either in annotations or upon calling `get_type_hints`):\r\n```py\r\nfrom typing import TypeAlias\r\n\r\nStr: TypeAlias = \"str\"\r\nInt: TypeAlias = \"int\"\r\n\r\na: Str | Int = 0  # SUS ALERT\r\n```\r\nThere could be a dedicated `typealias` keyword like other languages\r\n```py\r\ntypealias foo = int | str\r\n```\r\n(https://github.com/python/mypy/issues/11582)\r\n#### Shout out to NewType\r\n```py\r\nMyStr = NewType(\"MyStr\", str)\r\n```\r\nCould be\r\n```py\r\nnewtype MyStr = str\r\n```\r\n\r\n### Typing imports\r\nA large amount of types need to be imported from `typing`, leading to a boilerplate of imports in every module:\r\n```py\r\nfrom typing import NoReturn, TypedDict, TypeAlias, TypeVar, Generic, Protocol, etc, etc, etc, etc\r\n```\r\nThere could be a 'builtins' for type annotations that are usable by default.\r\n\r\nAlso there is no way to import a type statically, see #928\r\n\r\n### Type parameters\r\nAre one of the worst incantations imaginable:\r\n```py\r\nfrom typing import TypeVar, Generic\r\n\r\nT_cont = TypeVar(\"T_cont\", contravariant=True, bound=str)\r\n\r\nclass A(Generic[T_cont]):\r\n    ...\r\n```\r\nCould be\r\n```py\r\nclass A[in T_cont: str]:\r\n    ...\r\n```\r\n\r\nAlso you can't specify type parameters explicitly on function call sites:\r\n```py\r\nfoo_result: int | str = foo(1, \"a\")\r\nbar(foo_result)\r\n```\r\nCould be:\r\n```py\r\nbar(foo[int | str](1, \"a\"))\r\n```\r\n\r\n### Literal syntax\r\n```py\r\na: Literal[1, 2] = 1\r\n```\r\nCould be\r\n```py\r\na: 1 | 2 = 1\r\n```\r\n\r\n### cast\r\nYet another import, and very clunky to boot.\r\nAnd why is the type first? everywhere else(`isinstance`, `issubclass`, type annotations) types go second.\r\n```py\r\ncast(int, a)\r\n```\r\nCould be `as` or similar like other languages:\r\n```py\r\na as int\r\n```\r\n\r\n### Callable\r\nIn error messages mypy shows this as `(int, str) -> bool`\r\n```py\r\na: Callable[[int, str], bool]\r\n```\r\nCould be\r\n```py\r\na: (int, str) -> bool\r\n```\r\n\r\n### Confusion\r\nBecause all these type machineries exist at runtime, it creates very confusing situations where an annotation is used in a value position:\r\n```py\r\nA = int | str\r\nA()  # is this valid?\r\nfoo(A)  # is this valid?\r\nB: type[int | str] = int | str  # is this valid?\r\n```\r\n\r\nhttps://github.com/microsoft/pyright/issues/2522 shows that it's very easy to overlook this situation.\r\n\r\n\r\n# Match Patterns\r\n[Enter `match/case` statements](https://www.python.org/dev/peps/pep-0622/), which have their own unique language level syntax for dealing with all the concepts needed in a comprehensive way. For example matching a union of strings: `\"a\" | \"b\"` which in a typing annotation would just be evaluated as a normal expression and raise a `TypeError`.\r\n```py\r\nmatch \"a\":\r\n    case \"a\" | \"b\":  # 😳😳😳😳😳😳😳😳😳😳😳😳😳😳😳😳\r\n          print(\"AMONGUS\")\r\n```\r\n\r\nInstead of endlessly implementing special casing and workarounds, please overhaul the entire implementation of typing in the language from the ground up. Currently typing in python is very rough around the edges and could be vastly improved based upon the experience and usages gathered over the years since it's inception.\r\n\r\n# Proposal\r\nAdd the concept of a 'type context' that has it's own unique semantic meaning, separating the type realm functionality from the value(value context) realm functionality. This would make things such as the type union `|` much simpler and consistent (see issue where some types are not actually types at all, and don't have `__or__`). These operations would still produce type machinery representations:\r\n\r\n```py\r\nclass A:\r\n    a: int | str\r\nA.__annotations__  # {'a': UnionType[int, str]}\r\n```\r\n\r\nAnd these machinereies could be constructed manually in a 'value context' when needed:\r\n```py\r\na = UnionType(int, str)\r\n```\r\n\r\n### Example\r\n```py\r\nBar = 1 | 2\r\n    # ^ value context\r\ntypealias Foo = 1 | 2\r\n              # ^ type context\r\n```\r\n\r\n### Evaluating type syntax\r\n```py\r\nfrom __future__ import annotations\r\nfrom typing import get_type_hints\r\n\r\nclass A:\r\n    x: \"a\" | \"b\"\r\n\r\nget_type_hints(A)  # Currently will raise a TypeError\r\n```\r\nUnder the hood, `get_type_hints` simply `eval`s the annotation as a module level expression, I suggest adding a `eval_type` function that will evaluate it as a `type context` expression, not a `value context`\r\n\r\n\r\n","author":{"url":"https://github.com/KotlinIsland","@type":"Person","name":"KotlinIsland"},"datePublished":"2021-11-20T06:45:40Z","url":"https://github.com/python/typing/issues/953"}

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:15d6b3e8-37cb-bea8-2dda-0de127475ba3
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB9A6:3F9D33:21EA0AE:2E0C72A:6B0024FE
html-safe-noncef68fde7551502e5eae901f1046e7da409cdce377e59b642d2da76f75b10caa65
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOUE2OjNGOUQzMzoyMUVBMEFFOjJFMEM3MkE6NkIwMDI0RkUiLCJ2aXNpdG9yX2lkIjoiMjMyOTMxMDkzOTk0NzQ3NjIyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac93a391eddb97d447f14bb94d694b18029cc0e97f0d1a6b22170b29de7864cffc
hovercard-subject-tagissue:1059077728
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/953/issue_layout
twitter:imagehttps://opengraph.githubassets.com/cb1af7aedca402680ab9a6849a86bbf2d4f05bc6c71cb05d69ae2fbcb8ccf72f/python/typing/issues/953
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/cb1af7aedca402680ab9a6849a86bbf2d4f05bc6c71cb05d69ae2fbcb8ccf72f/python/typing/issues/953
og:image:altPythons implementation of typing at the language level has been very limited, with only the absolute minimal changes to the grammar being implemented: type annotations: a: int return type annotatio...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameKotlinIsland
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
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
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typing/issues/953#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F953
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%2F953
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/953
Reloadhttps://github.com/python/typing/issues/953
Reloadhttps://github.com/python/typing/issues/953
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/953
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/953
Overhaul the python typing system to make typing a 'first class citizen' with a dedicated typing syntax, like match patternshttps://github.com/python/typing/issues/953#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/KotlinIsland
KotlinIslandhttps://github.com/KotlinIsland
on Nov 20, 2021https://github.com/python/typing/issues/953#issue-1059077728
special casing type[...]https://github.com/gvanrossum/cpython/blob/4575c01b750cd26377e803247c38d65dad15e26a/Objects/abstract.c#L182
amonghttps://www.innersloth.com/games/among-us/
PEP-586https://peps.python.org/586
have commentedhttps://www.python.org/dev/peps/pep-0586/#adding-more-concise-syntax
@sobolevnhttps://github.com/sobolevn
python/mypy#11582https://github.com/python/mypy/issues/11582
#928https://github.com/python/typing/issues/928
microsoft/pyright#2522https://github.com/microsoft/pyright/issues/2522
Enter match/case statementshttps://www.python.org/dev/peps/pep-0622/
KotlinIslandhttps://github.com/KotlinIsland
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 Nov 20, 2021https://github.com/python/typing/issues/953#event-5648627027
https://github.com/sobolevn
sobolevnhttps://github.com/sobolevn
on Nov 20, 2021https://github.com/python/typing/issues/953#issuecomment-974613489
https://www.python.org/dev/peps/pep-0586/#adding-more-concise-syntaxhttps://www.python.org/dev/peps/pep-0586/#adding-more-concise-syntax
https://www.python.org/dev/peps/pep-0001/https://www.python.org/dev/peps/pep-0001/
KotlinIslandhttps://github.com/KotlinIsland
on Nov 20, 2021https://github.com/python/typing/issues/953#event-5648711872
https://github.com/KotlinIsland
KotlinIslandhttps://github.com/KotlinIsland
on Nov 20, 2021https://github.com/python/typing/issues/953#issuecomment-974617037
KotlinIslandhttps://github.com/KotlinIsland
on Nov 20, 2021https://github.com/python/typing/issues/953#event-5648728227
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Nov 23, 2021https://github.com/python/typing/issues/953#issuecomment-976393632
@sobolevnhttps://github.com/sobolevn
https://github.com/KotlinIsland
KotlinIslandhttps://github.com/KotlinIsland
on Nov 23, 2021https://github.com/python/typing/issues/953#issuecomment-976402992
https://github.com/danmou
danmouhttps://github.com/danmou
on Nov 25, 2021https://github.com/python/typing/issues/953#issuecomment-979441288
@KotlinIslandhttps://github.com/KotlinIsland
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Nov 25, 2021https://github.com/python/typing/issues/953#issuecomment-979483363
https://github.com/KotlinIsland
KotlinIslandhttps://github.com/KotlinIsland
on Nov 26, 2021https://github.com/python/typing/issues/953#issuecomment-979582044
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typing/issues/953
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typing/issues/953
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.