Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Use ABCMeta in classes with abstractmethod#8803

Merged
hugovk merged 2 commits into
python-pillow:mainpython-pillow/Pillow:mainfrom
radarhere:abstractradarhere/Pillow:abstractCopy head branch name to clipboard
Mar 5, 2025
Merged

Use ABCMeta in classes with abstractmethod#8803
hugovk merged 2 commits into
python-pillow:mainpython-pillow/Pillow:mainfrom
radarhere:abstractradarhere/Pillow:abstractCopy head branch name to clipboard

Conversation

@radarhere

@radarhere radarhere commented Mar 5, 2025

Copy link
Copy Markdown
Member

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
@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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.