Skip to content

Commit 1bf5277

Browse files
Fix #440: Incorrect pickles for subclasses of generic classes (#448)
* Fix #440: Incorrect pickles for subclasses of generic classes * Update CHANGES * Empty Commit to trigger CI Co-authored-by: Pierre Glaser <[email protected]>
1 parent 5d89947 commit 1bf5277

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
and `abc.abstractstaticmethod`.
66
([PR #450](https://github.com/cloudpipe/cloudpickle/pull/450))
77

8+
- Support for pickling subclasses of generic classes.
9+
([PR #448](https://github.com/cloudpipe/cloudpickle/pull/448))
10+
811
2.0.0
912
=====
1013

cloudpickle/cloudpickle.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,12 @@ def _typevar_reduce(obj):
910910

911911

912912
def _get_bases(typ):
913-
if hasattr(typ, '__orig_bases__'):
913+
if '__orig_bases__' in getattr(typ, '__dict__', {}):
914914
# For generic types (see PEP 560)
915+
# Note that simply checking `hasattr(typ, '__orig_bases__')` is not
916+
# correct. Subclasses of a fully-parameterized generic class does not
917+
# have `__orig_bases__` defined, but `hasattr(typ, '__orig_bases__')`
918+
# will return True because it's defined in the base class.
915919
bases_attr = '__orig_bases__'
916920
else:
917921
# For regular class objects

tests/cloudpickle_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,47 @@ def check_generic(generic, origin, type_value, use_args):
23352335
assert check_generic(C[int], C, int, use_args) == "ok"
23362336
assert worker.run(check_generic, C[int], C, int, use_args) == "ok"
23372337

2338+
def test_generic_subclass(self):
2339+
T = typing.TypeVar('T')
2340+
2341+
class Base(typing.Generic[T]):
2342+
pass
2343+
2344+
class DerivedAny(Base):
2345+
pass
2346+
2347+
class LeafAny(DerivedAny):
2348+
pass
2349+
2350+
class DerivedInt(Base[int]):
2351+
pass
2352+
2353+
class LeafInt(DerivedInt):
2354+
pass
2355+
2356+
class DerivedT(Base[T]):
2357+
pass
2358+
2359+
class LeafT(DerivedT[T]):
2360+
pass
2361+
2362+
klasses = [
2363+
Base, DerivedAny, LeafAny, DerivedInt, LeafInt, DerivedT, LeafT
2364+
]
2365+
for klass in klasses:
2366+
assert pickle_depickle(klass, protocol=self.protocol) is klass
2367+
2368+
with subprocess_worker(protocol=self.protocol) as worker:
2369+
2370+
def check_mro(klass, expected_mro):
2371+
assert klass.mro() == expected_mro
2372+
return "ok"
2373+
2374+
for klass in klasses:
2375+
mro = klass.mro()
2376+
assert check_mro(klass, mro)
2377+
assert worker.run(check_mro, klass, mro) == "ok"
2378+
23382379
def test_locally_defined_class_with_type_hints(self):
23392380
with subprocess_worker(protocol=self.protocol) as worker:
23402381
for type_ in _all_types_to_test():

0 commit comments

Comments
 (0)