Skip to content

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

Closed
aronchick opened this issue Apr 9, 2020 · 11 comments
Closed

str() doesn't work as expected #232

aronchick opened this issue Apr 9, 2020 · 11 comments
Labels
Question Unclear or open issue subject for debate

Comments

@aronchick
Copy link

When I parse a version string into a SemVer dict, I would expect str(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.

@tomschr tomschr added the Question Unclear or open issue subject for debate label Apr 9, 2020
@tomschr
Copy link
Member

tomschr commented Apr 9, 2020

Hi @aronchick

concatenating a dict directly isn't possible. You can use the semver.format_version function like this:

>>> v1 = semver.parse("1.2.3")
>>> semver.format_version(**v1)
'1.2.3'

Would that help?

@aronchick
Copy link
Author

Yep, that's perfect!

Is it possible to add a clearer method for doing this?

@tomschr
Copy link
Member

tomschr commented Apr 9, 2020

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.

@tomschr
Copy link
Member

tomschr commented Apr 9, 2020

Not sure if you are aware of another alternative way of doing (almost) the same. You can use the VersionInfo class. For example:

>>> 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. 😉

@aronchick
Copy link
Author

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):

def __init__(self, version):
	__version = semver.parse(version)

def print_version():
	return str(__version) 

a = my_object('0.0.1')
print(f"This object's version is {my_object.print_version()}")

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.

@tomschr
Copy link
Member

tomschr commented Apr 9, 2020

Ahh, I see what you mean.

I think a more pythonic way would be avoid the method print_version and to replace it with the magic method __str__. This method exists for exactly this use case. It would even make the code a bit simpler:

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 __str__.

And, mentally, it doesn't feel right to ".parse()" again - i've created the object.

That's true, but I don't see from your code above that this would be the case. 😉

I was just expecting that this object would be able to render itself as a joined string on its own.

It does, that's why the VersionInfo class contains the magic method __str__.

Maybe I don't have all information, but can you use the semver.VersionInfo class directly?

@aronchick
Copy link
Author

OH ok, wow. I get it now. I just thought semver was the object that I wanted to store, not semver.VersionInfo. That was just confusing to me!

Yes, VersionInfo behaves exactly as expected - it's just strange to me because I would have expected semver.parse(<value>) to result in the object rather than semver.VersionInfo.parse(<value>)

@tomschr
Copy link
Member

tomschr commented Apr 9, 2020

No problem. 😄

Yes, it can be confusing. There are mostly two ways to deal with version information in semver: the object oriented way (with semver.VersionInfo) and the "module level functions" way with deal mostly with builtin objects (dicts and the function semver.parse).

I referenced issue #229 which deals exactly with this: to deprecated these "module level functions" so we only have the semver.VersionInfo object in the future.

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."

@s-celles
Copy link
Member

@tomschr you are 100% allowed to follow this path to improve python-semver!
Could you summarize what is locking in #229 ?

@tomschr
Copy link
Member

tomschr commented Apr 10, 2020

@scls19fr thanks, will do. 👍

@aronchick can we close this issue? It seems, we could solve your question, right? 🙂

@aronchick
Copy link
Author

Yep! Close away - thanks so much for your help and responsiveness!

@tomschr tomschr closed this as completed Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Unclear or open issue subject for debate
Projects
None yet
Development

No branches or pull requests

3 participants