libtmux icon indicating copy to clipboard operation
libtmux copied to clipboard

Trying to get a compliant, typed version parser/comparator

Open tony opened this issue 3 years ago • 5 comments

Companion: https://github.com/tmux-python/tmuxp/pull/727

See also:

  • https://github.com/asottile/flake8-typing-imports/blob/923a533/flake8_typing_imports.py#L32-L42

  • https://github.com/pypa/packaging/blob/5984e3b25/packaging/version.py#L444

  • [x] String comparison's against Version

    @layday's snippet

  • [ ] LegacyVersion deprecation

    In packaging, LegacyVersion (the supercessor to LooseVersion) will be deprecated, we won't be able to support 2.4-openbsd (like what is seen on OpenBSD's tmux versions

      /home/t/work/python/libtmux/.venv/lib/python3.10/site-packages/packaging/version.py:127: 
        DeprecationWarning: Creating a LegacyVersion has been deprecated and will be removed in the next major release
        warnings.warn(
    
    -- Docs: https://docs.pytest.org/en/stable/warnings.html
    
  • [ ] tmux versions

     To the above, consider subclassing `Version` for tmux versions to handle git builds, platform builds, etc. then keeping `Version` for supporting pypi releases
    
  • [ ] Version Assure that libtmux.__version__ / tmuxp.__version__ supports git refs / local match groups

tony avatar Jan 11 '22 13:01 tony

Hi @Tony So in response to your question in the yt issue, I'm actually not sure my solution version_tuple fits your needs, if you want to support version schemes that used to rely on distutils.LooseVersion, my function may not work.

version_tuple is explicitly aimed only at parsing int tuples, and will truncate any trailing str to support alphas/betas

for instance

>>> version_tuple("2.13-hello")
(2, 13)

is this really what you need ?

neutrinoceros avatar Jan 15 '22 15:01 neutrinoceros

@neutrinoceros I saw your follow up here and realize it is explicitly tuple related and I'm dabbling with keeping it as an object: #351. I've underestimated the effort required.

I suppose if it takes no time at all, you could PR the function in src/libtmux/util.py

tony avatar Jan 15 '22 17:01 tony

Seems like you got a better solution from the packaging thread, I won't open a PR after all. Anyway feel free to copy my solution in case you need it still.

neutrinoceros avatar Jan 16 '22 09:01 neutrinoceros

Seems like you got a better solution from the packaging thread, I won't open a PR after all. Anyway feel free to copy my solution in case you need it still.

Thank you @neutrinoceros! @layday's mixin for Version seems quite nice and I'm trialing it in another PR here.

In case anyone googles their way here, @layday's snippet:

from functools import total_ordering

from packaging.version import Version


@total_ordering
class _VersionCmpMixin:
    def __eq__(self, other: object) -> bool:
        if isinstance(other, str):
            other = self.__class__(other)
        return super().__eq__(other)

    def __lt__(self, other: object) -> bool:
        if isinstance(other, str):
            other = self.__class__(other)
        return super().__lt__(other)


class MyVersion(_VersionCmpMixin, Version):
    pass


assert MyVersion("3") == "3.0.0"
assert MyVersion("3") != "3.0.1"

tony avatar Jan 16 '22 14:01 tony

Codecov Report

Merging #348 (42bc294) into master (5166809) will decrease coverage by 0.76%. The diff coverage is 88.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #348      +/-   ##
==========================================
- Coverage   87.82%   87.05%   -0.77%     
==========================================
  Files          15       15              
  Lines        1511     1530      +19     
==========================================
+ Hits         1327     1332       +5     
- Misses        184      198      +14     
Impacted Files Coverage Δ
libtmux/common.py 85.13% <88.23%> (-1.80%) :arrow_down:
tests/test_common.py 92.17% <0.00%> (-6.09%) :arrow_down:
tests/test_server.py 98.18% <0.00%> (-1.82%) :arrow_down:
tests/test_window.py 98.14% <0.00%> (-0.62%) :arrow_down:
libtmux/session.py 81.32% <0.00%> (-0.23%) :arrow_down:
libtmux/window.py 85.36% <0.00%> (+1.02%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 5166809...42bc294. Read the comment docs.

codecov[bot] avatar Feb 26 '22 17:02 codecov[bot]