Visitar URL original
str() doesn't work as expected · Issue #232 · python-semver/python-semver · GitHub
Skip to content

str() doesn't work as expected #232

Closed

Description

@aronchick

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.

Activity

  1. added
    QuestionUnclear or open issue subject for debate
    on Apr 9, 2020
  2. tomschr commented on Apr 9, 2020

    @tomschr
    Member

    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?

  3. aronchick commented on Apr 9, 2020

    @aronchick
    Author

    Yep, that's perfect!

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

  4. tomschr commented on Apr 9, 2020

    @tomschr
    Member

    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.

  5. tomschr commented on Apr 9, 2020

    @tomschr
    Member

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

  6. aronchick commented on Apr 9, 2020

    @aronchick
    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.

  7. tomschr commented on Apr 9, 2020

    @tomschr
    Member

    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?

  8. aronchick commented on Apr 9, 2020

    @aronchick
    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>)

  9. tomschr commented on Apr 9, 2020

    @tomschr
    Member

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

  10. s-celles commented on Apr 10, 2020

    @s-celles
    Member

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

  11. tomschr commented on Apr 10, 2020

    @tomschr
    Member

    @scls19fr thanks, will do. 👍

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

  12. aronchick commented on Apr 10, 2020

    @aronchick
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionUnclear or open issue subject for debate

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions