Skip to content

Fix ResourceWarning when passing pathlib.Path to send_photo/send_document #4908

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

Merged
merged 8 commits into from
Aug 17, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 13, 2025

When passing a pathlib.Path object to send_photo(), send_document(), or other file-sending methods, the library was generating a ResourceWarning due to an unclosed file handle. This became a critical issue in CI/CD environments using pytest with the -W error flag, causing tests to fail.

Problem

The issue occurred in the parse_file_input function where:

return InputFile(path.open(mode="rb"), filename=filename, attach=attach)

The file handle created by path.open() was passed to InputFile, which read the content but never explicitly closed the original handle. When the handle was garbage collected, Python issued a ResourceWarning.

Solution

Modified the code to use a context manager that ensures proper file closure:

with path.open(mode="rb") as file_handle:
    return InputFile(file_handle, filename=filename, attach=attach)

This minimal change ensures the file handle is automatically closed after InputFile reads the content, preventing the ResourceWarning.

Testing

  • Added a new test test_parse_file_input_path_no_resource_warning that specifically checks for the absence of ResourceWarning when using pathlib.Path
  • All existing tests continue to pass, confirming no regressions
  • Verified the fix resolves the original issue scenario

The change is surgical and maintains full backward compatibility while properly managing file resources.

Fixes #4907.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits August 13, 2025 23:51
Co-authored-by: harshil21 <37377066+harshil21@users.noreply.github.com>
Co-authored-by: harshil21 <37377066+harshil21@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] ResourceWarning: unclosed file when passing pathlib.Path to send_photo/send_document Fix ResourceWarning when passing pathlib.Path to send_photo/send_document Aug 13, 2025
@Copilot Copilot AI requested a review from harshil21 August 13, 2025 23:58
Copilot finished work on behalf of harshil21 August 13, 2025 23:58
@harshil21 harshil21 added the 🔌 bug pr description: bug label Aug 14, 2025
Copy link
Member

@harshil21 harshil21 left a comment

Choose a reason for hiding this comment

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

@copilot good job, I didn't expect the test suite to pass but it did.

Co-authored-by: harshil21 <37377066+harshil21@users.noreply.github.com>
Copilot finished work on behalf of harshil21 August 14, 2025 00:27
@Copilot Copilot AI requested a review from harshil21 August 14, 2025 00:27
@harshil21 harshil21 marked this pull request as ready for review August 14, 2025 00:38
@harshil21 harshil21 requested a review from Bibo-Joshi August 14, 2025 00:39
Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

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

I'm a bit impressed :D
Still, some comments/questions

@Bibo-Joshi Bibo-Joshi merged commit 19d891a into master Aug 17, 2025
28 of 30 checks passed
@Bibo-Joshi Bibo-Joshi deleted the copilot/fix-4907 branch August 17, 2025 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌 bug pr description: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ResourceWarning: unclosed file when passing pathlib.Path to send_photo/send_document
3 participants