@@ -126,6 +126,17 @@ See :ref:`__slots__ documentation <slots>` for details.
126126 Weak references are :ref: `generic <generics >` over the type of the object they
127127 reference.
128128
129+ A weak reference can be pickled
130+ if the referent is strongly referenced elsewhere in the same pickle;
131+ otherwise pickling raises :exc: `pickle.PicklingError `.
132+ The unpickled weak reference refers to the unpickled referent.
133+ A reference to a dead object unpickles as a reference to a dead object.
134+ The *callback * is not preserved.
135+
136+ Weak references are atomic for the :mod: `copy ` module:
137+ :func: `copy.copy ` and :func: `copy.deepcopy ` return
138+ the original weak reference without copying the referent.
139+
129140 .. attribute :: __callback__
130141
131142 This read-only attribute returns the callback currently associated to the
@@ -138,6 +149,9 @@ See :ref:`__slots__ documentation <slots>` for details.
138149 .. versionchanged :: 3.16
139150 Raise :exc: `!TypeError ` if *callback * is not callable or ``None ``.
140151
152+ .. versionchanged :: next
153+ Weak references can now be pickled.
154+
141155
142156.. function :: proxy(object[, callback])
143157
@@ -203,9 +217,16 @@ See :ref:`__slots__ documentation <slots>` for details.
203217 >>> d[k2] = 2 # d = {k2: 2}
204218 >>> del k1 # d = {k2: 2}
205219
220+ A :class: `!WeakKeyDictionary ` can be copied,
221+ and pickled if the keys are strongly referenced
222+ elsewhere in the same pickle (see :class: `ref `).
223+
206224 .. versionchanged :: 3.9
207225 Added support for ``| `` and ``|= `` operators, as specified in :pep: `584 `.
208226
227+ .. versionchanged :: next
228+ :class: `!WeakKeyDictionary ` can now be pickled.
229+
209230:class: `WeakKeyDictionary ` objects have an additional method that
210231exposes the internal references directly. The references are not guaranteed to
211232be "live" at the time they are used, so the result of calling the references
@@ -224,9 +245,16 @@ than needed.
224245 Mapping class that references values weakly. Entries in the dictionary will be
225246 discarded when no strong reference to the value exists any more.
226247
248+ A :class: `!WeakValueDictionary ` can be copied,
249+ and pickled if the values are strongly referenced
250+ elsewhere in the same pickle (see :class: `ref `).
251+
227252 .. versionchanged :: 3.9
228253 Added support for ``| `` and ``|= `` operators, as specified in :pep: `584 `.
229254
255+ .. versionchanged :: next
256+ :class: `!WeakValueDictionary ` can now be pickled.
257+
230258:class: `WeakValueDictionary ` objects have the following additional methods.
231259These have the same issues as the :meth: `WeakKeyDictionary.keyrefs ` method.
232260
@@ -246,6 +274,14 @@ These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method.
246274 Set class that keeps weak references to its elements. An element will be
247275 discarded when no strong reference to it exists any more.
248276
277+ A :class: `!WeakSet ` can be pickled if the elements are strongly
278+ referenced elsewhere in the same pickle (see :class: `ref `).
279+ A copy or a deep copy shares the elements with the original;
280+ user-defined attributes are copied or deep-copied.
281+
282+ .. versionchanged :: next
283+ :class: `!WeakSet ` can now be pickled and copied.
284+
249285
250286.. class :: WeakMethod(method[, callback])
251287
@@ -275,8 +311,16 @@ These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method.
275311
276312 *callback * is the same as the parameter of the same name to the :func: `ref ` function.
277313
314+ A :class: `!WeakMethod ` can be copied,
315+ and pickled if the object the method is bound to is strongly referenced
316+ elsewhere in the same pickle (see :class: `ref `).
317+ A copy or a deep copy is an equal weak method to the same object.
318+
278319 .. versionadded :: 3.4
279320
321+ .. versionchanged :: next
322+ :class: `!WeakMethod ` can now be pickled and copied.
323+
280324.. class :: finalize(obj, func, /, *args, **kwargs)
281325
282326 Return a callable finalizer object which will be called when *obj *
0 commit comments