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

Replace now-deprecated imghdr module #3304

Open
MaggieFero opened this issue Mar 3, 2024 · 1 comment
Open

Replace now-deprecated imghdr module #3304

MaggieFero opened this issue Mar 3, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@MaggieFero
Copy link
Contributor

Describe the bug
The imghdr module is deprecated as of Python 3.11, and will be removed in Python 3.13. The deprecation notice is here: https://docs.python.org/3/library/imghdr.html but the PEP referenced as featuring details and alternatives has only details, no specific alternative.

We'll need to find an alternative to imghdr by the time we move to 3.13, and when we do we can remove the linter exception introduced into bookwyrm/connectors/abstract_connector.py with PR #3303 as part of the Python 3.11 upgrade.

To Reproduce
See https://docs.python.org/3/library/imghdr.html and https://peps.python.org/pep-0594/#imghdr for details of the deprecation

Expected behavior
We should find a replacement! <3

Screenshots
N/A

Instance
All

Additional context
N/A

@MaggieFero MaggieFero added the bug Something isn't working label Mar 3, 2024
@Minnozz
Copy link
Contributor

Minnozz commented Mar 8, 2024

Pillow (an existing dependency) supports detecting image types by opening the image and looking at the format property: https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format

Both places in the code where this functionality is used (that I could find), the purpose is to generate a random filename with an extension that matches the encoding of the image:

try:
image_content, extension = get_image(url)
except: # pylint: disable=bare-except
return None
if not image_content:
return None
image_name = str(uuid4()) + "." + extension

image_content, extension = get_image(url)
if not image_content:
return None
image_name = f"{uuid4()}.{extension}"
return [image_name, image_content]

Using Pillow this way would parse the entire image instead of just the header, so that makes it less efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants