Skip to content

Commit e678672

Browse files
CAM-Gerlachmiss-islington
authored andcommitted
pythongh-95913: Copyedit, link & format Typing Features section in 3.11 What's New (pythonGH-96097)
Co-authored-by: Ezio Melotti <[email protected]> Co-authored-by: Alex Waygood <[email protected]> (cherry picked from commit 558768f) Co-authored-by: C.A.M. Gerlach <[email protected]>
1 parent 3bf8e0f commit e678672

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,28 @@ Irit Katriel in :issue:`45607`.)
203203

204204

205205
.. _new-feat-related-type-hints-311:
206+
.. _whatsnew311-typing-features:
206207

207208
New Features Related to Type Hints
208209
==================================
209210

210211
This section covers major changes affecting :pep:`484` type hints and
211212
the :mod:`typing` module.
212213

214+
215+
.. _whatsnew311-pep646:
216+
213217
PEP 646: Variadic generics
214218
--------------------------
215219

216-
:pep:`484` introduced :data:`~typing.TypeVar`, enabling creation
217-
of generics parameterised with a single type. :pep:`646` introduces
220+
:pep:`484` previously introduced :data:`~typing.TypeVar`, enabling creation
221+
of generics parameterised with a single type. :pep:`646` adds
218222
:data:`~typing.TypeVarTuple`, enabling parameterisation
219223
with an *arbitrary* number of types. In other words,
220224
a :data:`~typing.TypeVarTuple` is a *variadic* type variable,
221-
enabling *variadic* generics. This enables a wide variety of use cases.
225+
enabling *variadic* generics.
226+
227+
This enables a wide variety of use cases.
222228
In particular, it allows the type of array-like structures
223229
in numerical computing libraries such as NumPy and TensorFlow to be
224230
parameterised with the array *shape*. Static type checkers will now
@@ -230,26 +236,30 @@ See :pep:`646` for more details.
230236
Serhiy Storchaka and Jelle Zijlstra. PEP written by Mark Mendoza, Matthew
231237
Rahtz, Pradeep Kumar Srinivasan, and Vincent Siles.)
232238

239+
240+
.. _whatsnew311-pep655:
241+
233242
PEP 655: Marking individual ``TypedDict`` items as required or not-required
234243
---------------------------------------------------------------------------
235244

236245
:data:`~typing.Required` and :data:`~typing.NotRequired` provide a
237246
straightforward way to mark whether individual items in a
238-
:data:`~typing.TypedDict` must be present. Previously this was only possible
247+
:class:`~typing.TypedDict` must be present. Previously, this was only possible
239248
using inheritance.
240249

241-
Fields are still required by default, unless the ``total=False``
242-
parameter is set.
243-
For example, the following specifies a dictionary with one required and
244-
one not-required key::
250+
All fields are still required by default,
251+
unless the *total* parameter is set to ``False``,
252+
in which case all fields are still not-required by default.
253+
For example, the following specifies a :class:`!TypedDict`
254+
with one required and one not-required key::
245255

246256
class Movie(TypedDict):
247257
title: str
248258
year: NotRequired[int]
249259

250-
m1: Movie = {"title": "Black Panther", "year": 2018} # ok
251-
m2: Movie = {"title": "Star Wars"} # ok (year is not required)
252-
m3: Movie = {"year": 2022} # error (missing required field title)
260+
m1: Movie = {"title": "Black Panther", "year": 2018} # OK
261+
m2: Movie = {"title": "Star Wars"} # OK (year is not required)
262+
m3: Movie = {"year": 2022} # ERROR (missing required field title)
253263

254264
The following definition is equivalent::
255265

@@ -262,15 +272,20 @@ See :pep:`655` for more details.
262272
(Contributed by David Foster and Jelle Zijlstra in :issue:`47087`. PEP
263273
written by David Foster.)
264274

275+
276+
.. _whatsnew311-pep673:
277+
265278
PEP 673: ``Self`` type
266279
----------------------
267280

268281
The new :data:`~typing.Self` annotation provides a simple and intuitive
269282
way to annotate methods that return an instance of their class. This
270-
behaves the same as the :data:`~typing.TypeVar`-based approach specified
271-
in :pep:`484` but is more concise and easier to follow.
283+
behaves the same as the :class:`~typing.TypeVar`-based approach
284+
:pep:`specified in PEP 484 <484#annotating-instance-and-class-methods>`,
285+
but is more concise and easier to follow.
272286

273-
Common use cases include alternative constructors provided as classmethods
287+
Common use cases include alternative constructors provided as
288+
:func:`classmethod <classmethod>`\s,
274289
and :meth:`~object.__enter__` methods that return ``self``::
275290

276291
class MyLock:
@@ -295,6 +310,9 @@ See :pep:`673` for more details.
295310
(Contributed by James Hilton-Balfe in :issue:`46534`. PEP written by
296311
Pradeep Kumar Srinivasan and James Hilton-Balfe.)
297312

313+
314+
.. _whatsnew311-pep675:
315+
298316
PEP 675: Arbitrary literal string type
299317
--------------------------------------
300318

@@ -329,14 +347,17 @@ See :pep:`675` for more details.
329347
(Contributed by Jelle Zijlstra in :issue:`47088`. PEP written by Pradeep
330348
Kumar Srinivasan and Graham Bleaney.)
331349

350+
351+
.. _whatsnew311-pep681:
352+
332353
PEP 681: Data Class Transforms
333354
------------------------------
334355

335356
:data:`~typing.dataclass_transform` may be used to
336357
decorate a class, metaclass, or a function that is itself a decorator.
337358
The presence of ``@dataclass_transform()`` tells a static type checker that the
338-
decorated object performs runtime "magic" that
339-
transforms a class, giving it :func:`dataclasses.dataclass`-like behaviors.
359+
decorated object performs runtime "magic" that transforms a class,
360+
giving it :func:`dataclass <dataclasses.dataclass>`-like behaviors.
340361

341362
For example::
342363

@@ -348,26 +369,32 @@ For example::
348369
cls.__ne__ = ...
349370
return cls
350371

351-
# The create_model decorator can now be used to create new model
352-
# classes, like this:
372+
# The create_model decorator can now be used to create new model classes:
353373
@create_model
354374
class CustomerModel:
355375
id: int
356376
name: str
357377

358-
c = CustomerModel(id=327, name="John Smith")
378+
c = CustomerModel(id=327, name="Eric Idle")
359379

360380
See :pep:`681` for more details.
361381

362382
(Contributed by Jelle Zijlstra in :gh:`91860`. PEP written by
363383
Erik De Bonte and Eric Traut.)
364384

365-
PEP 563 May Not Be the Future
385+
386+
.. _whatsnew311-pep563-deferred:
387+
388+
PEP 563 may not be the future
366389
-----------------------------
367390

368-
* :pep:`563` Postponed Evaluation of Annotations, ``__future__.annotations``
369-
that was planned for this release has been indefinitely postponed.
370-
See `this message <https://mail.python.org/archives/list/[email protected]/message/VIZEBX5EYMSYIJNDBF6DMUMZOCWHARSO/>`_ for more information.
391+
:pep:`563` Postponed Evaluation of Annotations
392+
(the ``from __future__ import annotations`` :ref:`future statement <future>`)
393+
that was originally planned for release in Python 3.10
394+
has been put on hold indefinitely.
395+
See `this message from the Steering Council <https://mail.python.org/archives/list/[email protected]/message/VIZEBX5EYMSYIJNDBF6DMUMZOCWHARSO/>`__
396+
for more information.
397+
371398

372399
Windows py.exe launcher improvements
373400
------------------------------------

0 commit comments

Comments
 (0)