-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Fix ResourceWarnings during unittest #5264
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
Conversation
covering known sources of resource warnings
…tion still works an files are closed
Ok, the pipelines for python 3.5 both failed but from python 3.6 onwards they succeeded. I think this is what happened: https://docs.python.org/3/library/os.path.html#os.path.exists
First of all the logs produced by the broken Azure Pipelines:
For Windows it is almost the same message ( When looking at class
I guess, swapping the first two if-statements could fix this. Note, that I did not try compiling spacy in my local copy for python 3.5 (it took me too long to install another VS). I.e. I did not directly check, if I could reproduce this without the changes I did. So I could as well be wrong about this. |
Sure, it's not always possible to test on any platform! That's why we have the CI :-) I think swapping the two |
Ah ok, I see :) |
…failing python35(win) setup
I've added the "Work in progress" label, so feel free to "play around" ;-) |
…ython35(win) setup
…precation warnings in python35(win) setup to pop up
I think the changes can be reviewed now. These are the things I changed in addition those mentioned above:
Unfortunately I was not able to provide a unit test for this for with make_tempdir as d:
path = d / "test"
with path.open("w") as f:
f.write("test")
reader = Reader(path) But maybe I was using it wrong. Instead I provided a unit test using a |
This PR fixes the issue where files were not closed in a timely manner, after some objects were writing to them during tests using
unittest
#5230Description
I ran through possible sources for the
ResourceWarnings
being raised. Classes directly pointed out by the warnings wereLanguage
,Tagger
,Vectors
. There the methodsto_disk
used write functions that did not close properly after write when used within tests (unittest
andpytest
as it turned out). Tests were implemented by catching the warnings and checking whether they still occur.Having had a closer look, the classes
Pipes
andEntityLinker
seemed to have implemented similar versions ofto_disk
, so I took care of them as well.For each case I wrote unit tests (
unittest
,pytest
) and fixed the affected code in a minimal possible way, so that existing unit tests still run green.Side notes:
Vectors
the numpy methodsave
is used to persist numpy arrays to disk. Reading the source code (numpy versions 1.18 and 1.15) this should close the file descriptor. However, after wrapping the affected line into a context manager, theResourceWarnings
vorVectors
disappeared. I left a comment about this at the corresponding line I changed.ResourceWarnings
are swallowed bypytest
. They can be made visible inpytest
as again simply by catching them usingwarnings
.Tests were run using both,
pytest
andunittest
.Looking forward for your feedback. If there is anything that should be improved in this PR I'd be happy to do so.
Thanks for your great work!
Types of change
Bug fix
Checklist