Skip to content
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

cloudpickle fails to pickle NewType in main module by value on Python 3.10 and newer #520

Open
hauntsaninja opened this issue Oct 11, 2023 · 1 comment

Comments

@hauntsaninja
Copy link

In a.py:

import cloudpickle
from typing import Any, NewType
RunDict = NewType("RunDict", dict[str, Any])
with open("tmp.pickle", "wb") as f:
    f.write(cloudpickle.dumps(RunDict))

In b.py

import cloudpickle
with open("tmp.pickle", "rb") as f:
    value = cloudpickle.load(f)
print(value)
λ python3.9 a.py
λ python3.9 b.py
<function NewType.<locals>.new_type at 0x1004580d0>
λ python3.10 a.py
λ python3.10 b.py
Traceback (most recent call last):
  File "/Users/shantanu/devoai/openai/b.py", line 3, in <module>
    value = cloudpickle.load(f)
AttributeError: Can't get attribute 'RunDict' on <module '__main__' from '/Users/shantanu/devoai/openai/b.py'>
@ogrisel
Copy link
Contributor

ogrisel commented Oct 13, 2023

Thanks for the report. I confirm I can reproduce with Python 3.11 (I haven't tried Python 3.12).

I suspect we might need something like the _typevar_reduce custom reducer. Feel free to open a PR if you wish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants