Skip to content

Commit

Permalink
Fix #440: Incorrect pickles for subclasses of generic classes (#448)
Browse files Browse the repository at this point in the history
* Fix #440: Incorrect pickles for subclasses of generic classes

* Update CHANGES

* Empty Commit to trigger CI

Co-authored-by: Pierre Glaser <pierreglaser@msn.com>
  • Loading branch information
huzecong and pierreglaser committed Feb 16, 2022
1 parent 5d89947 commit 1bf5277
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -5,6 +5,9 @@
and `abc.abstractstaticmethod`.
([PR #450](https://github.com/cloudpipe/cloudpickle/pull/450))

- Support for pickling subclasses of generic classes.
([PR #448](https://github.com/cloudpipe/cloudpickle/pull/448))

2.0.0
=====

Expand Down
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 @@ -2335,6 +2335,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 1bf5277

Please sign in to comment.