Skip to content

Commit c7ac4e7

Browse files
committed
Merge remote-tracking branch 'origin/main' into gh-158195-unpin-type-lock
2 parents 2aca6d5 + 796b26b commit c7ac4e7

80 files changed

Lines changed: 1609 additions & 461 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Doc/deprecations/pending-removal-in-3.21.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Pending removal in Python 3.21
1818
* The ``dims`` property of ``ast.Tuple`` will be removed in Python 3.21. Use
1919
the ``ast.Tuple.elts`` property instead.
2020

21+
* :mod:`functools`:
22+
23+
* Using a return annotation to infer the dispatch type for
24+
:func:`functools.singledispatch` or
25+
:class:`functools.singledispatchmethod` is deprecated since Python 3.16
26+
and will raise :exc:`TypeError` in Python 3.21. Annotate the dispatch
27+
parameter or pass the dispatch type explicitly instead.
28+
(Contributed by Bartosz Sławecki in :gh:`143465`.)
29+
2130
* :mod:`struct`:
2231

2332
* Soft-deprecated since Python 3.15, using ``'F'`` and ``'D'`` type codes are now

‎Doc/library/argparse.rst‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,19 @@ Argument groups
20722072
is now deprecated.
20732073

20742074

2075+
.. class:: ArgumentGroup
2076+
2077+
The type of objects returned by :meth:`ArgumentParser.add_argument_group`.
2078+
Its :meth:`~ArgumentParser.add_argument` and
2079+
:meth:`~ArgumentParser.add_mutually_exclusive_group` methods work as on
2080+
:class:`ArgumentParser`. Use :meth:`~ArgumentParser.add_argument_group`
2081+
to create instances. Only the methods documented here are part of the
2082+
public API; other attributes and methods are implementation details.
2083+
2084+
.. versionadded:: next
2085+
The private ``_ArgumentGroup`` name remains as an alias.
2086+
2087+
20752088
Mutual exclusion
20762089
^^^^^^^^^^^^^^^^
20772090

@@ -2135,6 +2148,20 @@ Mutual exclusion
21352148
exposed through inheritance.
21362149

21372150

2151+
.. class:: MutuallyExclusiveGroup
2152+
2153+
The type of objects returned by
2154+
:meth:`ArgumentParser.add_mutually_exclusive_group`.
2155+
Its :meth:`~ArgumentParser.add_argument` method works as on
2156+
:class:`ArgumentParser`. Use
2157+
:meth:`~ArgumentParser.add_mutually_exclusive_group` to create instances.
2158+
Only the ``add_argument`` method documented here is part of the public API;
2159+
other attributes and methods are implementation details.
2160+
2161+
.. versionadded:: next
2162+
The private ``_MutuallyExclusiveGroup`` name remains as an alias.
2163+
2164+
21382165
Parser defaults
21392166
^^^^^^^^^^^^^^^
21402167

‎Doc/library/ctypes.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ way is to instantiate :py:class:`CDLL` or one of its subclasses:
15631563
On non-Windows systems, *name* can be ``None``. In this case,
15641564
:c:func:`!dlopen` is called with ``NULL``, which opens the main program
15651565
as a "library".
1566-
(Some systems do the same is *name* is empty; ``None``/``NULL`` is more
1566+
(Some systems do the same if *name* is empty; ``None``/``NULL`` is more
15671567
portable.)
15681568

15691569
.. admonition:: CPython implementation detail

‎Doc/library/functools.rst‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -506,24 +506,37 @@ The :mod:`!functools` module defines the following functions:
506506
:no-typesetting:
507507

508508
To add overloaded implementations to the function, use the :func:`!register`
509-
attribute of the generic function, which can be used as a decorator. For
510-
functions annotated with types, the decorator will infer the type of the
511-
first argument automatically::
509+
attribute of the generic function, which can be used as a decorator.
510+
Pass the dispatch type to the decorator::
512511

513-
>>> @fun.register
514-
... def _(arg: int, verbose=False):
512+
>>> @fun.register(int)
513+
... def _(arg, verbose=False):
515514
... if verbose:
516515
... print("Strength in numbers, eh?", end=" ")
517516
... print(arg)
518517
...
519-
>>> @fun.register
520-
... def _(arg: list, verbose=False):
518+
>>> @fun.register(list)
519+
... def _(arg, verbose=False):
521520
... if verbose:
522521
... print("Enumerate this:")
523522
... for i, elem in enumerate(arg):
524523
... print(i, elem)
525524

526-
:class:`typing.Union` can also be used::
525+
As a shortcut, for functions annotated with types, the decorator can infer
526+
the dispatch type without an explicit type argument::
527+
528+
>>> @fun.register
529+
... def _(arg: int, verbose=False):
530+
... if verbose:
531+
... print("Strength in numbers, eh?", end=" ")
532+
... print(arg)
533+
...
534+
535+
This shortcut uses the first annotation, even if it belongs to another
536+
parameter or the return value. Annotate the dispatch parameter, or pass the
537+
dispatch type explicitly if its annotation is not first.
538+
539+
:class:`typing.Union` can also be used with this shortcut::
527540

528541
>>> @fun.register
529542
... def _(arg: int | float, verbose=False):
@@ -540,16 +553,6 @@ The :mod:`!functools` module defines the following functions:
540553
... print(i, elem)
541554
...
542555

543-
For code which doesn't use type annotations, the appropriate type
544-
argument can be passed explicitly to the decorator itself::
545-
546-
>>> @fun.register(complex)
547-
... def _(arg, verbose=False):
548-
... if verbose:
549-
... print("Better than complicated.", end=" ")
550-
... print(arg.real, arg.imag)
551-
...
552-
553556
For code that dispatches on a collections type (e.g., ``list``), but wants
554557
to typehint the items of the collection (e.g., ``list[int]``), the
555558
dispatch type should be passed explicitly to the decorator itself with the
@@ -662,6 +665,12 @@ The :mod:`!functools` module defines the following functions:
662665
The :func:`~singledispatch.register` attribute now supports
663666
:class:`typing.Union` as a type annotation.
664667

668+
.. deprecated:: 3.16
669+
Using a return annotation to infer the dispatch type is deprecated and
670+
will raise :exc:`TypeError` in Python 3.21. Annotate the dispatch
671+
parameter or pass the dispatch type explicitly to :func:`!register`
672+
instead.
673+
665674

666675
.. class:: singledispatchmethod(func)
667676

@@ -686,6 +695,20 @@ The :mod:`!functools` module defines the following functions:
686695
def _(self, arg: bool):
687696
return not arg
688697

698+
When *self* or *cls* is annotated, pass the dispatch type explicitly rather
699+
than relying on annotation inference::
700+
701+
from typing import Self
702+
703+
class Negator:
704+
@singledispatchmethod
705+
def neg(self, arg):
706+
raise NotImplementedError("Cannot negate a")
707+
708+
@neg.register(int)
709+
def _(self: Self, arg: int):
710+
return -arg
711+
689712
``@singledispatchmethod`` supports nesting with other decorators such as
690713
:deco:`classmethod`. Note that to allow for
691714
``dispatcher.register``, ``singledispatchmethod`` must be the *outer most*

‎Doc/library/pickle.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,11 @@ The following types can be pickled:
517517
* classes accessible from the top level of a module;
518518

519519
* instances of such classes for which the result of calling :meth:`~object.__getstate__`
520-
is picklable (see section :ref:`pickle-inst` for details).
520+
is picklable (see section :ref:`pickle-inst` for details);
521+
522+
* weak references and the weak containers from the :mod:`weakref` module,
523+
provided that the weakly referenced objects are strongly referenced
524+
elsewhere in the same pickle (see :class:`weakref.ref`).
521525

522526
Attempts to pickle unpicklable objects will raise the :exc:`PicklingError`
523527
exception; when this happens, an unspecified number of bytes may have already

‎Doc/library/weakref.rst‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
210231
exposes the internal references directly. The references are not guaranteed to
211232
be "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.
231259
These 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*

‎Doc/library/zlib.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,14 @@ the following constants:
487487

488488
The version string of the zlib library actually loaded by the interpreter.
489489

490+
Prefer :const:`!zlib_version` over its :const:`!ZLIB_RUNTIME_VERSION` alias.
491+
490492
.. versionadded:: 3.3
491493
The :const:`!ZLIB_RUNTIME_VERSION` constant.
492494

493495
.. versionadded:: 3.16
494-
The :const:`!zlib_version` alias.
496+
The :const:`!zlib_version` constant; :const:`!ZLIB_RUNTIME_VERSION` is now
497+
an alias for it.
495498

496499

497500
.. data:: ZLIB_VERSION_INFO

‎Doc/whatsnew/3.16.rst‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ New modules
126126
Improved modules
127127
================
128128

129+
argparse
130+
--------
131+
132+
* :class:`~argparse.ArgumentGroup` and
133+
:class:`~argparse.MutuallyExclusiveGroup`, the types returned by
134+
:meth:`~argparse.ArgumentParser.add_argument_group` and
135+
:meth:`~argparse.ArgumentParser.add_mutually_exclusive_group`, are now
136+
public. The ``_``-prefixed names remain as aliases.
137+
(Contributed by Bernát Gábor in :gh:`144812`.)
138+
129139
asyncio
130140
-------
131141

@@ -586,6 +596,17 @@ pydoc
586596
(Contributed by Serhiy Storchaka in :gh:`153906`.)
587597

588598

599+
pickle
600+
------
601+
602+
* Weak references (:class:`weakref.ref`), :class:`weakref.WeakMethod`,
603+
:class:`weakref.WeakSet`, :class:`weakref.WeakValueDictionary` and
604+
:class:`weakref.WeakKeyDictionary` can now be pickled if the weakly
605+
referenced objects are strongly referenced elsewhere in the same pickle;
606+
otherwise pickling raises :exc:`pickle.PicklingError`.
607+
:class:`!WeakMethod` and :class:`!WeakSet` can now also be copied.
608+
(Contributed by Serhiy Storchaka in :gh:`74876`.)
609+
589610
re
590611
--
591612

@@ -1068,6 +1089,16 @@ New deprecations
10681089
3.9, now issues a deprecation warning on use. This property is slated for
10691090
removal in 3.21. Use ``ast.Tuple.elts`` instead.
10701091

1092+
* :mod:`functools`:
1093+
1094+
* Using a return annotation to infer the dispatch type for
1095+
:func:`functools.singledispatch` or
1096+
:class:`functools.singledispatchmethod` now emits a
1097+
:exc:`DeprecationWarning` and will raise :exc:`TypeError` in Python 3.21.
1098+
Annotate the dispatch parameter or pass the dispatch type explicitly
1099+
instead.
1100+
(Contributed by Bartosz Sławecki in :gh:`143465`.)
1101+
10711102
* :mod:`struct`:
10721103

10731104
* The ``'F'`` and ``'D'`` type codes, soft-deprecated since Python 3.15,

0 commit comments

Comments
 (0)