-
Notifications
You must be signed in to change notification settings - Fork 48
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
Fix ResourceWarning due to unclosed file #212
Conversation
Codecov Report
@@ Coverage Diff @@
## main #212 +/- ##
==========================================
- Coverage 91.95% 91.91% -0.04%
==========================================
Files 46 46
Lines 3827 3835 +8
==========================================
+ Hits 3519 3525 +6
- Misses 308 310 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Thank you for the catch! This is indeed a bug, but I don't think we should even open that file to get its path. Instead we should simply retrieve the path. In @functools.lru_cache()
def datamol_data_file_path(filename: str, dm_module: str = "datamol.data"):
if sys.version_info < (3, 9, 0):
with importlib_resources.path("datamol.data", filename) as p:
data_path = p
else:
data_path = importlib_resources.files(dm_module).joinpath(filename)
return str(data_path) |
Thanks for the suggestion @hadim, that's definitely a much better way of handling things. I'll push an update a little later today. |
@hadim I just pushed a commit with the requested changes. |
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.
Thank you. See a small comment below.
Thank you, @kkovary, for your contribution! |
Thank you @hadim for the wonderful tools, hope to make more contributions in the future 😄 |
Changelogs
closes #211
This PR addresses a ResourceWarning that was being raised due to an unclosed file in datamol/mol.py. The warning was triggered by the following line of code:
This line opens a file but does not close it, which is why the ResourceWarning was being raised. To resolve this issue, I've modified the line to use a with block, which ensures that the file is properly closed after its name is retrieved:
This change should prevent the ResourceWarning from being raised in the future. I've tested the change locally and confirmed that it resolves the warning without causing any other issues.
Checklist:
feature
,fix
ortest
(or ask a maintainer to do it for you).discussion related to that PR