@@ -51,30 +51,28 @@ class ConfigValue(NamedTuple):
51
51
rebuild : _ConfigRebuild
52
52
53
53
54
- def is_serializable (obj : object , * , _recursive_guard : frozenset [int ] = frozenset ()) -> bool :
55
- """Check if object is serializable or not."""
54
+ def is_serializable (obj : object , * , _seen : frozenset [int ] = frozenset ()) -> bool :
55
+ """Check if an object is serializable or not."""
56
56
if isinstance (obj , UNSERIALIZABLE_TYPES ):
57
57
return False
58
58
59
59
# use id() to handle un-hashable objects
60
- if id (obj ) in _recursive_guard :
60
+ if id (obj ) in _seen :
61
61
return True
62
62
63
63
if isinstance (obj , dict ):
64
- guard = _recursive_guard | {id (obj )}
65
- for key , value in obj .items ():
66
- if (
67
- not is_serializable (key , _recursive_guard = guard )
68
- or not is_serializable (value , _recursive_guard = guard )
69
- ):
70
- return False
64
+ seen = _seen | {id (obj )}
65
+ return all (
66
+ is_serializable (key , _seen = seen ) and is_serializable (value , _seen = seen )
67
+ for key , value in obj .items ()
68
+ )
71
69
elif isinstance (obj , (list , tuple , set , frozenset )):
72
- guard = _recursive_guard | {id (obj )}
73
- return all (is_serializable (item , _recursive_guard = guard ) for item in obj )
70
+ seen = _seen | {id (obj )}
71
+ return all (is_serializable (item , _seen = seen ) for item in obj )
74
72
75
73
# if an issue occurs for a non-serializable type, pickle will complain
76
- # since the object is likely coming from a third-party extension (we
77
- # natively expect 'simple' types and not weird ones)
74
+ # since the object is likely coming from a third-party extension
75
+ # (we natively expect 'simple' types and not weird ones)
78
76
return True
79
77
80
78
@@ -473,7 +471,8 @@ def __getstate__(self) -> dict:
473
471
# will always mark the config value as changed,
474
472
# and thus always invalidate the cache and perform a rebuild.
475
473
logger .warning (
476
- __ ('cannot cache unpickable configuration value: %r' ),
474
+ __ ('cannot cache unpickable configuration value: %r '
475
+ '(because it contains a function, class, or module object)' ),
477
476
name ,
478
477
type = 'config' ,
479
478
subtype = 'cache' ,
0 commit comments