Title: Rethinking __init__ of VersionInfo class? · Issue #303 · python-semver/python-semver · GitHub
Open Graph Title: Rethinking __init__ of VersionInfo class? · Issue #303 · python-semver/python-semver
X Title: Rethinking __init__ of VersionInfo class? · Issue #303 · python-semver/python-semver
Description: Situation Originally posted by @tomschr in #258 (comment) From the above discussion, I thought it would be worth to decouple the compare discussion from the initializer discussion. Both are somewhat related, but can live independently. W...
Open Graph Description: Situation Originally posted by @tomschr in #258 (comment) From the above discussion, I thought it would be worth to decouple the compare discussion from the initializer discussion. Both are somewha...
X Description: Situation Originally posted by @tomschr in #258 (comment) From the above discussion, I thought it would be worth to decouple the compare discussion from the initializer discussion. Both are somewha...
Opengraph URL: https://github.com/python-semver/python-semver/issues/303
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Rethinking __init__ of VersionInfo class?","articleBody":"# Situation\r\n\r\n_Originally posted by @tomschr in https://github.com/python-semver/python-semver/issues/258#issuecomment-652482631_\r\n\r\nFrom the above discussion, I thought it would be worth to decouple the `compare` discussion from the initializer discussion. Both are somewhat related, but can live independently. \r\n\r\nWith a more \"advanced\" initializer/constructor we get the following benefits:\r\n\r\n* more \"pythonic\": it's one, obvious way to get an instance.\r\n* avoids the longer function call `Version.parse(...)`.\r\n* more readable\r\n\r\nWith such an (overloaded?) initializer, we could cover the following use cases:\r\n\r\n```python\r\n\u003e\u003e\u003e from semver import Version\r\n\u003e\u003e\u003e Version(1)\r\nVersion(major=1, minor=0, patch=0, prerelease=None, build=None)\r\n\u003e\u003e\u003e Version(1, \"4\", b\"5\")\r\nVersion(major=1, minor=4, patch=5, prerelease=None, build=None)\r\n\u003e\u003e\u003e Version(1, patch=2)\r\nVersion(major=1, minor=0, patch=2, prerelease=None, build=None)\r\n\u003e\u003e\u003e Version(\"1.2.3\")\r\nVersion(major=1, minor=2, patch=3, prerelease=None, build=None)\r\n\u003e\u003e\u003e Version(b\"1.2.3\")\r\nVersion(major=1, minor=2, patch=3, prerelease=None, build=None)\r\n\u003e\u003e\u003e v = Version(b\"2.3.4\")\r\n\u003e\u003e\u003e Version(v)\r\nVersion(major=2, minor=3, patch=4, prerelease=None, build=None)\r\n\u003e\u003e\u003e t = (1, 2, 3)\r\n\u003e\u003e\u003e Version(*t) \r\nVersion(major=1, minor=2, patch=3, prerelease=None, build=None)\r\n\u003e\u003e\u003e d = {'major': 3, 'minor': 4, 'patch': 5, 'prerelease': 'pre.2', 'build': 'build.4'}\r\n\u003e\u003e\u003e Version(**d)\r\nVersion(major=3, minor=4, patch=5, prerelease='pre.2', build='build.4')\r\n```\r\n\r\n# Discussions and Possible Solutions\r\n\r\nTo implement a somewhat more advanced constructor/initializer, we have these options:\r\n\r\n1. Program it manually with `isinstance` and if...else constructs\r\n1. Use the [`typing.overload`](https://docs.python.org/3.6/library/typing.html#typing.overload) function (suggested by @tlaferriere)\r\n1. Use [`functools.singledispatch`](https://docs.python.org/3.6/library/functools.html#functools.singledispatch)\r\n\r\nHowever, all three comes at a cost or an issue:\r\n\r\n1. Maybe not impossible, but the code would look probably ugly.\r\n1. \"The `@overload` decorator is purely for type hints, you can only specify one function body and it has to distinguish the different types using isinstance.` (_Originally posted by @tlaferriere in https://github.com/python-semver/python-semver/issues/258#issuecomment-650648949_)\r\n1. Is not possible with an `__init__` method. The `singledispatch` works only for _functions_(!), *not* methods. For methods we would need [`functools.singledispatchmethod`](https://docs.python.org/3.8/library/functools.html#functools.singledispatchmethod) which is only available from Python \u003e= 3.8.\r\n\r\n----\r\n\r\nAnother idea goes into a completely different direction. Maybe we shouldn't change the `Version` class much, but offer a much shorter variant: `semver.v`.\r\n\r\n```python\r\nfrom functools import singledispatch\r\n\r\n# ...\r\n@singledispatch\r\ndef ver(major, minor=0, patch=0, prerelease=None, build=None) -\u003e \"Version\":\r\n return Version(major, minor, patch, prerelease, build)\r\n \r\n@ver.register(bytes)\r\n@ver.register(str)\r\ndef _(ver: str) -\u003e \"Version\":\r\n if isinstance(ver, bytes):\r\n ver = str(ver, \"utf-8\")\r\n if \".\" in ver:\r\n return Version.parse(ver)\r\n return Version(int(ver))\r\n\r\n@ver.register(Version)\r\ndef _(ver: \"Version\") -\u003e \"Version\":\r\n return ver\r\n```\r\n\r\nWhich means, we could just use `semver.v` as a much shorter variant:\r\n\r\n```python\r\n\u003e\u003e\u003e import semver\r\n\u003e\u003e\u003e semver.v(\"1.2.3\")\r\nVersion(major=1, minor=2, patch=3, prerelease=None, build=None)\r\n```\r\n\r\nOne drawback could be that `v` is quite short. Maybe too short? Especially if you import it with `from semver import v` it could be easily overwritten by other, local variables.\r\nThat could be a bit avoided to use capital `V` or `ver`. Or we use the much longer name `semver.version`.\r\n\r\nThoughts? Any other ideas? Would that be worth the effort?\r\n\r\n@tlaferriere @gsakkis @ppkt ","author":{"url":"https://github.com/tomschr","@type":"Person","name":"tomschr"},"datePublished":"2020-10-26T19:22:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/303/python-semver/issues/303"}
| 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:b476bab4-f14d-afc6-1afa-07afaa3bca8f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B972:3EB92C:1FF5E28:2A3BD8A:696EF3DB |
| html-safe-nonce | 2b96bea5def40588d65b750b75a3fea9e01ec2ecae270186dcc94308a8078d23 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTcyOjNFQjkyQzoxRkY1RTI4OjJBM0JEOEE6Njk2RUYzREIiLCJ2aXNpdG9yX2lkIjoiMjA2Mzc1OTcxNzY2OTIwNDk1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c180b2373fe01384eae99e66efab73ae363d975d105a5f68c54c58be4dfddeb4 |
| hovercard-subject-tag | issue:729840307 |
| 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-semver/python-semver/303/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a0c466c57c510183db0691182cbb7251dd09d5a3f9cafae37566b924846ac8d7/python-semver/python-semver/issues/303 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a0c466c57c510183db0691182cbb7251dd09d5a3f9cafae37566b924846ac8d7/python-semver/python-semver/issues/303 |
| og:image:alt | Situation Originally posted by @tomschr in #258 (comment) From the above discussion, I thought it would be worth to decouple the compare discussion from the initializer discussion. Both are somewha... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tomschr |
| hostname | github.com |
| expected-hostname | github.com |
| None | b278ad162d35332b6de714dfb005de04386c4d92df6475522bef910f491a35ee |
| turbo-cache-control | no-preview |
| go-import | github.com/python-semver/python-semver git https://github.com/python-semver/python-semver.git |
| octolytics-dimension-user_id | 57228994 |
| octolytics-dimension-user_login | python-semver |
| octolytics-dimension-repository_id | 3375726 |
| octolytics-dimension-repository_nwo | python-semver/python-semver |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3375726 |
| octolytics-dimension-repository_network_root_nwo | python-semver/python-semver |
| 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 | 39aed5006635ab6f45e6b77d23e73b08a00272a3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width