Kaydet (Commit) e69657df authored tarafından Andrés Delfino's avatar Andrés Delfino Kaydeden (comit) Guido van Rossum

[3.7] bpo-32769: A new take on annotations/type hinting glossary entries (GH-6829) (#7127)

(cherry picked from commit 6e33f810)
Co-authored-by: 's avatarAndrés Delfino <adelfino@gmail.com>
üst 14d289be
...@@ -39,6 +39,20 @@ Glossary ...@@ -39,6 +39,20 @@ Glossary
and loaders (in the :mod:`importlib.abc` module). You can create your own and loaders (in the :mod:`importlib.abc` module). You can create your own
ABCs with the :mod:`abc` module. ABCs with the :mod:`abc` module.
annotation
A label associated with a variable, a class
attribute or a function parameter or return value,
used by convention as a :term:`type hint`.
Annotations of local variables cannot be accessed at runtime, but
annotations of global variables, class attributes, and functions
are stored in the :attr:`__annotations__`
special attribute of modules, classes, and functions,
respectively.
See :term:`variable annotation`, :term:`function annotation`, :pep:`484`
and :pep:`526`, which describe this functionality.
argument argument
A value passed to a :term:`function` (or :term:`method`) when calling the A value passed to a :term:`function` (or :term:`method`) when calling the
function. There are two kinds of argument: function. There are two kinds of argument:
...@@ -175,6 +189,10 @@ Glossary ...@@ -175,6 +189,10 @@ Glossary
normally contain method definitions which operate on instances of the normally contain method definitions which operate on instances of the
class. class.
class variable
A variable defined in a class and intended to be modified only at
class level (i.e., not in an instance of the class).
coercion coercion
The implicit conversion of an instance of one type to another during an The implicit conversion of an instance of one type to another during an
operation which involves two arguments of the same type. For example, operation which involves two arguments of the same type. For example,
...@@ -367,16 +385,20 @@ Glossary ...@@ -367,16 +385,20 @@ Glossary
and the :ref:`function` section. and the :ref:`function` section.
function annotation function annotation
An arbitrary metadata value associated with a function parameter or return An :term:`annotation` of a function parameter or return value.
value. Its syntax is explained in section :ref:`function`. Annotations
may be accessed via the :attr:`__annotations__` special attribute of a
function object.
See also the :term:`variable annotation` glossary entry. Function annotations are usually used for
:term:`type hints <type hint>`: for example this function is expected to take two
:class:`int` arguments and is also expected to have an :class:`int`
return value::
Annotations are meant to provide a standard way for programmers to def sum_two_numbers(a: int, b: int) -> int:
document types of functions they design. See :pep:`484`, which return a + b
describes this functionality.
Function annotation syntax is explained in section :ref:`function`.
See :term:`variable annotation` and :pep:`484`,
which describe this functionality.
__future__ __future__
A pseudo-module which programmers can use to enable new language features A pseudo-module which programmers can use to enable new language features
...@@ -1024,6 +1046,43 @@ Glossary ...@@ -1024,6 +1046,43 @@ Glossary
:attr:`~instance.__class__` attribute or can be retrieved with :attr:`~instance.__class__` attribute or can be retrieved with
``type(obj)``. ``type(obj)``.
type alias
A synonym for a type, created by assigning the type to an identifier.
Type aliases are useful for simplifying :term:`type hints <type hint>`.
For example::
from typing import List, Tuple
def remove_gray_shades(
colors: List[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
pass
could be made more readable like this::
from typing import List, Tuple
Color = Tuple[int, int, int]
def remove_gray_shades(colors: List[Color]) -> List[Color]:
pass
See :mod:`typing` and :pep:`484`, which describe this functionality.
type hint
An :term:`annotation` that specifies the expected type for a variable, a class
attribute, or a function parameter or return value.
Type hints are optional and are not enforced by Python but
they are useful to static type analysis tools, and aid IDEs with code
completion and refactoring.
Type hints of global variables, class attributes, and functions,
but not local variables, can be accessed using
:func:`typing.get_type_hints`.
See :mod:`typing` and :pep:`484`, which describe this functionality.
universal newlines universal newlines
A manner of interpreting text streams in which all of the following are A manner of interpreting text streams in which all of the following are
recognized as ending a line: the Unix end-of-line convention ``'\n'``, recognized as ending a line: the Unix end-of-line convention ``'\n'``,
...@@ -1032,17 +1091,23 @@ Glossary ...@@ -1032,17 +1091,23 @@ Glossary
:func:`bytes.splitlines` for an additional use. :func:`bytes.splitlines` for an additional use.
variable annotation variable annotation
A type metadata value associated with a module global variable or An :term:`annotation` of a variable or a class attribute.
a class attribute. Its syntax is explained in section :ref:`annassign`.
Annotations are stored in the :attr:`__annotations__` special
attribute of a class or module object and can be accessed using
:func:`typing.get_type_hints`.
See also the :term:`function annotation` glossary entry. When annotating a variable or a class attribute, assignment is optional::
Annotations are meant to provide a standard way for programmers to class C:
document types of functions they design. See :pep:`484` and :pep:`526` field: 'annotation'
which describe this functionality.
Variable annotations are usually used for
:term:`type hints <type hint>`: for example this variable is expected to take
:class:`int` values::
count: int = 0
Variable annotation syntax is explained in section :ref:`annassign`.
See :term:`function annotation`, :pep:`484`
and :pep:`526`, which describe this functionality.
virtual environment virtual environment
A cooperatively isolated runtime environment that allows Python users A cooperatively isolated runtime environment that allows Python users
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment