PEP 484: Fix the example code (GH-583)

This commit is contained in:
Davydenko Myroslav 2018-03-03 23:19:13 +02:00 committed by Mariatta
parent 5410bd462e
commit 5920dc1117
1 changed files with 5 additions and 7 deletions

View File

@ -355,6 +355,7 @@ You can include a ``Generic`` base class to define a user-defined class
as generic. Example::
from typing import TypeVar, Generic
from logging import Logger
T = TypeVar('T')
@ -373,7 +374,7 @@ as generic. Example::
return self.value
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``
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')
class Node(Generic[T]):
x = None # type: T # Instance attribute (see below)
def __init__(self, label: T = None) -> None:
...
x = None # Type: T
x = Node('') # Inferred type is Node[str]
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
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])
a, b, *c = range(5) # type: float, float, List[float]
x = [
1,
2,
] # type: List[int]
x = [1, 2] # type: List[int]
Type comments should be put on the last line of the statement that
contains the variable definition. They can also be placed on