-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add Magika.get_content_type_info() public API method (fixes #826) #1318
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
Open
shivamtiwari3
wants to merge
2
commits into
google:main
Choose a base branch
from
shivamtiwari3:feat/expose-content-type-info-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -761,6 +761,37 @@ def test_get_model_and_output_content_types() -> None: | |
| }.issubset(model_content_types_set) | ||
|
|
||
|
|
||
| def test_get_content_type_info() -> None: | ||
| """Test Magika.get_content_type_info() — fixes #826. | ||
|
|
||
| Verifies that the public API correctly exposes ContentTypeInfo metadata | ||
| (mime_type, group, description, extensions, is_text) for every | ||
| ContentTypeLabel that is part of the content type knowledge base. | ||
| """ | ||
| m = Magika() | ||
|
|
||
| # Every output content type must be queryable and return valid metadata. | ||
| for label in m.get_output_content_types(): | ||
| info = m.get_content_type_info(label) | ||
| assert isinstance(info, ContentTypeInfo) | ||
| assert info.label == label | ||
| assert isinstance(info.mime_type, str) and info.mime_type != "" | ||
| assert isinstance(info.group, str) and info.group != "" | ||
| assert isinstance(info.description, str) and info.description != "" | ||
| assert isinstance(info.extensions, list) | ||
| assert isinstance(info.is_text, bool) | ||
|
|
||
| # Spot-check a well-known content type's metadata. | ||
| pdf_info = m.get_content_type_info(ContentTypeLabel.PDF) | ||
| assert pdf_info.mime_type == "application/pdf" | ||
| assert pdf_info.group == "document" | ||
| assert pdf_info.is_text is False | ||
|
|
||
| # Text-based type should report is_text=True. | ||
| py_info = m.get_content_type_info(ContentTypeLabel.PYTHON) | ||
| assert py_info.is_text is True | ||
|
Comment on lines
+764
to
+792
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test covers the success paths well, but it's missing a check for the error condition. The def test_get_content_type_info() -> None:
"""Test Magika.get_content_type_info() — fixes #826.
Verifies that the public API correctly exposes ContentTypeInfo metadata
(mime_type, group, description, extensions, is_text) for every
ContentTypeLabel that is part of the content type knowledge base.
"""
from magika.types import MagikaError
m = Magika()
# Every output content type must be queryable and return valid metadata.
for label in m.get_output_content_types():
info = m.get_content_type_info(label)
assert isinstance(info, ContentTypeInfo)
assert info.label == label
assert isinstance(info.mime_type, str) and info.mime_type != ""
assert isinstance(info.group, str) and info.group != ""
assert isinstance(info.description, str) and info.description != ""
assert isinstance(info.extensions, list)
assert isinstance(info.is_text, bool)
# Spot-check a well-known content type's metadata.
pdf_info = m.get_content_type_info(ContentTypeLabel.PDF)
assert pdf_info.mime_type == "application/pdf"
assert pdf_info.group == "document"
assert pdf_info.is_text is False
# Text-based type should report is_text=True.
py_info = m.get_content_type_info(ContentTypeLabel.PYTHON)
assert py_info.is_text is True
# Verify that an unknown label raises MagikaError.
with pytest.raises(MagikaError, match="is not in the content type knowledge base"):
# Note: we use a string here because all ContentTypeLabel enum
# members are expected to be in the KB.
m.get_content_type_info("this_is_not_a_real_label") |
||
|
|
||
|
|
||
| def test_magika_imports(): | ||
| imported_modules = utils.get_imported_objects_after_wildcard() | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great addition to the public API. I have a couple of suggestions for improvement:
try...exceptblock (EAFP - "it's easier to ask for forgiveness than permission") instead of checking for the key's presence before accessing it. This is generally more performant if the key is expected to be present most of the time.ContentTypeLabel.UNDEFINEDin theRaisessection of the docstring appears to be incorrect, asundefinedis present incontent_types_kb.min.jsonand thus will not raise an error. To avoid confusion, it's best to remove the example.Here's a suggested implementation incorporating these points: