Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@hugovk
Copy link
Member

@hugovk hugovk commented Mar 29, 2020

PILLOW_VERSION was deprecated in Pillow 5.2.0 in July 2018 (#3090, #3782) and removed in 7.0.0 in January 2020 (#4107).

However, it didn't raise a deprecation warning. The removal has caused problems for users of torchvision.

torchvision had already updated to use __version__ in October 2019 (pytorch/vision#1501) but when Pillow 7.0.0 came out on 2020-01-02, had not yet released it (pytorch/vision#1712).

The fixed torchvision 0.5.0 was released on 2020-01-16.

However, some people are still having difficulties using torchvision 0.5.0 and Pillow 7.0.0 (pytorch/vision#1712) and are pinning to old Pillow versions or editing the source. As it's not much code for us, I suggest re-adding PILLOW_VERSION until some unspecified future release.


As before, this still doesn't raise a deprecation warning. I don't know if it's possible to raise one when calling a constant (or calling something that looks like calling a constant), but that would be welcome for 7.1.0 or a later release.

@hugovk hugovk added the Deprecation Feature that will be removed in the future label Mar 29, 2020
@hugovk hugovk added this to the 7.1.0 milestone Mar 29, 2020
@wiredfool
Copy link
Member

wiredfool commented Mar 29, 2020

Does @property, or something like it work for modules?

@hugovk
Copy link
Member Author

hugovk commented Mar 29, 2020

No, but we can do something like https://stackoverflow.com/a/880550/724176:

# __init__.py
import sys
import warnings


class _Deprecated_Version(object):
    def deprecated_version(self):
        warnings.warn(
            "PILLOW_VERSION is deprecated and will be removed in a future release. "
            "Use __version__ instead.",
            DeprecationWarning,
            stacklevel=2,
        )
        return __version__

    PILLOW_VERSION = property(deprecated_version)


sys.modules[__name__] = _Deprecated_Version()
# 1.py
import PIL

print(dir(PIL))
print(PIL.PILLOW_VERSION)
$ python -Wall 1.py
['PILLOW_VERSION', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'deprecated_version']
1.py:5: DeprecationWarning: PILLOW_VERSION is deprecated and will be removed in a future release. Use __version__ instead.
  print(PIL.PILLOW_VERSION)
7.1.0.dev0

@hugovk
Copy link
Member Author

hugovk commented Mar 29, 2020

However, the sys.modules[__name__] = _Deprecated_Version() in __init__.py means:

>>> from PIL import Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Image' from '<unknown module name>' (unknown location)

@hugovk
Copy link
Member Author

hugovk commented Mar 29, 2020

Alternatively, use PEP 562 to warn for at least Python 3.7+ (https://stackoverflow.com/a/57263518/724176), put this in both __init__.py and Image.py:

if sys.version_info >= (3, 7):
    def __getattr__(name):
        if name == 'PILLOW_VERSION':
            warnings.warn(
                "PILLOW_VERSION is deprecated and will be removed in a future release. "
                "Use __version__ instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            return __version__
        raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name))
else:
    PILLOW_VERSION = __version__
# 1.py
import PIL
from PIL import Image

print(PIL.PILLOW_VERSION)
print(Image.PILLOW_VERSION)
$ python3.6 -Wall 1.py
7.1.0.dev0
7.1.0.dev0
$ python3.7 -Wall 1.py
1.py:5: DeprecationWarning: PILLOW_VERSION is deprecated and will be removed in a future release. Use __version__ instead.
  print(PIL.PILLOW_VERSION)
7.1.0.dev0
1.py:6: DeprecationWarning: PILLOW_VERSION is deprecated and will be removed in a future release. Use __version__ instead.
  print(Image.PILLOW_VERSION)
7.1.0.dev0

Of last month's pip downloads, there were about 7.2m downloads for Python 3.5+. About 52% were 3.5-3.6, about 48% for 3.7+.

@python-pillow python-pillow deleted a comment from codecov bot Mar 31, 2020
@python-pillow python-pillow deleted a comment from codecov bot Mar 31, 2020
@hugovk
Copy link
Member Author

hugovk commented Mar 31, 2020

Thanks to @radarhere, updated via hugovk#46 with deprecation warnings via a class for some common uses for Python 3.5-3.6, and using PEP 562/__getattr__ for 3.7+.

@hugovk hugovk merged commit 16e1d6a into python-pillow:master Mar 31, 2020
@hugovk hugovk deleted the re-add-PILLOW_VERSION branch March 31, 2020 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Deprecation Feature that will be removed in the future

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants