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

Cloudpickling of class variable fails for subclass of typing.Generic #463

Closed
dannyfriar opened this issue Feb 3, 2022 · 5 comments
Closed

Comments

@dannyfriar
Copy link

dannyfriar commented Feb 3, 2022

Related to: #440

Run the following to create the object and cloudpickle to disk:

import typing
import cloudpickle

T = typing.TypeVar("T")

class Base(typing.Generic[T]):    
    foo = "bar"
    
class Child(Base):
    pass

child = Child()

with open("test.pkl", "wb") as f:
    cloudpickle.dump(child, f)

Then if load in another Python process:

import cloudpickle

with open("test.pkl", "rb") as f:
    obj = cloudpickle.load(f)
    
print(obj.foo)

Gives AttributeError: 'Child' object has no attribute 'foo'

Using cloudpickle==2.0.0 and python version 3.8.12. Observed on both Windows 10 and CentOS 8.4.

The above example works correctly (i.e. does not throw the AttributeError) with cloudpickle==2.0.0 and python version 3.6.6.

@dannyfriar
Copy link
Author

dannyfriar commented Feb 4, 2022

Also fails if you modify the above to set a property on the parent class:

class Base(typing.Generic[T]):    
    @property
    def foo(self):
        return "bar"

@pierreglaser
Copy link
Member

pierreglaser commented Feb 4, 2022

Hi @dannyfriar, thanks for the report. We'll fix this in #448. Also, is:

import typing
import cloudpickle

T = typing.TypeVar("T")

class Base(typing.Generic[T]):    
    foo = "bar"
    
class Child(Base):
    pass

a legitimate typing use-case? Regardless of the answer, we'll fix this issue in cloudpickle, but I'm just curious.

@dannyfriar
Copy link
Author

Thanks @pierreglaser. I think this is a legitimate use case (obviously not the dummy example here). A better example from the mypy docs: https://mypy.readthedocs.io/en/stable/generics.html

@dannyfriar
Copy link
Author

@pierreglaser is it possible to merge #448 now?

@pierreglaser
Copy link
Member

Closed by #448

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