Skip to content

Commit 717204f

Browse files
andresdelfinoilevkivskyi
authored andcommitted
[3.6] bpo-32769: A new take on annotations/type hinting glossary entries (GH-6829) (GH-7128)
* [3.6] bpo-32769: A new take on annotations/type hinting glossary entries (GH-6829). (cherry picked from commit 6e33f81) Co-authored-by: Andrés Delfino <[email protected]> * Typo fix spotted by Guido
1 parent 9ba3be4 commit 717204f

File tree

1 file changed

+84
-16
lines changed

1 file changed

+84
-16
lines changed

Doc/glossary.rst

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ Glossary
3939
and loaders (in the :mod:`importlib.abc` module). You can create your own
4040
ABCs with the :mod:`abc` module.
4141

42+
annotation
43+
A label associated with a variable, a class
44+
attribute or a function parameter or return value,
45+
used by convention as a :term:`type hint`.
46+
47+
Annotations of local variables cannot be accessed at runtime, but
48+
annotations of global variables, class attributes, and functions
49+
are stored in the :attr:`__annotations__`
50+
special attribute of modules, classes, and functions,
51+
respectively.
52+
53+
See :term:`variable annotation`, :term:`function annotation`, :pep:`484`
54+
and :pep:`526`, which describe this functionality.
55+
4256
argument
4357
A value passed to a :term:`function` (or :term:`method`) when calling the
4458
function. There are two kinds of argument:
@@ -175,6 +189,10 @@ Glossary
175189
normally contain method definitions which operate on instances of the
176190
class.
177191

192+
class variable
193+
A variable defined in a class and intended to be modified only at
194+
class level (i.e., not in an instance of the class).
195+
178196
coercion
179197
The implicit conversion of an instance of one type to another during an
180198
operation which involves two arguments of the same type. For example,
@@ -367,14 +385,20 @@ Glossary
367385
and the :ref:`function` section.
368386

369387
function annotation
370-
An arbitrary metadata value associated with a function parameter or return
371-
value. Its syntax is explained in section :ref:`function`. Annotations
372-
may be accessed via the :attr:`__annotations__` special attribute of a
373-
function object.
388+
An :term:`annotation` of a function parameter or return value.
389+
390+
Function annotations are usually used for
391+
:term:`type hints <type hint>`: for example this function is expected to take two
392+
:class:`int` arguments and is also expected to have an :class:`int`
393+
return value::
394+
395+
def sum_two_numbers(a: int, b: int) -> int:
396+
return a + b
397+
398+
Function annotation syntax is explained in section :ref:`function`.
374399

375-
Python itself does not assign any particular meaning to function
376-
annotations. They are intended to be interpreted by third-party libraries
377-
or tools. See :pep:`3107`, which describes some of their potential uses.
400+
See :term:`variable annotation` and :pep:`484`,
401+
which describe this functionality.
378402

379403
__future__
380404
A pseudo-module which programmers can use to enable new language features
@@ -1016,6 +1040,43 @@ Glossary
10161040
:attr:`~instance.__class__` attribute or can be retrieved with
10171041
``type(obj)``.
10181042

1043+
type alias
1044+
A synonym for a type, created by assigning the type to an identifier.
1045+
1046+
Type aliases are useful for simplifying :term:`type hints <type hint>`.
1047+
For example::
1048+
1049+
from typing import List, Tuple
1050+
1051+
def remove_gray_shades(
1052+
colors: List[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
1053+
pass
1054+
1055+
could be made more readable like this::
1056+
1057+
from typing import List, Tuple
1058+
1059+
Color = Tuple[int, int, int]
1060+
1061+
def remove_gray_shades(colors: List[Color]) -> List[Color]:
1062+
pass
1063+
1064+
See :mod:`typing` and :pep:`484`, which describe this functionality.
1065+
1066+
type hint
1067+
An :term:`annotation` that specifies the expected type for a variable, a class
1068+
attribute, or a function parameter or return value.
1069+
1070+
Type hints are optional and are not enforced by Python but
1071+
they are useful to static type analysis tools, and aid IDEs with code
1072+
completion and refactoring.
1073+
1074+
Type hints of global variables, class attributes, and functions,
1075+
but not local variables, can be accessed using
1076+
:func:`typing.get_type_hints`.
1077+
1078+
See :mod:`typing` and :pep:`484`, which describe this functionality.
1079+
10191080
universal newlines
10201081
A manner of interpreting text streams in which all of the following are
10211082
recognized as ending a line: the Unix end-of-line convention ``'\n'``,
@@ -1024,16 +1085,23 @@ Glossary
10241085
:func:`bytes.splitlines` for an additional use.
10251086

10261087
variable annotation
1027-
A type metadata value associated with a module global variable or
1028-
a class attribute. Its syntax is explained in section :ref:`annassign`.
1029-
Annotations are stored in the :attr:`__annotations__` special
1030-
attribute of a class or module object and can be accessed using
1031-
:func:`typing.get_type_hints`.
1088+
An :term:`annotation` of a variable or a class attribute.
1089+
1090+
When annotating a variable or a class attribute, assignment is optional::
1091+
1092+
class C:
1093+
field: 'annotation'
1094+
1095+
Variable annotations are usually used for
1096+
:term:`type hints <type hint>`: for example this variable is expected to take
1097+
:class:`int` values::
1098+
1099+
count: int = 0
1100+
1101+
Variable annotation syntax is explained in section :ref:`annassign`.
10321102

1033-
Python itself does not assign any particular meaning to variable
1034-
annotations. They are intended to be interpreted by third-party libraries
1035-
or type checking tools. See :pep:`526`, :pep:`484` which describe
1036-
some of their potential uses.
1103+
See :term:`function annotation`, :pep:`484`
1104+
and :pep:`526`, which describe this functionality.
10371105

10381106
virtual environment
10391107
A cooperatively isolated runtime environment that allows Python users

0 commit comments

Comments
 (0)