Skip to content

Commit d2a0345

Browse files
committed
Merge branch 'master' into deprecate-struct-init-2/78724
2 parents fff147f + 548526b commit d2a0345

29 files changed

+1243
-989
lines changed

Doc/library/array.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
--------------
1010

1111
This module defines an object type which can compactly represent an array of
12-
basic values: characters, integers, floating-point numbers. Arrays are sequence
12+
basic values: characters, integers, floating-point numbers. Arrays are mutable :term:`sequence`
1313
types and behave very much like lists, except that the type of objects stored in
1414
them is constrained. The type is specified at object creation time by using a
1515
:dfn:`type code`, which is a single character. The following type codes are
@@ -93,7 +93,7 @@ The module defines the following type:
9393
otherwise, the initializer's iterator is passed to the :meth:`extend` method
9494
to add initial items to the array.
9595

96-
Array objects support the ordinary sequence operations of indexing, slicing,
96+
Array objects support the ordinary :ref:`mutable <typesseq-mutable>` :term:`sequence` operations of indexing, slicing,
9797
concatenation, and multiplication. When using slice assignment, the assigned
9898
value must be an array object with the same type code; in all other cases,
9999
:exc:`TypeError` is raised. Array objects also implement the buffer interface,

Doc/library/itertools.rst

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Iterator Arguments Results
4747
Iterator Arguments Results Example
4848
============================ ============================ ================================================= =============================================================
4949
:func:`accumulate` p [,func] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) → 1 3 6 10 15``
50-
:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=2) → AB CD EF G``
50+
:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=3) → ABC DEF G``
5151
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') → A B C D E F``
5252
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F``
5353
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F``
@@ -181,7 +181,7 @@ loops that truncate the stream.
181181
Roughly equivalent to::
182182

183183
def batched(iterable, n, *, strict=False):
184-
# batched('ABCDEFG', 2) → AB CD EF G
184+
# batched('ABCDEFG', 3) → ABC DEF G
185185
if n < 1:
186186
raise ValueError('n must be at least one')
187187
iterator = iter(iterable)
@@ -842,7 +842,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
842842
from contextlib import suppress
843843
from functools import reduce
844844
from math import comb, prod, sumprod, isqrt
845-
from operator import itemgetter, getitem, mul, neg
845+
from operator import is_not, itemgetter, getitem, mul, neg
846846

847847
def take(n, iterable):
848848
"Return first n items of the iterable as a list."
@@ -978,6 +978,16 @@ and :term:`generators <generator>` which incur interpreter overhead.
978978
slices = starmap(slice, combinations(range(len(seq) + 1), 2))
979979
return map(getitem, repeat(seq), slices)
980980

981+
def derangements(iterable, r=None):
982+
"Produce r length permutations without fixed points."
983+
# derangements('ABCD') → BADC BCDA BDAC CADB CDAB CDBA DABC DCAB DCBA
984+
# Algorithm credited to Stefan Pochmann
985+
seq = tuple(iterable)
986+
pos = tuple(range(len(seq)))
987+
have_moved = map(map, repeat(is_not), repeat(pos), permutations(pos, r=r))
988+
valid_derangements = map(all, have_moved)
989+
return compress(permutations(seq, r=r), valid_derangements)
990+
981991
def iter_index(iterable, value, start=0, stop=None):
982992
"Return indices where a value occurs in a sequence or iterable."
983993
# iter_index('AABCADEAF', 'A') → 0 1 4 7
@@ -1663,6 +1673,36 @@ The following recipes have a more mathematical flavor:
16631673
['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']
16641674

16651675

1676+
>>> ' '.join(map(''.join, derangements('ABCD')))
1677+
'BADC BCDA BDAC CADB CDAB CDBA DABC DCAB DCBA'
1678+
>>> ' '.join(map(''.join, derangements('ABCD', 3)))
1679+
'BAD BCA BCD BDA CAB CAD CDA CDB DAB DCA DCB'
1680+
>>> ' '.join(map(''.join, derangements('ABCD', 2)))
1681+
'BA BC BD CA CD DA DC'
1682+
>>> ' '.join(map(''.join, derangements('ABCD', 1)))
1683+
'B C D'
1684+
>>> ' '.join(map(''.join, derangements('ABCD', 0)))
1685+
''
1686+
>>> # Compare number of derangements to https://oeis.org/A000166
1687+
>>> [len(list(derangements(range(n)))) for n in range(10)]
1688+
[1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496]
1689+
>>> # Verify that identical objects are treated as unique by position
1690+
>>> identical = 'X'
1691+
>>> distinct = 'x'
1692+
>>> seq1 = ('A', identical, 'B', identical)
1693+
>>> result1 = ' '.join(map(''.join, derangements(seq1)))
1694+
>>> result1
1695+
'XAXB XBXA XXAB BAXX BXAX BXXA XAXB XBAX XBXA'
1696+
>>> seq2 = ('A', identical, 'B', distinct)
1697+
>>> result2 = ' '.join(map(''.join, derangements(seq2)))
1698+
>>> result2
1699+
'XAxB XBxA XxAB BAxX BxAX BxXA xAXB xBAX xBXA'
1700+
>>> result1 == result2
1701+
False
1702+
>>> result1.casefold() == result2.casefold()
1703+
True
1704+
1705+
16661706
>>> list(powerset([1,2,3]))
16671707
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
16681708
>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))

Doc/library/pathlib.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,10 @@ Reading directories
13311331
PosixPath('setup.py'),
13321332
PosixPath('test_pathlib.py')]
13331333

1334+
.. note::
1335+
The paths are returned in no particular order.
1336+
If you need a specific order, sort the results.
1337+
13341338
.. seealso::
13351339
:ref:`pathlib-pattern-language` documentation.
13361340

@@ -1365,6 +1369,10 @@ Reading directories
13651369
Glob the given relative *pattern* recursively. This is like calling
13661370
:func:`Path.glob` with "``**/``" added in front of the *pattern*.
13671371

1372+
.. note::
1373+
The paths are returned in no particular order.
1374+
If you need a specific order, sort the results.
1375+
13681376
.. seealso::
13691377
:ref:`pathlib-pattern-language` and :meth:`Path.glob` documentation.
13701378

Doc/library/stdtypes.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,11 +1093,14 @@ Notes:
10931093
still ``0``.
10941094

10951095
(4)
1096-
The slice of *s* from *i* to *j* is defined as the sequence of items with index
1097-
*k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
1098-
``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
1099-
``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
1100-
empty.
1096+
The slice of *s* from *i* to *j* is defined as the sequence of items with
1097+
index *k* such that ``i <= k < j``.
1098+
1099+
* If *i* is omitted or ``None``, use ``0``.
1100+
* If *j* is omitted or ``None``, use ``len(s)``.
1101+
* If *i* or *j* is less than ``-len(s)``, use ``0``.
1102+
* If *i* or *j* is greater than ``len(s)``, use ``len(s)``.
1103+
* If *i* is greater than or equal to *j*, the slice is empty.
11011104

11021105
(5)
11031106
The slice of *s* from *i* to *j* with step *k* is defined as the sequence of

Doc/library/typing.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ These functions and classes should not be used directly as annotations.
23692369
Their intended purpose is to be building blocks for creating and declaring
23702370
types.
23712371

2372-
.. class:: NamedTuple
2372+
.. function:: NamedTuple
23732373

23742374
Typed version of :func:`collections.namedtuple`.
23752375

@@ -2442,6 +2442,10 @@ types.
24422442
Removed the ``_field_types`` attribute in favor of the more
24432443
standard ``__annotations__`` attribute which has the same information.
24442444

2445+
.. versionchanged:: 3.9
2446+
``NamedTuple`` is now a function rather than a class.
2447+
It can still be used as a class base, as described above.
2448+
24452449
.. versionchanged:: 3.11
24462450
Added support for generic namedtuples.
24472451

@@ -2585,10 +2589,10 @@ types.
25852589
for more details.
25862590

25872591

2588-
.. class:: TypedDict(dict)
2592+
.. function:: TypedDict
25892593

25902594
Special construct to add type hints to a dictionary.
2591-
At runtime it is a plain :class:`dict`.
2595+
At runtime ":class:`!TypedDict` instances" are simply :class:`dicts <dict>`.
25922596

25932597
``TypedDict`` declares a dictionary type that expects all of its
25942598
instances to have a certain set of keys, where each key is
@@ -2811,6 +2815,10 @@ types.
28112815

28122816
.. versionadded:: 3.8
28132817

2818+
.. versionchanged:: 3.9
2819+
``TypedDict`` is now a function rather than a class.
2820+
It can still be used as a class base, as described above.
2821+
28142822
.. versionchanged:: 3.11
28152823
Added support for marking individual keys as :data:`Required` or :data:`NotRequired`.
28162824
See :pep:`655`.

Doc/reference/datamodel.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,12 @@ Slice objects are used to represent slices for
18191819
:meth:`~object.__getitem__`
18201820
methods. They are also created by the built-in :func:`slice` function.
18211821

1822+
.. versionadded:: 3.15
1823+
1824+
The :func:`slice` type now supports :ref:`subscription <subscriptions>`. For
1825+
example, ``slice[float]`` may be used in type annotations to indicate a slice
1826+
containing :type:`float` objects.
1827+
18221828
.. index::
18231829
single: start (slice object attribute)
18241830
single: stop (slice object attribute)

Doc/whatsnew/3.13.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ Limited C API Changes
23402340
* :c:func:`PySys_AuditTuple`
23412341
* :c:func:`PyType_GetModuleByDef`
23422342

2343-
(Contributed by Victor Stinner in :gh:`85283`, :gh:`85283`, and :gh:`116936`.)
2343+
(Contributed by Victor Stinner in :gh:`85283` and :gh:`116936`.)
23442344

23452345
* Python built with :option:`--with-trace-refs` (tracing references)
23462346
now supports the :ref:`Limited API <limited-c-api>`.

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ Other language changes
402402
:class:`tuple` (including classes created by :func:`collections.namedtuple`).
403403
(Contributed by Serhiy Storchaka in :gh:`41779`.)
404404

405+
* The :class:`slice` type now supports subscription,
406+
making it a :term:`generic type`.
407+
(Contributed by James Hilton-Balfe in :gh:`128335`.)
405408

406409
New modules
407410
===========
@@ -889,14 +892,14 @@ Upgraded JIT compiler
889892

890893
Results from the `pyperformance <https://github.com/python/pyperformance>`__
891894
benchmark suite report
892-
`3-4% <https://github.com/facebookexperimental/free-threading-benchmarking/blob/main/results/bm-20251214-3.15.0a2%2B-6cddf04-JIT/bm-20251214-vultr-x86_64-python-6cddf04344a1e8ca9df5-3.15.0a2%2B-6cddf04-vs-base.svg>`__
895+
`4-5% <https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20260110-3.15.0a3%2B-aa8578d-JIT/bm-20260110-vultr-x86_64-python-aa8578dc54df2af9daa3-3.15.0a3%2B-aa8578d-vs-base.svg>`__
893896
geometric mean performance improvement for the JIT over the standard CPython
894897
interpreter built with all optimizations enabled on x86-64 Linux. On AArch64
895898
macOS, the JIT has a
896-
`7-8% <https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20260103-3.15.0a3%2B-9609574-JIT/bm-20260103-macm4pro-arm64-python-9609574e7fd36edfaa8b-3.15.0a3%2B-9609574-vs-base.svg>`__
899+
`7-8% <https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20260110-3.15.0a3%2B-aa8578d-JIT/bm-20260110-macm4pro-arm64-python-aa8578dc54df2af9daa3-3.15.0a3%2B-aa8578d-vs-base.svg>`__
897900
speedup over the :ref:`tail calling interpreter <whatsnew314-tail-call-interpreter>`
898901
with all optimizations enabled. The speedups for JIT
899-
builds versus no JIT builds range from roughly 20% slowdown to over
902+
builds versus no JIT builds range from roughly 15% slowdown to over
900903
100% speedup (ignoring the ``unpack_sequence`` microbenchmark) on
901904
x86-64 Linux and AArch64 macOS systems.
902905

Include/internal/pycore_opcode_metadata.h

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)