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

For Python 3.13: A drop-in replacement for imghdr.what() #178

Closed
wants to merge 2 commits into from

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented May 20, 2024

The Python 3.13 release notes mention filetype.py as one of the alternatives for imghdr which was removed from the Standard Library.

Given the discussion in #175, it might be quite helpful to have a drop-in replacement for imghdr.what(). It would provide a smooth transition to Py3.13 if developers could confidently replace all instances of imghdr.what() with filetype.what() -- same args, same results.


Current pytest results: 49 passed in 0.04s

  • test_what_from_file is 4 tests
  • ztest_what_from_file_none is 0 test -- disabled with the leading z
  • test_what_from_string is 10 tests
  • test_what_from_string_py311 is 2 tests
    • Fail on imghdr only on Python < 3.11
  • test_what_from_string_todo is 33 tests -- Lots of work to do!
    • Fail on filetype
def what(file: PathLike | str | None, h: bytes | None) -> str:
    """A drop-in replacement for `imghdr.what()` which was removed from the standard
    library in Python 3.13."""

Usage:

# Replace...
from imghdr import what
# with...
from filetype import what

# Or replace...
import imghdr
ext = imghdr.what(...)
# with...
import filetype
ext = filetype.what(...)

imghdr documentation: https://docs.python.org/3.12/library/imghdr.html
imghdr source code: https://github.com/python/cpython/blob/3.12/Lib/imghdr.py

@cclauss cclauss changed the title For Python 3.13: A drop-in replacement for imghdr.what For Python 3.13: A drop-in replacement for imghdr.what() May 20, 2024
@cclauss
Copy link
Contributor Author

cclauss commented Jun 18, 2024

Closing in favor of puremagic.what() because it has better recognition of image file formats.

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.

1 participant