Skip to content

Improve file discovery tests and inject PDF converter - #1

Open
SuperMuel wants to merge 1 commit into
mainfrom
codex/add-tests-for-file-discovery-and-outputs
Open

Improve file discovery tests and inject PDF converter#1
SuperMuel wants to merge 1 commit into
mainfrom
codex/add-tests-for-file-discovery-and-outputs

Conversation

@SuperMuel

Copy link
Copy Markdown
Owner

Summary

  • support case-insensitive PDF discovery
  • inject dependencies into process_and_save_pdf
  • add extensive unit tests for file discovery and output planning

Testing

  • uv run python -m pytest

https://chatgpt.com/codex/tasks/task_e_68416c86dd188328909c7f8f27a7d1d2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @SuperMuel, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

Hello team, gemini-code-assist here to provide a summary of this pull request. This PR, authored by SuperMuel, focuses on improving the robustness of file discovery and enhancing the testability of the PDF processing logic. The main changes involve making the PDF file discovery case-insensitive, injecting dependencies into the process_and_save_pdf function to facilitate testing, and adding a comprehensive suite of unit tests covering file discovery, output path planning, and the newly refactored processing function.

Highlights

  • Case-Insensitive PDF Discovery: The find_pdf_files function has been updated to correctly identify PDF files regardless of the case of the .pdf extension (e.g., .PDF, .pdf).
  • Dependency Injection: The process_and_save_pdf function now accepts optional process_func, extract_func, and save_func arguments, allowing external code (like tests) to inject custom implementations of these steps.
  • Extensive Unit Tests: A new test file (tests/test_file_discovery.py) has been added containing multiple tests for find_pdf_files, determine_output_directory, create_conversion_plan, and process_and_save_pdf (using injected mocks).

Changelog

Click here to see the changelog
  • src/mistral_ocr/init.py
    • Modified find_pdf_files to use rglob('*') and check the lowercased suffix (path.suffix.lower() == '.pdf') to support case-insensitive PDF extension matching.
  • src/mistral_ocr/ocr_utils.py
    • Added Callable import from typing.
    • Updated the signature of process_and_save_pdf to include keyword-only arguments process_func, extract_func, and save_func with default values pointing to the original functions.
    • Modified the implementation of process_and_save_pdf to call the injected functions (process_func, extract_func, save_func) instead of the hardcoded ones.
  • tests/test_file_discovery.py
    • Added a new test file.
    • Added tests for find_pdf_files covering single files, directories with subdirectories, case-insensitivity, and missing paths.
    • Added tests for determine_output_directory covering directory and file inputs.
    • Added tests for create_conversion_plan covering output path generation and skipping existing files.
    • Added a test for process_and_save_pdf demonstrating the use of injected dummy functions to verify its control flow.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Tests now check the case,
Dependencies find their place,
Code is easier to trace.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces improvements to file discovery, dependency injection for PDF processing, and extensive unit tests. The changes enhance the flexibility and testability of the OCR processing pipeline. Overall, the changes look good, but there are a few areas that could be improved.

Summary of Findings

  • File Discovery: The case-insensitive PDF discovery is a good improvement. Consider adding a comment explaining the use of rglob and potentially exploring Path.glob with case-insensitive patterns for performance.
  • Dependency Injection: The dependency injection in process_and_save_pdf enhances testability. Adding type hints and more descriptive parameter names would further improve readability.

Merge Readiness

The pull request is in good shape and introduces valuable improvements. Addressing the suggestions regarding comments and type hints would further enhance the code quality. I am unable to directly approve the pull request, and users should have others review and approve this code before merging.

Comment on lines +86 to +90
return [
path
for path in input_path.rglob("*")
if path.suffix.lower() == ".pdf"
]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a good change to make the PDF discovery case-insensitive. However, consider adding a comment explaining why rglob is used instead of glob for future maintainers. Also, consider using Path.glob with a case-insensitive glob pattern if supported by the underlying OS, as it might be more performant than filtering with a list comprehension.

Suggested change
return [
path
for path in input_path.rglob("*")
if path.suffix.lower() == ".pdf"
]
# Use rglob to find all files recursively and filter for case-insensitive PDF suffix.
return [
path
for path in input_path.rglob("*")
if path.suffix.lower() == ".pdf"
]

Comment on lines 138 to 147
def process_and_save_pdf(
client: Mistral, input_path: Path, output_dir: Path, force: bool = False
client: Mistral,
input_path: Path,
output_dir: Path,
force: bool = False,
*,
process_func: Callable[[Mistral, Path], OCRResponse] = process_pdf_file,
extract_func: Callable[[OCRResponse], str] = extract_markdown_from_response,
save_func: Callable[[str, Path], None] = save_markdown_to_file,
) -> tuple[bool, str, ProcessedDocument | None]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The injection of dependencies into process_and_save_pdf is a good practice for testability and flexibility. However, consider adding type hints for the process_func, extract_func, and save_func parameters to improve code readability and maintainability. Also, consider renaming the parameters to be more descriptive, e.g. pdf_processor, response_extractor, markdown_saver.

Suggested change
def process_and_save_pdf(
client: Mistral, input_path: Path, output_dir: Path, force: bool = False
client: Mistral,
input_path: Path,
output_dir: Path,
force: bool = False,
*,
process_func: Callable[[Mistral, Path], OCRResponse] = process_pdf_file,
extract_func: Callable[[OCRResponse], str] = extract_markdown_from_response,
save_func: Callable[[str, Path], None] = save_markdown_to_file,
) -> tuple[bool, str, ProcessedDocument | None]:
def process_and_save_pdf(
client: Mistral,
input_path: Path,
output_dir: Path,
force: bool = False,
*,
pdf_processor: Callable[[Mistral, Path], OCRResponse] = process_pdf_file,
response_extractor: Callable[[OCRResponse], str] = extract_markdown_from_response,
markdown_saver: Callable[[str, Path], None] = save_markdown_to_file,
) -> tuple[bool, str, ProcessedDocument | None]:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant