PEP 484: Fix the example code (GH-583)
This commit is contained in:
parent
5410bd462e
commit
5920dc1117
12
pep-0484.txt
12
pep-0484.txt
|
@ -355,6 +355,7 @@ You can include a ``Generic`` base class to define a user-defined class
|
||||||
as generic. Example::
|
as generic. Example::
|
||||||
|
|
||||||
from typing import TypeVar, Generic
|
from typing import TypeVar, Generic
|
||||||
|
from logging import Logger
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
@ -373,7 +374,7 @@ as generic. Example::
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
def log(self, message: str) -> None:
|
def log(self, message: str) -> None:
|
||||||
self.logger.info('{}: {}'.format(self.name message))
|
self.logger.info('{}: {}'.format(self.name, message))
|
||||||
|
|
||||||
``Generic[T]`` as a base class defines that the class ``LoggedVar``
|
``Generic[T]`` as a base class defines that the class ``LoggedVar``
|
||||||
takes a single type parameter ``T``. This also makes ``T`` valid as
|
takes a single type parameter ``T``. This also makes ``T`` valid as
|
||||||
|
@ -582,9 +583,9 @@ argument(s) is substituted. Otherwise, ``Any`` is assumed. Example::
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
class Node(Generic[T]):
|
class Node(Generic[T]):
|
||||||
|
x = None # type: T # Instance attribute (see below)
|
||||||
def __init__(self, label: T = None) -> None:
|
def __init__(self, label: T = None) -> None:
|
||||||
...
|
...
|
||||||
x = None # Type: T
|
|
||||||
|
|
||||||
x = Node('') # Inferred type is Node[str]
|
x = Node('') # Inferred type is Node[str]
|
||||||
y = Node(0) # Inferred type is Node[int]
|
y = Node(0) # Inferred type is Node[int]
|
||||||
|
@ -1460,14 +1461,11 @@ No first-class syntax support for explicitly marking variables as being
|
||||||
of a specific type is added by this PEP. To help with type inference in
|
of a specific type is added by this PEP. To help with type inference in
|
||||||
complex cases, a comment of the following format may be used::
|
complex cases, a comment of the following format may be used::
|
||||||
|
|
||||||
x = [] # type: List[Employee]
|
x = [] # type: List[Employee]
|
||||||
x, y, z = [], [], [] # type: List[int], List[int], List[str]
|
x, y, z = [], [], [] # type: List[int], List[int], List[str]
|
||||||
x, y, z = [], [], [] # type: (List[int], List[int], List[str])
|
x, y, z = [], [], [] # type: (List[int], List[int], List[str])
|
||||||
a, b, *c = range(5) # type: float, float, List[float]
|
a, b, *c = range(5) # type: float, float, List[float]
|
||||||
x = [
|
x = [1, 2] # type: List[int]
|
||||||
1,
|
|
||||||
2,
|
|
||||||
] # type: List[int]
|
|
||||||
|
|
||||||
Type comments should be put on the last line of the statement that
|
Type comments should be put on the last line of the statement that
|
||||||
contains the variable definition. They can also be placed on
|
contains the variable definition. They can also be placed on
|
||||||
|
|
Loading…
Reference in New Issue