← 输入框|github.com|刷新
Skip to content

VersionInfo.parse should be a class method #276

Closed

Description

@Anvil

I wanted to create a subclass of VersionInfo for company reasons (more checkings, more methods, etc) and it turned out the VersionInfo.parse is a staticmethod, always returning VersionInfo instances, no matter what.

By turning the parse static method into a class method and making it return cls(**version_parts) the result of parse will be an instance of the subclass (my class), not the superclass (VersionInfo).

e.g:

class MyBogusVersionInfo(semver.VersionInfo):

    @classmethod
    def parse(cls, version):
        if version.startswith("foobar"):
            version = version[6:]
        return super().parse(version)

print(type(MyBogusVersionInfo.parse("1.2.3")))

Current result is : <class 'semver.VersionInfo'>, while I would have expected <class 'MyBogusVersionInfo'>

Activity

  1. added
    Enhancement
    Question
    on Jul 21, 2020
  2. tomschr commented on Jul 21, 2020

    @tomschr
    Member

    Thanks @Anvil for your issue and the pull request. 👍

    That's an interesting approach. I see your point. However, not entirely sure if changing .parse from a staticmethod to a classmethod is completely backward compatible... 🤔

    Apart from that, the .parse method could be removed in semver3. We've discussed this in issue #258. For semver3, an idea is to "overload" the initializer to allow more datatypes. Maybe that could solve your use case. If we go this route, that would make the .parse and adapting it kind of obsolete.

    Do you need it for Python2 only or do you use it for Python3?

    Thanks for your pull request (PR). As the above discussion is not really solved, I would like to wait for this issue until the above issue is remedied.

    Just a short comment about your PR: you don't have to change anything right now (due to the open discussion). It's just a reminder for future PRs. We require test cases for each new feature. 😉 If a feature changes the public API, this also needs to be documented. Ideally with some code examples. For further details, refer to our Contributing to semver section.

  3. added
    Blocked
    on Jul 21, 2020
  4. Anvil commented on Jul 24, 2020

    @Anvil
    ContributorAuthor

    Hello

    As far as I can tell, there wont be any breaking by changing static to class. The reference "VersionInfo.parse" makes it automatically bound to the class.

    In general, all factory methods (methods that return an instance of the class) should be class methods, not staticmethods.

    I dont use python 2 anymore. Do you intent do keep compatibility with python2 ?

  5. Anvil commented on Jul 24, 2020

    @Anvil
    ContributorAuthor

    I can provide tests and examples if it's accepted, though. Feel free to ask.

  6. Anvil commented on Jul 24, 2020

    @Anvil
    ContributorAuthor

    A basic yet (i think) meaningful test case / example, would be to have a subclass that accepts "v1.2.3" as semver, so that you can directly feed git tag ot the parse method. The subclass would have a parse such as:

    class GitTagSemVer(VersionInfo):
        @classmethod
        def parse(cls, version: str):
            if not version.startswith('v'):
                raise ValueError(f"{version}: not a valid semantic version tag") 
            return super().parse(version[1:])
    
        def __str__(self):
            # Reconstruct the tag.
            return "v" + super().__str__()
    
    import git
    import logging
    
    def semver_tags(path):
        repo = git.Repo(path)
        for tag in repo.tags:
            try:
                yield GitTagSemVer.parse(tag)
            except ValueError:
                logging.error(f"{tag}: bad semver tag")
  7. tomschr commented on Oct 12, 2020

    @tomschr
    Member

    Thank you @Anvil for all the ideas. Sorry for not responding to it earlier. I hope I can work on that soon.

    Your ideas and arguments make sense to me. 👍

    Do you intent do keep compatibility with python2 ?

    That's a good question. If possible, I would love to have at least this issue compatible between the two versions. It doesn't look like a big problem. But let me think of it again, once I'm coming back to this issue.

  8. Anvil commented on Oct 16, 2020

    @Anvil
    ContributorAuthor

    I'm glade it makes sense. On my part, there is no requirement nor need for a python2 compatibility. My team and I are working with python >= 3.6. Thank you for your work !

  9. tomschr commented on Oct 16, 2020

    @tomschr
    Member

    I'm glade it makes sense.

    Absolutely! 👍 I'm glad you've reached out to us and contributed.

    On my part, there is no requirement nor need for a python2 compatibility. My team and I are working with python >= 3.6. Thank you for your work !

    That's great! I've added a small test case; there I had to adapt the super() method a bit. I almost forgot how that worked as I'm using Python 3 most of the day.

    Plan is to release 2.11.0 soon and start working on Python 3. Otherwise these compatibility things will always bite me. 😆

  10. tomschr commented on Oct 16, 2020

    @tomschr
    Member

    Integrated in commit 6e4b402 so closing this issue.

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

    BlockedEnhancementQuestion

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions