From 3b9e09b527998437834054d624ba37cb4dca7798 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 20:01:08 -0700 Subject: [PATCH 01/12] Add examples for dict evaluation order --- Doc/whatsnew/3.8.rst | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 82da10cc3be86e..eccf966ddc05e0 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -468,8 +468,30 @@ Other Language Changes (Contributed by Mark Dickinson in :issue:`36027`.) -* When dictionary comprehensions are evaluated, the key is now evaluated before - the value, as proposed by :pep:`572`. +* Dict comprehensions have been synced-up with dict literals so that the + key is computed first and the value second:: + + >>> # Dict comprehension + >>> cast = {input('role? '): input('actor? ') for i in range(2)} + role? King Arthur + actor? Chapman + role? Black Knight + actor? Cleese + + >>> # Dict literal + >>> cast = {input('role? '): input('actor? ')} + role? Sir Robin + actor? Eric Idle + + The guaranteed execution order is helpful with assignment expressions + because variables assigned in the key expression will be available in + the value expression:: + + >>> names = ['Martin von Löwis', 'Łukasz Langa', 'Walter Dörwald'] + >>> {(n := normalize('NFC', name)).casefold() : n for name in names} + {'martin von löwis': 'Martin von Löwis', + 'łukasz langa': 'Łukasz Langa', + 'walter dörwald': 'Walter Dörwald'} New Modules From 4138137b24213074cf37f17396570b46f0ece54f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 20:34:35 -0700 Subject: [PATCH 02/12] Add test set up --- Doc/whatsnew/3.8.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index eccf966ddc05e0..58c405a5edc689 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -59,6 +59,7 @@ notable items not yet covered are: from datetime import date from math import cos, radians + from unicodedata import normalize import re import math From 6a25e5ace7fd247f9d2cdaf9582faf17d3cd70ef Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 21:05:14 -0700 Subject: [PATCH 03/12] Explain the replace() method for code objects. --- Doc/whatsnew/3.8.rst | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 58c405a5edc689..e1034568c6eac8 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -440,7 +440,25 @@ Other Language Changes and Windows use this to properly terminate scripts in interactive sessions. (Contributed by Google via Gregory P. Smith in :issue:`1054041`.) -* Added new ``replace()`` method to the code type (:class:`types.CodeType`). +* Some advanced styles of programming require updating the + :class:`types.CodeType` object for an existing function. Since code + objects are immutable, a new code object needs to be created, one + that is modeled on the existing code object. With 19 parameters, + this was somewhat tedious. Now, the new ``replace()`` method makes + it possible to create a clone with a few altered parameters. + + Here's an example that alters the :func:`statistics.mean` function to + prevent the *data* parameter from being used as a keyword argument::C + + >>> from statistics import mean + >>> mean(data=[10, 20, 90]) + 40 + >>> mean.__code__ = mean.__code__.replace(co_posonlyargcount = 1) + >>> mean(data=[10, 20, 90]) + Traceback (most recent call last): + ... + TypeError: mean() got some positional-only arguments passed as keyword arguments: 'data' + (Contributed by Victor Stinner in :issue:`37032`.) * For integers, the three-argument form of the :func:`pow` function now From f9f511eca94884a61564fdd5308aa756de6f60bb Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 22:30:12 -0700 Subject: [PATCH 04/12] Remove stray character --- Doc/whatsnew/3.8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index e1034568c6eac8..4320d7399d5057 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -448,7 +448,7 @@ Other Language Changes it possible to create a clone with a few altered parameters. Here's an example that alters the :func:`statistics.mean` function to - prevent the *data* parameter from being used as a keyword argument::C + prevent the *data* parameter from being used as a keyword argument:: >>> from statistics import mean >>> mean(data=[10, 20, 90]) From 609915396e0390b4e9eeef0289eba53b61ccc845 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 23:05:09 -0700 Subject: [PATCH 05/12] Edit the entry on the missing comma SyntaxWarning --- Doc/whatsnew/3.8.rst | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 4320d7399d5057..49b66f653c61cb 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -415,15 +415,11 @@ Other Language Changes and :keyword:`return` statements. (Contributed by David Cuthbert and Jordan Chapman in :issue:`32117`.) -* The compiler now produces a :exc:`SyntaxWarning` in some cases when a comma - is missed before tuple or list. For example:: - - data = [ - (1, 2, 3) # oops, missing comma! - (4, 5, 6) - ] - - (Contributed by Serhiy Storchaka in :issue:`15248`.) +* When a comma is missed in code such as ``[(10, 20) (30, 40)]``, the + compiler displays a :exc:`SyntaxWarning` with a helpful suggestion. + This improves on just having a :exc:`TypeError` indicating that the + first tuple was not callable. (Contributed by Serhiy Storchaka in + :issue:`15248`.) * Arithmetic operations between subclasses of :class:`datetime.date` or :class:`datetime.datetime` and :class:`datetime.timedelta` objects now return From 838d143612434237b19b21cb35e96dac4a5516e6 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 23:33:34 -0700 Subject: [PATCH 06/12] Syntax change for iterable unpacking in yield/return statements --- Doc/whatsnew/3.8.rst | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 49b66f653c61cb..2744e14f2fb09b 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -411,8 +411,19 @@ Other Language Changes never intended to permit more than a bare name on the left-hand side of a keyword argument assignment term. See :issue:`34641`. -* Iterable unpacking is now allowed without parentheses in :keyword:`yield` - and :keyword:`return` statements. +* Generalized iterable unpacking in :keyword:`yield` and + :keyword:`return` statements no longer requires enclosing parentheses. + This brings the *yield* and *return* syntax into better agreement with + normal assignment syntax:: + + >>> def parse(family): + lastname, *members = family.split() + return lastname.upper(), *members + + >>> parse('simpsons homer marge bart lisa sally') + ('SIMPSONS', 'homer', 'marge', 'bart', 'lisa', 'sally') + + (Contributed by David Cuthbert and Jordan Chapman in :issue:`32117`.) * When a comma is missed in code such as ``[(10, 20) (30, 40)]``, the From b325ff78daef2713b006fefec5799a468d2c364e Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 10 Aug 2019 23:35:15 -0700 Subject: [PATCH 07/12] We're no longer on Mercurial --- Doc/whatsnew/3.8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 2744e14f2fb09b..51630292dbd634 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -39,7 +39,7 @@ module. (Contributed by P.Y. Developer in :issue:`12345`.) - This saves the maintainer the effort of going through the Mercurial log + This saves the maintainer the effort of going through the Git log when researching a change. :Editor: Raymond Hettinger From 8a91d0fbfa5c330b83398cb738967022aeee0440 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 11 Aug 2019 00:36:51 -0700 Subject: [PATCH 08/12] Rationale for as_integer_ratio() on int and bool --- Doc/whatsnew/3.8.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 51630292dbd634..69ab7147da7cd2 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -384,9 +384,13 @@ Other Language Changes was lifted. (Contributed by Serhiy Storchaka in :issue:`32489`.) -* The :class:`int` type now has a new :meth:`~int.as_integer_ratio` method - compatible with the existing :meth:`float.as_integer_ratio` method. - (Contributed by Lisa Roach in :issue:`33073`.) +* The :class:`int` and :class:`bool` types now have an + :meth:`~int.as_integer_ratio` method like that found in + :class:`float` and :class:`decimal.Decimal`. This minor API + extension makes it possible to write ``numerator, denominator = + x.as_integer_ratio()`` and have it work across multiple numeric types. + In the future, :class:`fractions.Fraction` will likely also support + this method. (Contributed by Lisa Roach in :issue:`33073`.) * Constructors of :class:`int`, :class:`float` and :class:`complex` will now use the :meth:`~object.__index__` special method, if available and the From 2392249267ca5e138ca4004c3dcd72214e30d4c3 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 11 Aug 2019 14:34:41 -0700 Subject: [PATCH 09/12] Update for Fraction.as_integer_ratio() --- Doc/whatsnew/3.8.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 69ab7147da7cd2..0f6d7b1d6ae031 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -384,13 +384,13 @@ Other Language Changes was lifted. (Contributed by Serhiy Storchaka in :issue:`32489`.) -* The :class:`int` and :class:`bool` types now have an - :meth:`~int.as_integer_ratio` method like that found in - :class:`float` and :class:`decimal.Decimal`. This minor API - extension makes it possible to write ``numerator, denominator = +* The :class:`bool`, :class:`int`, and :class:`fractions.Fraction` types + now have an :meth:`~int.as_integer_ratio` method like that found in + :class:`float` and :class:`decimal.Decimal`. This minor API extension + makes it possible to write ``numerator, denominator = x.as_integer_ratio()`` and have it work across multiple numeric types. - In the future, :class:`fractions.Fraction` will likely also support - this method. (Contributed by Lisa Roach in :issue:`33073`.) + (Contributed by Lisa Roach in :issue:`33073` and Raymond Hettinger is + :issue:`37819`.) * Constructors of :class:`int`, :class:`float` and :class:`complex` will now use the :meth:`~object.__index__` special method, if available and the From 5290bfdcba4cb6f80c4c6a2ac603fb23dd717a5c Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 11 Aug 2019 16:05:26 -0700 Subject: [PATCH 10/12] Demonstrate the importlib.metadata package --- Doc/whatsnew/3.8.rst | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 0f6d7b1d6ae031..c230c97f812259 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -528,9 +528,25 @@ New Modules =========== * The new :mod:`importlib.metadata` module provides (provisional) support for - reading metadata from third-party packages. For example, you can extract an - installed package's version number, list of entry points, and more. See - :issue:`34632` for additional details. + reading metadata from third-party packages. For example, it can extract an + installed package's version number, list of entry points, and more:: + + >>> # Note following example requires that the popular "requests" + >>> # package has been installed. + >>> + >>> from importlib.metadata import version, requires, files + >>> version('requests') + '2.22.0' + >>> list(requires('requests')) + ['chardet (<3.1.0,>=3.0.2)'] + >>> list(files('requests'))[:5] + [PackagePath('requests-2.22.0.dist-info/INSTALLER'), + PackagePath('requests-2.22.0.dist-info/LICENSE'), + PackagePath('requests-2.22.0.dist-info/METADATA'), + PackagePath('requests-2.22.0.dist-info/RECORD'), + PackagePath('requests-2.22.0.dist-info/WHEEL')] + + (Contributed in :issue:`34632` by Barry Warsaw and Jason R. Coombs.) Improved Modules From fa7a9c95c07b7dc3693eed9c939f7c9d4b241993 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 11 Aug 2019 21:55:16 -0700 Subject: [PATCH 11/12] Fix typo --- Doc/whatsnew/3.8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index c230c97f812259..3e7d56d10d61c5 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -389,7 +389,7 @@ Other Language Changes :class:`float` and :class:`decimal.Decimal`. This minor API extension makes it possible to write ``numerator, denominator = x.as_integer_ratio()`` and have it work across multiple numeric types. - (Contributed by Lisa Roach in :issue:`33073` and Raymond Hettinger is + (Contributed by Lisa Roach in :issue:`33073` and Raymond Hettinger in :issue:`37819`.) * Constructors of :class:`int`, :class:`float` and :class:`complex` will now From 310ddfbc813b4333a4e7c394f0cc03dc9c4cb417 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 12 Aug 2019 09:03:10 -0700 Subject: [PATCH 12/12] PEP 8 nit --- Doc/whatsnew/3.8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 3e7d56d10d61c5..e8238251d6ea25 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -464,7 +464,7 @@ Other Language Changes >>> from statistics import mean >>> mean(data=[10, 20, 90]) 40 - >>> mean.__code__ = mean.__code__.replace(co_posonlyargcount = 1) + >>> mean.__code__ = mean.__code__.replace(co_posonlyargcount=1) >>> mean(data=[10, 20, 90]) Traceback (most recent call last): ...