From 40efa4e7ed2e3508f30a59129f2cae8d7287bc3f Mon Sep 17 00:00:00 2001 From: Pedro Fonini Date: Fri, 8 Nov 2024 10:58:30 -0300 Subject: [PATCH 1/5] Change "bound type var" to "bounded" in docs --- Doc/library/typing.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index cd8b90854b0e94..4ba93ddcad7fb5 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1726,10 +1726,10 @@ without the dedicated syntax, as documented below. class Sequence[T]: # T is a TypeVar ... - This syntax can also be used to create bound and constrained type + This syntax can also be used to create bounded and constrained type variables:: - class StrSequence[S: str]: # S is a TypeVar bound to str + class StrSequence[S: str]: # S is a TypeVar bounded by str ... @@ -1763,8 +1763,8 @@ without the dedicated syntax, as documented below. """Add two strings or bytes objects together.""" return x + y - Note that type variables can be *bound*, *constrained*, or neither, but - cannot be both bound *and* constrained. + Note that type variables can be *bounded*, *constrained*, or neither, but + cannot be both bounded *and* constrained. The variance of type variables is inferred by type checkers when they are created through the :ref:`type parameter syntax ` or when @@ -1774,8 +1774,8 @@ without the dedicated syntax, as documented below. By default, manually created type variables are invariant. See :pep:`484` and :pep:`695` for more details. - Bound type variables and constrained type variables have different - semantics in several important ways. Using a *bound* type variable means + Bounded type variables and constrained type variables have different + semantics in several important ways. Using a *bounded* type variable means that the ``TypeVar`` will be solved using the most specific type possible:: x = print_capitalized('a string') @@ -1789,7 +1789,7 @@ without the dedicated syntax, as documented below. z = print_capitalized(45) # error: int is not a subtype of str - Type variables can be bound to concrete types, abstract types (ABCs or + Type variables can be bounded by concrete types, abstract types (ABCs or protocols), and even unions of types:: # Can be anything with an __abs__ method From 0297395a3668abaf1d1454b472d471ddf7420fc8 Mon Sep 17 00:00:00 2001 From: Pedro Fonini Date: Fri, 8 Nov 2024 12:50:33 -0300 Subject: [PATCH 2/5] Rewording suggestions --- Doc/library/typing.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 4ba93ddcad7fb5..97ed7642b1061f 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1729,7 +1729,7 @@ without the dedicated syntax, as documented below. This syntax can also be used to create bounded and constrained type variables:: - class StrSequence[S: str]: # S is a TypeVar bounded by str + class StrSequence[S: str]: # S is a TypeVar with a `str` upper bound ... @@ -1789,8 +1789,8 @@ without the dedicated syntax, as documented below. z = print_capitalized(45) # error: int is not a subtype of str - Type variables can be bounded by concrete types, abstract types (ABCs or - protocols), and even unions of types:: + The upper bound of a type variable can be a concrete type, abstract type + (ABC or Protocol), or even a union of types:: # Can be anything with an __abs__ method def print_abs[T: SupportsAbs](arg: T) -> None: From fa0e3bd72406181432dde63105501c69dc26764a Mon Sep 17 00:00:00 2001 From: Pedro Fonini Date: Fri, 8 Nov 2024 12:54:00 -0300 Subject: [PATCH 3/5] Change "bound" as a noun to "upper bound" --- Doc/library/typing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 97ed7642b1061f..b3c0dbd7593adb 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1834,7 +1834,7 @@ without the dedicated syntax, as documented below. .. attribute:: __bound__ - The bound of the type variable, if any. + The upper bound of the type variable, if any. .. versionchanged:: 3.12 @@ -2100,7 +2100,7 @@ without the dedicated syntax, as documented below. return x + y Without ``ParamSpec``, the simplest way to annotate this previously was to - use a :class:`TypeVar` with bound ``Callable[..., Any]``. However this + use a :class:`TypeVar` with upper bound ``Callable[..., Any]``. However this causes two problems: 1. The type checker can't type check the ``inner`` function because From 673a71b2a2466eb7b9fc03a1dad16907b1da2bd9 Mon Sep 17 00:00:00 2001 From: Pedro Fonini Date: Fri, 8 Nov 2024 15:20:18 -0300 Subject: [PATCH 4/5] Note that "bounded" and "upper bound" refer to the same thing --- Doc/library/typing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index b3c0dbd7593adb..9fd29003e6a3ab 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1729,8 +1729,8 @@ without the dedicated syntax, as documented below. This syntax can also be used to create bounded and constrained type variables:: - class StrSequence[S: str]: # S is a TypeVar with a `str` upper bound - ... + class StrSequence[S: str]: # S is a TypeVar with a `str` upper bound; + ... # We can say that S is "bounded by `str`" class StrOrBytesSequence[A: (str, bytes)]: # A is a TypeVar constrained to str or bytes From daf3fe17a3a58b00c493dd385f87af6212c042cc Mon Sep 17 00:00:00 2001 From: Pedro Fonini Date: Sun, 10 Nov 2024 14:22:13 -0300 Subject: [PATCH 5/5] Typo Co-authored-by: Jelle Zijlstra --- Doc/library/typing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 9fd29003e6a3ab..0fee782121b0af 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1730,7 +1730,7 @@ without the dedicated syntax, as documented below. variables:: class StrSequence[S: str]: # S is a TypeVar with a `str` upper bound; - ... # We can say that S is "bounded by `str`" + ... # we can say that S is "bounded by `str`" class StrOrBytesSequence[A: (str, bytes)]: # A is a TypeVar constrained to str or bytes