cdgriffith / puremagic

Pure python implementation of identifying files based off their magic numbers
MIT License
158 stars 34 forks source link

Add a `imghdr_strict` option to `.what()` to deliver the same false positives #81

Closed cclauss closed 3 months ago

cclauss commented 3 months ago
- def what(file: os.PathLike | str | None, h: bytes | None) -> str | None:
+ def what(file: os.PathLike | str | None, h: bytes | None, imghdr_strict: bool = True) -> str | None:

imghdr_strict enables bug-for-bug compatibility between imghdr.what() and puremagic.what() when the imghdr returns a match but puremagic returns None. We believe that imghdr is delivering a "false positive" in each of these scenarios but we want puremagic.what()'s default behavior to match imghdr.what()'s false positives so we do not break existing applications.

If imghdr_strict is True (the default) then a lookup will be done to deliver a matching result on all known false positives. If imghdr_strict is False then puremagic's algorithms will determine the image type. True is more compatible while False is more correct.

NOTE: This compatibility effort only deals with imghdr's false positives and we are not interested in tracking the opposite situation where puremagic's delivers a match while imghdr would have returned None. Also, puremagic.what() can recognize many more file types than the twelve image file types that imghdr focused on.

@NebularNerd Your review, please.

NebularNerd commented 3 months ago

Sounds good, at least folks using imghdr will continue to get the results they expect. 🙂

cclauss commented 3 months ago

When you think a pull request is useful and is ready to be merged, please consider giving it a positive review.

Every check mark ✔️ at the top right of this page gives project maintainers confidence that the proposed changes have been read through and deemed both useful and safe to merge into the codebase.

Anyone can review a pull request on GitHub. To do so here:

  1. Scroll to the top of this page.
  2. Click the Files changed tab and read through each file carefully looking for potential issues.
  3. Click the Review changes button.
  4. Click Approve (or one of the other options) and add comments only if they have not already been stated in the PR.
  5. Click Submit review so that your ✔️ will be added to the list.