Serialize subclass of abstract class in python 3.7.1

Viewed 615

I have the following script:

from abc import ABC, abstractmethod
import dill


class A(ABC):

    @abstractmethod
    def test(self, a):
        """Abstract method"""


class B(A):

    def test(self, a):
        return a + 1


if __name__ == '__main__':
    test_obj = B()
    with open('test_save.pkl', 'wb') as f:
        dill.dump(test_obj, f)

When I run this script in a python 3.6 environment, it runs successfully. When I run it in a python 3.7.1 environment, it errors with:

TypeError: can't pickle _abc_data objects

One work around I have found is to move the class definition of B into a separate file and import it - then saving works. HOWEVER, assuming I do NOT want to do that:

1) is there a way to serialize with dill a subclass of an abstractclass defined in the same file

2) what changed between python 3.6 and python 3.7 that caused this?

0 Answers
Related