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

Added enums, deprecate constants #5954

Merged
merged 5 commits into from Feb 18, 2022
Merged

Added enums, deprecate constants #5954

merged 5 commits into from Feb 18, 2022

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Jan 11, 2022

Resolves #5875, by replacing various sets of constants with IntEnum classes. This would seem to be the exact scenario for which enums were added to Python.

I have retained backwards compatibility by including lines like globals().update(enum.__members__).

I have updated internal usage to use the enums, but not the tests or documentation.

@hugovk
Copy link
Member

hugovk commented Jan 11, 2022

This is a good way to group things together, but for backwards compatibility we have each one doubled up:

Image.ROTATE_90
Image.Transpose.ROTATE_90
Image.ROTATE_180
Image.Transpose.ROTATE_180
Image.ROTATE_270 
Image.Transpose.ROTATE_270
# etc.

Although dir(Image) only lists the extra Transpose.

Is the idea be that end user code should be updated to use the enum form?

Do we keep the old constants "forever"?

Or do we want to deprecate the old constants and remove them in Pillow 10 (15 months' deprecation period)?

Copy link

@melonmouse melonmouse left a comment

Choose a reason for hiding this comment

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

To me, this change looks great! I'm a guest in this codebase, so can't judge it completely - thats the only reason I'm commenting and not approving.

One small remark would be that some of the introduced enum classes have names that are a bit generic (like Encoding) and might lead to naming collisions later.

BLP_ENCODING_UNCOMPRESSED = 1
BLP_ENCODING_DXT = 2
BLP_ENCODING_UNCOMPRESSED_RAW_BGRA = 3
class Format(IntEnum):

Choose a reason for hiding this comment

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

To avoid potential naming collisions, what about BlpFormat?

BLP_ALPHA_ENCODING_DXT3 = 1
BLP_ALPHA_ENCODING_DXT5 = 7

class Encoding(IntEnum):

Choose a reason for hiding this comment

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

To avoid potential naming collisions, what about BlpEncoding?

UNCOMPRESSED_RAW_BGRA = 3


class AlphaEncoding(IntEnum):

Choose a reason for hiding this comment

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

For consistency, what about BlpAlphaEncoding?

@radarhere
Copy link
Member Author

When you talk about naming collisions, I find it hard to imagine that we'd want to add another module-level variable called Format. Or are you thinking of a user running from PIL.BlpImagePlugin import Format?

I was trying to avoid users writing

>>> from PIL import BlpImagePlugin
>>> BlpImagePlugin.BlpFormat.JPEG

Instead,

>>> from PIL import BlpImagePlugin
>>> BlpImagePlugin.Format.JPEG

seems cleaner to me.

@radarhere
Copy link
Member Author

radarhere commented Jan 14, 2022

Ok, I've updated the tests, documentation, and deprecated the original constants.

I've also deprecated the "backwards compatibility" constants for Resampling, and deprecated NEAREST for use in Dither, which isn't documented, in favour of NONE.

@hugovk
Copy link
Member

hugovk commented Feb 18, 2022

Let's also add this to the "Deprecations and removals" page and release notes.

https://pillow.readthedocs.io/en/latest/deprecations.html

@radarhere
Copy link
Member Author

@hugovk hugovk merged commit 07741c9 into python-pillow:main Feb 18, 2022
@hugovk
Copy link
Member

hugovk commented Feb 18, 2022

Thanks!

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.

Change from constants to enums
3 participants