-
Notifications
You must be signed in to change notification settings - Fork 96
str() doesn't work as expected #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @aronchick concatenating a dict directly isn't possible. You can use the >>> v1 = semver.parse("1.2.3")
>>> semver.format_version(**v1)
'1.2.3' Would that help? |
Yep, that's perfect! Is it possible to add a clearer method for doing this? |
What do you have in mind with "clearer method"? Do you mean the name of the function? Or something different? Currently, we are discussing to deprecate certain functions for the next major release. For details see #229. It's still under discussion, but depending on the outcome, this function could also be affected. |
Not sure if you are aware of another alternative way of doing (almost) the same. You can use the >>> v2 = semver.VersionInfo.parse("2.3.4")
>>> str(v2)
'2.3.4' See also the sections Creating a Version and Parsing a Version String in the semver documentation. In case the documentation is lacking some important information, let me know so I can improve it. In that case we can turn this issue into a documentation bug. 😉 |
so, my methodology is to do the parse once, and then treat it like an object. In this case, my flow would be (forgive the hacky code):
or some equivalent. I can put whatever I want into the accessor method (obviously), but i didn't even know about the 'format_version' method. And, mentally, it doesn't feel right to ".parse()" again - i've created the object. I was just expecting that this object would be able to render itself as a joined string on its own. |
Ahh, I see what you mean. I think a more pythonic way would be avoid the method def __init__(self, version):
__version = semver.parse(version)
def __str__(self):
return str(self.__version)
# outside the class:
a = my_object('0.0.1')
print(f"This object's version is {a}") This would implicitly call the magic method
That's true, but I don't see from your code above that this would be the case. 😉
It does, that's why the VersionInfo class contains the magic method Maybe I don't have all information, but can you use the |
OH ok, wow. I get it now. I just thought Yes, |
No problem. 😄 Yes, it can be confusing. There are mostly two ways to deal with version information in semver: the object oriented way (with I referenced issue #229 which deals exactly with this: to deprecated these "module level functions" so we only have the IMHO, this would make the project a bit easier to understand and adheres more to the Zen of Python: "There should be one-- and preferably only one --obvious way to do it." |
@scls19fr thanks, will do. 👍 @aronchick can we close this issue? It seems, we could solve your question, right? 🙂 |
Yep! Close away - thanks so much for your help and responsiveness! |
When I parse a version string into a
SemVer
dict, I would expectstr(semver_variable)
to return a concatenated string.Is there a best practice here? I'd really like to store my variable as a SemVer, but I have to print it often, so having a simple pattern for doing so would be great.
The text was updated successfully, but these errors were encountered: