Skip to content
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

Update python version regex and fix version ordering #10613

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

amazimbe
Copy link
Contributor

@amazimbe amazimbe commented Sep 16, 2024

What are you trying to accomplish?

Link to issue
Update our python version implementation to be conformant with the python version specification here: https://peps.python.org/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions

I'm updating the regex for version formats to be the one specified in the python version specification.

Anything you want to highlight for special attention from reviewers?

How will you know you've accomplished your goal?

Checklist

  • I have run the complete test suite to ensure all tests and linters pass.
  • I have thoroughly tested my code changes to ensure they work as expected, including adding additional tests for new functionality.
  • I have written clear and descriptive commit messages.
  • I have provided a detailed description of the changes in the pull request, including the problem it addresses, how it fixes the problem, and any relevant details about the implementation.
  • I have ensured that the code is well-documented and easy to understand.

Copy link
Contributor

@kbukum1 kbukum1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a merged python regex update changes related to this test cases? Or this is refining the test cases? I couldn't see the regex changes here?

@amazimbe amazimbe force-pushed the amazimbe/update-python-version-regex branch from 5b5c2f8 to 1ee202d Compare September 17, 2024 09:46
@@ -104,7 +105,7 @@
"1.0.0-beta.11",
"1.0.0-rc.1",
"1",
# "1.0.0.post", TODO fails comparing to 1
"1.0.0.post", # TODO: fails comparing to 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this red flag while adding the test cases and was curious to find out what the sorted list looks like.

 expected: ["", "0.9", "1.0.0-alpha", "1.0.0-a.1", "1.0.0-beta", "1.0.0-b.2", "1.0.0-beta.11", "1.0.0-rc.1", "1", "1.0.0.post", "1.0.0+gc1", "1.post2", "1.post2+gc1", "1.post2+gc1.2", "1.post2+gc1.11", "1.0.0.post11", "1.0.2", "1.0.11", "2016.1", "1!0.1.0", "2!0.1.0", "10!0.1.0"]
            got: ["", "0.9", "1", "1!0.1.0", "1.0.0+gc1", "1.0.0-a.1", "1.0.0-alpha", "1.0.0-b.2", "1.0.0-beta", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0.post", "1.0.0.post11", "1.0.11", "1.0.2", "1.post2", "1.post2+gc1", "1.post2+gc1.11", "1.post2+gc1.2", "10!0.1.0", "2!0.1.0", "2016.1"]

There are a few things wrong with this output. The first is that it's not sorting version epocs correctly. In ascending order, versions like "1!1.0" should come after versions without an epoch like "0.9" which is equivalent to 0!0.9.

The epocs also need to be ordered numerically as in "1!0.1.0", "2!0.1.0", "10!0.1.0" and this is not the case here where we are ordering alphabetically.

We are also ordering versions without epochs alphabetically rather than numerically and that is why we have "1.0.11" appear before "1.0.2". The python version spec is clear on this

All numeric components MUST be interpreted and ordered according to their numeric value, not as text strings.

Finally, the sort order for versions with suffixes is also incorrect. The python spec say the correct order is .devN, aN, bN, rcN, , .postN and within each suffix we should sort numerically where applicable.

So "1.0.0-alpha" < "1.0.0-a.1" but we're seeing the opposite. Likewise, in the commented out test "1" < "1.0.0.post" but that's not what we get.

@amazimbe
Copy link
Contributor Author

Is there a merged python regex update changes related to this test cases? Or this is refining the test cases? I couldn't see the regex changes here?

It's coming. It's got python regex features and converting those to ruby has been problematic so far.

VERSION_PATTERN = r"""
    v?
    (?:
        (?:(?P<epoch>[0-9]+)!)?                           # epoch
        (?P<release>[0-9]+(?:\.[0-9]+)*)                  # release segment
        (?P<pre>                                          # pre-release
            [-_\.]?
            (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
            [-_\.]?
            (?P<pre_n>[0-9]+)?
        )?
        (?P<post>                                         # post release
            (?:-(?P<post_n1>[0-9]+))
            |
            (?:
                [-_\.]?
                (?P<post_l>post|rev|r)
                [-_\.]?
                (?P<post_n2>[0-9]+)?
            )
        )?
        (?P<dev>                                          # dev release
            [-_\.]?
            (?P<dev_l>dev)
            [-_\.]?
            (?P<dev_n>[0-9]+)?
        )?
    )
    (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?       # local version
"""

@amazimbe amazimbe changed the title Update python version regex and add test cases Update python version regex and fix version ordering Sep 18, 2024
@amazimbe amazimbe force-pushed the amazimbe/update-python-version-regex branch from 1ee202d to 49a1514 Compare September 19, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants