René's URL Explorer Experiment


Title: A Comparable type? · Issue #59 · python/typing · GitHub

Open Graph Title: A Comparable type? · Issue #59 · python/typing

X Title: A Comparable type? · Issue #59 · python/typing

Description: I (Guido) wondered how I would compare two arguments whose type is a type variable: def cmp(a: T, b: T) -> int: if a < b: return -1 if a > b: return 1 return 0 Jukka wrote: """ This is a long-standing issue, and we've discussed this befo...

Open Graph Description: I (Guido) wondered how I would compare two arguments whose type is a type variable: def cmp(a: T, b: T) -> int: if a < b: return -1 if a > b: return 1 return 0 Jukka wrote: """ This is a long-stand...

X Description: I (Guido) wondered how I would compare two arguments whose type is a type variable: def cmp(a: T, b: T) -> int: if a < b: return -1 if a > b: return 1 return 0 Jukka wrote: ""&qu...

Mail addresses
notifications@github.com

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A Comparable type?","articleBody":"I (Guido) wondered how I would compare two arguments whose type is a type variable:\n\n```\ndef cmp(a: T, b: T) -> int:\n    if a \u003c b: return -1\n    if a > b: return 1\n    return 0\n```\n\nJukka wrote:\n\"\"\"\nThis is a long-standing issue, and we've discussed this before in some detail (e.g., https://github.com/JukkaL/typing/issues/9). However, it wasn't discussed in typehinting, as far as I can tell. Currently we need to use a cast to use \u003c instead of ==, which is pretty ugly.\n\nWe could define a Comparable ABC and support that as an \"upper bound\" for a type variable (bounded polymorphism). About the simplest useful implementation would be like this:\n\n```\nclass Comparable(metaclass=ABCMeta):\n    @abstractmethod\n    def __gt__(self, other: Any) -> bool: pass\n    ... # __lt__ etc. as well\n\n...\n\nCT = TypeVar('CT', bound=Comparable) # This is different from TypeVar('CT', Comparable)!\n\ndef min(x: CT, y: CT) -> CT:\n    if x \u003c y:\n        return x\n    else:\n        return y\n\nf(1, 2) # ok, return type int\nf('x', 'y') # ok, return type str\nf('x', 1) # probably ok, return type Comparable, which is not optimal\nf(int, int)  # error, types aren't comparable\n```\n\nHowever, this doesn't verify whether the types can be compared to each other, only that they can be compared to something (because of the Any argument type). This feature would be easy to add to mypy.\n\nThe way Java etc. do this is to support a fancier language feature called \"F-bounded polymorphism\" or \"F-bounded quantification\". With it we can say that int can be compared with only int (Comparable[int]), etc. For example:\n\n```\nclass Comparable(Generic[T]):\n    @abstractmethod\n    def __gt__(self, other: T) -> bool: pass\n    ... # __lt__ etc. as well\n\n...\n\n\nCT = TypeVar('CT', bound=Comparable['CT'])   # Any type that is comparable with itself\n\ndef min(x: CT, y: CT) -> CT:\n    if x \u003c y:\n        return x\n    else:\n        return y\n\nf(1, 2) # ok, return type int\nf('x', 'y') # ok, return type str\nf('x', 1) # error, since these are not comparable to each other\nf(int, int)  # error, types aren't comparable at all\n```\n\nThis would be more involved to add to mypy. It would probably be one or two days of work for a minimal implementation. I'm not even quite sure that the above would be sufficiently general (Comparable should be contravariant, I think, so that Comparable[object] is a subtype of Comparable[int] :-/ ).\n\nComparable is probably the only common use case for this _complex_ feature.\n\"\"\"\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2015-03-19T20:48:59Z","url":"https://github.com/python/typing/issues/59"}

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:578be699-2e41-ecbd-8e47-3f111f014eb2
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE328:1A77D:33F394:45D604:6B0027AD
html-safe-nonce38bae8657c3183a6458f6beb51dc0e4498c6f555f6a70aee4b7248cefcb75ee1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzI4OjFBNzdEOjMzRjM5NDo0NUQ2MDQ6NkIwMDI3QUQiLCJ2aXNpdG9yX2lkIjoiNzExMDc4MzMyOTE4NzY2OTkzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmaccc73c341e9e69c30b0f309a46bf71a9e594ff81578a980d57958a465e3b0cb88
hovercard-subject-tagissue:63088239
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/59/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3628852b1b691ebd829f2878ee4ec3b7df171e6079f1992f5285d2da4044010c/python/typing/issues/59
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3628852b1b691ebd829f2878ee4ec3b7df171e6079f1992f5285d2da4044010c/python/typing/issues/59
og:image:altI (Guido) wondered how I would compare two arguments whose type is a type variable: def cmp(a: T, b: T) -> int: if a < b: return -1 if a > b: return 1 return 0 Jukka wrote: """ This is a long-stand...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegvanrossum
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/59#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F59
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%2F59
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/59
Reloadhttps://github.com/python/typing/issues/59
Reloadhttps://github.com/python/typing/issues/59
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/59
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/59
A Comparable type?https://github.com/python/typing/issues/59#top
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Mar 19, 2015https://github.com/python/typing/issues/59#issue-63088239
https://github.com/JukkaL/typing/issues/9https://github.com/JukkaL/typing/issues/9
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Mar 23, 2015https://github.com/python/typing/issues/59#issuecomment-85224015
vlasovskikhhttps://github.com/vlasovskikh
on Apr 13, 2015https://github.com/python/typing/issues/59#event-280154034
https://github.com/vlasovskikh
vlasovskikhhttps://github.com/vlasovskikh
on Apr 14, 2015https://github.com/python/typing/issues/59#issuecomment-92558818
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Apr 14, 2015https://github.com/python/typing/issues/59#issuecomment-92887722
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Apr 14, 2015https://github.com/python/typing/issues/59#issuecomment-92890454
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on May 4, 2015https://github.com/python/typing/issues/59#issuecomment-98565143
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on May 4, 2015https://github.com/python/typing/issues/59#issuecomment-98580133
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on May 5, 2015https://github.com/python/typing/issues/59#issuecomment-99179922
@JukkaLhttps://github.com/JukkaL
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on May 7, 2015https://github.com/python/typing/issues/59#issuecomment-99705978
http://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/TypeVariable.htmlhttp://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/TypeVariable.html
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on May 7, 2015https://github.com/python/typing/issues/59#issuecomment-99707075
http://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/TypeVariable.htmlhttp://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/TypeVariable.html
#59 (comment)https://github.com/python/typing/issues/59#issuecomment-99705978
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on May 7, 2015https://github.com/python/typing/issues/59#issuecomment-99713777
gvanrossumhttps://github.com/gvanrossum
bughttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22bug%22
on May 18, 2015https://github.com/python/typing/issues/59#event-307774372
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on May 18, 2015https://github.com/python/typing/issues/59#issuecomment-103198681
gvanrossumhttps://github.com/gvanrossum
on May 18, 2015https://github.com/python/typing/issues/59#event-307881499
Add type variables with upper bound. (Fixes issue #59.)https://github.com/python/typing/commit/bc6276d16cf20e9ebccb8c7b127f7e5cd7192cad
#59https://github.com/python/typing/issues/59
bc6276dhttps://github.com/python/typing/commit/bc6276d16cf20e9ebccb8c7b127f7e5cd7192cad
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on May 18, 2015https://github.com/python/typing/issues/59#issuecomment-103239588
gvanrossumhttps://github.com/gvanrossum
completedhttps://github.com/python/typing/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on May 18, 2015https://github.com/python/typing/issues/59#event-307882227
https://github.com/mitar
mitarhttps://github.com/mitar
on Dec 16, 2017https://github.com/python/typing/issues/59#issuecomment-352183762
https://github.com/Dentosal
Dentosalhttps://github.com/Dentosal
on Dec 25, 2017https://github.com/python/typing/issues/59#issuecomment-353878355
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Dec 28, 2017https://github.com/python/typing/issues/59#issuecomment-354263666
@Dentosalhttps://github.com/Dentosal
https://github.com/MartinThoma
MartinThomahttps://github.com/MartinThoma
on Jun 25, 2020https://github.com/python/typing/issues/59#issuecomment-649230580
@Dentosalhttps://github.com/Dentosal
trishankatdatadoghttps://github.com/trishankatdatadog
on Nov 30, 2020https://github.com/python/typing/issues/59#event-4050585931
A decent implementation of Comparable.https://github.com/trishankatdatadog/tuf-on-a-plane/commit/f8d3437f15725b8537044a6331a26527e6e03312
f8d3437https://github.com/trishankatdatadog/tuf-on-a-plane/commit/f8d3437f15725b8537044a6331a26527e6e03312
Update timestamp (#12)https://github.com/trishankatdatadog/tuf-on-a-plane/commit/83a94a9c1a167fac77c1636e3a0c2c9e3c4454d9
#12https://github.com/trishankatdatadog/tuf-on-a-plane/pull/12
83a94a9https://github.com/trishankatdatadog/tuf-on-a-plane/commit/83a94a9c1a167fac77c1636e3a0c2c9e3c4454d9
marcelmhttps://github.com/marcelm
on Aug 20, 2022https://github.com/python/typing/issues/59#event-7226294873
Add "Comparable" Protocolhttps://github.com/whatshap/whatshap/commit/fa6e1bef2af381de615936d6d6accefe6cbd7827
fa6e1behttps://github.com/whatshap/whatshap/commit/fa6e1bef2af381de615936d6d6accefe6cbd7827
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typing/issues/59
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typing/issues/59
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.