Skip to content

Commit

Permalink
Fix #440: Incorrect pickles for subclasses of generic classes
Browse files Browse the repository at this point in the history
  • Loading branch information
huzecong committed Sep 18, 2021
1 parent b959d80 commit 8862e70
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cloudpickle/cloudpickle.py
Expand Up @@ -910,8 +910,12 @@ def _typevar_reduce(obj):


def _get_bases(typ):
if hasattr(typ, '__orig_bases__'):
if '__orig_bases__' in getattr(typ, '__dict__', {}):
# For generic types (see PEP 560)
# Note that simply checking `hasattr(typ, '__orig_bases__')` is not
# correct. Subclasses of a fully-parameterized generic class does not
# have `__orig_bases__` defined, but `hasattr(typ, '__orig_bases__')`
# will return True because it's defined in the base class.
bases_attr = '__orig_bases__'
else:
# For regular class objects
Expand Down
41 changes: 41 additions & 0 deletions tests/cloudpickle_test.py
Expand Up @@ -2223,6 +2223,47 @@ def check_generic(generic, origin, type_value, use_args):
assert check_generic(C[int], C, int, use_args) == "ok"
assert worker.run(check_generic, C[int], C, int, use_args) == "ok"

def test_generic_subclass(self):
T = typing.TypeVar('T')

class Base(typing.Generic[T]):
pass

class DerivedAny(Base):
pass

class LeafAny(DerivedAny):
pass

class DerivedInt(Base[int]):
pass

class LeafInt(DerivedInt):
pass

class DerivedT(Base[T]):
pass

class LeafT(DerivedT[T]):
pass

klasses = [
Base, DerivedAny, LeafAny, DerivedInt, LeafInt, DerivedT, LeafT
]
for klass in klasses:
assert pickle_depickle(klass, protocol=self.protocol) is klass

with subprocess_worker(protocol=self.protocol) as worker:

def check_mro(klass, expected_mro):
assert klass.mro() == expected_mro
return "ok"

for klass in klasses:
mro = klass.mro()
assert check_mro(klass, mro)
assert worker.run(check_mro, klass, mro) == "ok"

def test_locally_defined_class_with_type_hints(self):
with subprocess_worker(protocol=self.protocol) as worker:
for type_ in _all_types_to_test():
Expand Down

0 comments on commit 8862e70

Please sign in to comment.