-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Attempting to dill
a function defined in a doctest
run with pytest
causes a TypeError: cannot pickle 'EncodedFile' object
.
#12448
Comments
This is also probably relevant. If I extend the example to include class MyClass:
"""Running a file containing this class with `python -m pytest this_file.py --doctest-modules` will fail with `TypeError: cannot pickle 'EncodedFile' object`.
Examples:
>>> def template_function():
... return "Hello, World!"
>>> import dill
>>> string = dill.dumps(template_function)
>>> dill.loads(string)()
'Hello, World!'
"""
|
Does it work when IO capture is disabled? |
I get a different error during unpickling that also only occurs with pytest. You can see my second post in this issue for those details. |
Based on the traceback dill pickles random objects We might be able to give better errors but we cant fix dill doing things that are fragile |
Facing the same issue. Something's off with module parser: running (.venv) user:~/proj/proj-agent $ pytest tests/agent/ PASSED
whereas running bare (.venv) user:~/proj/proj-agent [j1] $ pytest -k test_tool FAIL
The pickled subject being a function referenced from within a fixture's inner scope
Other observations
|
You need to disable assertion rewrite for at least the module And then hope It's generally unsafe to serialize inner functions in any way Dill is playing with fire there and it breaks whenever someone throws in objects that aren't serializable |
pip list
from the virtual environment you are usingDescription of the Problem
When I attempt to
dill
a function defined inside of adoctest
,pytest
raises an error, complaining thatTypeError: cannot pickle 'EncodedFile' object
. Best I can tell, this is theEncodedFile
object in pytest.This error only occurs when I run those doctests via pytest. They run just fine via vanilla doctest. The same code also works just fine in a script and as a regular pytest test function.
I fear that this is related to #10845, which doesn't seem to have a solution.
Minimal Reproducible Example
Using pytest to run those doctests will cause the
TypeError: cannot pickle 'EncodedFile' object
.Using vanilla doctest works fine. (Ignore the fact that the test failed. The expected output did not match. Note that it did successfully dump the function using dill.)
Using a regular pytest function to perform the test also works fine. This is only when attempting to dill a function defined in a doctest run with pytest.
Pytest and Operating System versions
This was run on Pytest 8.2.2, Python 3.11 on an M1 Macbook Air (macOS Sonoma 14.4.1). The same errors were also seen on Python 3.9 on the same Macbook and on Python 3.11 on an amd64 Ubuntu Machine (Ubuntu 22.04 LTS).
Pip list
The text was updated successfully, but these errors were encountered: