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
Domain: github.com
{"@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-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:578be699-2e41-ecbd-8e47-3f111f014eb2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E328:1A77D:33F394:45D604:6B0027AD |
| html-safe-nonce | 38bae8657c3183a6458f6beb51dc0e4498c6f555f6a70aee4b7248cefcb75ee1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzI4OjFBNzdEOjMzRjM5NDo0NUQ2MDQ6NkIwMDI3QUQiLCJ2aXNpdG9yX2lkIjoiNzExMDc4MzMyOTE4NzY2OTkzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cc73c341e9e69c30b0f309a46bf71a9e594ff81578a980d57958a465e3b0cb88 |
| hovercard-subject-tag | issue:63088239 |
| 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/59/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3628852b1b691ebd829f2878ee4ec3b7df171e6079f1992f5285d2da4044010c/python/typing/issues/59 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3628852b1b691ebd829f2878ee4ec3b7df171e6079f1992f5285d2da4044010c/python/typing/issues/59 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width