robotools / vanilla

A Pythonic wrapper around Cocoa.
MIT License
78 stars 28 forks source link

distutils Version classes are deprecated. #168

Closed typemytype closed 2 years ago

typemytype commented 2 years ago

moving to packaging.version from the deprecated distutils.version. Lots of other tools are just checking string with a version number.. and packaging.version always expects a Version object and raises and type error.

To keep support we maybe have to subclass Version, I dont really like that but if we change it will break lots of interfaces where macOS version are checked...

from distutils.version import StrictVersion

osVersion10_7 = StrictVersion("10.7")

print("10.8" > osVersion10_7)
print(osVersion10_7 < "10.8")

from packaging.version import Version

def _checkString(func):
    def wrapper(self, other):
        if isinstance(other, str):
            other = Version(other)
        return func(self, other)
    return wrapper

class LooseVersion(Version):

    __lt__ = _checkString(Version.__lt__)
    __le__ = _checkString(Version.__le__)
    __eq__ = _checkString(Version.__eq__)
    __ge__ = _checkString(Version.__ge__)
    __gt__ = _checkString(Version.__gt__)
    __ne__ = _checkString(Version.__ne__)

osVersion10_7 = LooseVersion("10.7")

print("10.8" > osVersion10_7)
print(osVersion10_7 < "10.8")

osVersion10_7 = Version("10.7")

print("10.8" > osVersion10_7)
print(osVersion10_7 < "10.8")
justvanrossum commented 2 years ago

Are you saying the osVersionXXX constants from vanillaBase.py are used a lot outside of vanilla? You can't even import them via vanilla.

typemytype commented 2 years ago

fe: https://github.com/typedev/KernTool4/blob/86a0b7215e70c449ae66b40699e5910068092e5a/KernTool4.roboFontExt/lib/KernTool4.py#L176 https://github.com/RafalBuchner/masterTools/blob/00e4ea41c9ef6c9867a7fd7fa0cd1310f963b48d/master-tools.roboFontExt/lib/masterTools/UI/settings.py#L253

but they are not comparing with strings, which is good! this means I can make a PR in vanilla with a plain Version object, fixing the deprecated warning (without the subclass, oef)

DrawBot does not import from vanilla but is comparing it with a string... https://github.com/typemytype/drawbot/blob/abf00e47e0e812e878631bd61a98d0e48aa9d934/drawBot/context/baseContext.py#L1234

I will PR it in DrawBot to use the vanilla version constants.

justvanrossum commented 2 years ago

I think DrawBot should just use packaging.version, not vanilla's stuff, and not compare to a string.