Skip to content

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Mar 5, 2025

https://docs.python.org/3/library/abc.html#abc.abstractmethod

@abc.abstractmethod
A decorator indicating abstract methods.

Using this decorator requires that the class’s metaclass is ABCMeta or is derived from it.

This is true. In main,

>>> from PIL import Image
>>> Image.ImagePointHandler()
<PIL.Image.ImagePointHandler object at 0x1044f93d0>

but once this PR changes the class to inherit from abc.ABC,

>>> from PIL import Image
>>> Image.ImagePointHandler()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class ImagePointHandler with abstract method point

While I'm here, I'm also going to replace

Pillow/src/PIL/ImageFile.py

Lines 458 to 460 in 7552893

def _open(self) -> None:
msg = "StubImageFile subclass must implement _open"
raise NotImplementedError(msg)
and

Pillow/src/PIL/ImageFile.py

Lines 474 to 477 in 7552893

def _load(self) -> StubHandler | None:
"""(Hook) Find actual image loader."""
msg = "StubImageFile subclass must implement _load"
raise NotImplementedError(msg)
with abstract methods.

@radarhere radarhere changed the title Inherit classes with abstractmethod from ABC Use ABCMeta in classes with abstractmethod Mar 5, 2025
@hugovk hugovk merged commit 5e9eea1 into python-pillow:main Mar 5, 2025
54 of 55 checks passed
@radarhere radarhere deleted the abstract branch March 5, 2025 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants