Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39b7030

Browse files
author
Miguel Garcia Lafuente
committed
corrected some issues & typos
1 parent 08fb60b commit 39b7030

9 files changed

Lines changed: 63 additions & 63 deletions

File tree

mypy/checker.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ def visit_return_stmt(self, s: ReturnStmt) -> Type:
12101210
self.fail(messages.NO_RETURN_VALUE_EXPECTED, s)
12111211
else:
12121212
if self.function_stack[-1].is_coroutine: # Something similar will be needed to mix return and yield
1213-
#If the function is a coroutine, wrap the return type in a Future
1213+
# If the function is a coroutine, wrap the return type in a Future
12141214
typ = self.wrap_generic_type(typ, self.return_types[-1], 'asyncio.futures.Future', s)
12151215
self.check_subtype(
12161216
typ, self.return_types[-1], s,
@@ -1225,20 +1225,21 @@ def visit_return_stmt(self, s: ReturnStmt) -> Type:
12251225
self.fail(messages.RETURN_VALUE_EXPECTED, s)
12261226

12271227
def wrap_generic_type(self, typ: Type, rtyp: Type, check_type: str, context: Context) -> Type:
1228-
n_diff = self.count_concatenated_types(rtyp, check_type) - self.count_concatenated_types(typ, check_type)
1229-
if n_diff >= 1:
1228+
n_diff = self.count_nested_types(rtyp, check_type) - self.count_nested_types(typ, check_type)
1229+
if n_diff == 1:
12301230
return self.named_generic_type(check_type, [typ])
1231-
elif n_diff == 0:
1231+
elif n_diff == 0 or n_diff > 1:
12321232
self.fail(messages.INCOMPATIBLE_RETURN_VALUE_TYPE
12331233
+ ": expected {}, got {}".format(rtyp, typ), context)
12341234
return typ
12351235
return typ
12361236

1237-
def count_concatenated_types(self, typ: Type, check_type: str) -> int:
1237+
def count_nested_types(self, typ: Type, check_type: str) -> int:
12381238
c = 0
12391239
while is_subtype(typ, self.named_type(check_type)):
12401240
c += 1
1241-
if hasattr(typ, 'args') and typ.args:
1241+
typ = map_instance_to_supertype(typ, self.lookup_typeinfo(check_type))
1242+
if typ.args:
12421243
typ = typ.args[0]
12431244
else:
12441245
return c
@@ -1268,7 +1269,7 @@ def visit_yield_from_stmt(self, s: YieldFromStmt) -> Type:
12681269
return_type = self.return_types[-1]
12691270
type_func = self.accept(s.expr, return_type)
12701271
if isinstance(type_func, Instance):
1271-
if hasattr(type_func, 'type') and hasattr(type_func.type, 'fullname') and type_func.type.fullname() == 'asyncio.futures.Future':
1272+
if type_func.type.fullname() == 'asyncio.futures.Future':
12721273
# if is a Future, in stmt don't need to do nothing
12731274
# because the type Future[Some] jus matters to the main loop
12741275
# that python executes, in statement we shouldn't get the Future,
@@ -1277,15 +1278,15 @@ def visit_yield_from_stmt(self, s: YieldFromStmt) -> Type:
12771278
elif is_subtype(type_func, self.named_type('typing.Iterable')):
12781279
# If it's and Iterable-Like, let's check the types.
12791280
# Maybe just check if have __iter__? (like in analyse_iterable)
1280-
self.check_iterable_yf(s)
1281+
self.check_iterable_yield_from(s)
12811282
else:
1282-
self.msg.yield_from_not_valid_applied(type_func, s)
1283+
self.msg.yield_from_invalid_operand_type(type_func, s)
12831284
elif isinstance(type_func, AnyType):
1284-
self.check_iterable_yf(s)
1285+
self.check_iterable_yield_from(s)
12851286
else:
1286-
self.msg.yield_from_not_valid_applied(type_func, s)
1287+
self.msg.yield_from_invalid_operand_type(type_func, s)
12871288

1288-
def check_iterable_yf(self, s: YieldFromStmt) -> Type:
1289+
def check_iterable_yield_from(self, s: YieldFromStmt) -> Type:
12891290
"""
12901291
Check that return type is super type of Iterable (Maybe just check if have __iter__?)
12911292
and compare it with the type of the expression
@@ -1295,9 +1296,9 @@ def check_iterable_yf(self, s: YieldFromStmt) -> Type:
12951296
if not is_subtype(expected_item_type, self.named_type('typing.Iterable')):
12961297
self.fail(messages.INVALID_RETURN_TYPE_FOR_YIELD_FROM, s)
12971298
return None
1298-
elif hasattr(expected_item_type, 'args') and expected_item_type.args:
1299+
elif expected_item_type.args:
1300+
expected_item_type = map_instance_to_supertype(expected_item_type, self.lookup_typeinfo('typing.Iterable'))
12991301
expected_item_type = expected_item_type.args[0] # Take the item inside the iterator
1300-
# expected_item_type = expected_item_type
13011302
elif isinstance(expected_item_type, AnyType):
13021303
expected_item_type = AnyType()
13031304
else:
@@ -1308,6 +1309,7 @@ def check_iterable_yf(self, s: YieldFromStmt) -> Type:
13081309
else:
13091310
actual_item_type = self.accept(s.expr, expected_item_type)
13101311
if hasattr(actual_item_type, 'args') and actual_item_type.args:
1312+
actual_item_type = map_instance_to_supertype(actual_item_type, self.lookup_typeinfo('typing.Iterable'))
13111313
actual_item_type = actual_item_type.args[0] # Take the item inside the iterator
13121314
self.check_subtype(actual_item_type, expected_item_type, s,
13131315
messages.INCOMPATIBLE_TYPES_IN_YIELD_FROM,
@@ -1625,7 +1627,7 @@ def visit_call_expr(self, e: CallExpr) -> Type:
16251627

16261628
def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
16271629
result = self.expr_checker.visit_yield_from_expr(e)
1628-
if hasattr(result, 'type') and result.type.fullname() == "asyncio.futures.Future":
1630+
if result.type.fullname() == "asyncio.futures.Future":
16291631
self.function_stack[-1].is_coroutine = True # Set the function as coroutine
16301632
result = result.args[0] # Set the return type as the type inside
16311633
elif is_subtype(result, self.named_type('typing.Iterable')):
@@ -1634,8 +1636,7 @@ def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
16341636
# Maybe set result like in the Future
16351637
pass
16361638
else:
1637-
self.msg.yield_from_not_valid_applied(e.expr, e)
1638-
self.breaking_out = False
1639+
self.msg.yield_from_invalid_operand_type(e.expr, e)
16391640
return result
16401641

16411642
def visit_member_expr(self, e: MemberExpr) -> Type:

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def signatures_incompatible(self, method: str, other_method: str,
637637
self.fail('Signatures of "{}" and "{}" are incompatible'.format(
638638
method, other_method), context)
639639

640-
def yield_from_not_valid_applied(self, expr: Type, context: Context) -> Type:
640+
def yield_from_invalid_operand_type(self, expr: Type, context: Context) -> Type:
641641
text = self.format(expr) if self.format(expr) != 'object' else expr
642642
self.fail('"yield from" can\'t be applied to {}'.format(text), context)
643643
return AnyType()

mypy/nodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ def __init__(self, expr: Node) -> None:
936936
def accept(self, visitor: NodeVisitor[T]) -> T:
937937
return visitor.visit_yield_from_expr(self)
938938

939+
939940
class IndexExpr(Node):
940941
"""Index expression x[y].
941942

mypy/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def parse_return_stmt(self) -> ReturnStmt:
742742
expr = None # type: Node
743743
if not isinstance(self.current(), Break):
744744
expr = self.parse_expression()
745-
if isinstance(expr, YieldFromExpr): #cant go a yield from expr
745+
if isinstance(expr, YieldFromExpr): # "yield from" expressions can't be returned.
746746
return None
747747
br = self.expect_break()
748748
node = ReturnStmt(expr)

mypy/test/data/check-statements.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def f() -> Iterator[None]:
626626
-- Iterables
627627
-- ----------
628628

629-
[case testSimpleYFIter]
629+
[case testSimpleYieldFromWithIterator]
630630
from typing import Iterator
631631
def g() -> Iterator[str]:
632632
yield '42'
@@ -638,15 +638,15 @@ def f() -> Iterator[str]:
638638
[out]
639639
main: In function "f":
640640

641-
[case testYFAppliedToAny]
641+
[case testYieldFromAppliedToAny]
642642
from typing import Any
643643
def g() -> Any:
644644
yield object()
645645
def f() -> Any:
646646
yield from g()
647647
[out]
648648

649-
[case testYFInFunctionReturningFunction]
649+
[case testYieldFromInFunctionReturningFunction]
650650
from typing import Iterator, Function
651651
def g() -> Iterator[int]:
652652
yield 42
@@ -655,7 +655,7 @@ def f() -> Function[[], None]:
655655
[out]
656656
main: In function "f":
657657

658-
[case testGoodYFNotIterableReturnType]
658+
[case testYieldFromNotIterableReturnType]
659659
from typing import Iterator
660660
def g() -> Iterator[int]:
661661
yield 42
@@ -664,7 +664,7 @@ def f() -> int:
664664
[out]
665665
main: In function "f":
666666

667-
[case testYFNotAppliedIter]
667+
[case testYieldFromNotAppliedIterator]
668668
from typing import Iterator
669669
def g() -> int:
670670
return 42
@@ -673,7 +673,7 @@ def f() -> Iterator[int]:
673673
[out]
674674
main: In function "f":
675675

676-
[case testYFCheckIncompatibleTypesTwoIterables]
676+
[case testYieldFromCheckIncompatibleTypesTwoIterables]
677677
from typing import List, Iterator
678678
def g() -> Iterator[List[int]]:
679679
yield [2, 3, 4]
@@ -684,13 +684,13 @@ def f() -> Iterator[List[int]]:
684684
[out]
685685
main: In function "f":
686686

687-
[case testYFNotAppliedToNothing]
687+
[case testYieldFromNotAppliedToNothing]
688688
def h():
689689
yield from # E: Parse error before end of line
690690
[out]
691691
main: In function "h":
692692

693-
[case testYFAndYieldTogether]
693+
[case testYieldFromAndYieldTogether]
694694
from typing import Iterator
695695
def f() -> Iterator[str]:
696696
yield "g1 ham"

mypy/test/data/parse-errors.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def f():
348348
file: In function "f":
349349
file, line 2: Parse error before end of line
350350

351-
[case testYielFromAfterReturn]
351+
[case testYieldFromAfterReturn]
352352
def f():
353353
return yield from h()
354354
[out]

stubs/3.4/asyncio/events.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,36 @@ def set_default_executor(self, executor: Any) -> None: pass
6161
# Network I/O methods returning Futures.
6262
@abstractmethod
6363
def getaddrinfo(self, host: str, port: int, *,
64-
family: int=0, type: int=0, proto: int=0, flags: int=0) -> List[Tuple[int, int, int, str, tuple]]: pass
64+
family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> List[Tuple[int, int, int, str, tuple]]: pass
6565
@abstractmethod
66-
def getnameinfo(self, sockaddr: tuple, flags: int=0) -> Tuple[str, int]: pass
66+
def getnameinfo(self, sockaddr: tuple, flags: int = 0) -> Tuple[str, int]: pass
6767
@abstractmethod
68-
def create_connection(self, protocol_factory: Any, host: str=None, port: int=None, *,
69-
ssl: Any=None, family: int=0, proto: int=0, flags: int=0, sock: Any=None,
70-
local_addr: str=None, server_hostname: str=None) -> tuple: pass
68+
def create_connection(self, protocol_factory: Any, host: str = None, port: int = None, *,
69+
ssl: Any = None, family: int = 0, proto: int = 0, flags: int = 0, sock: Any = None,
70+
local_addr: str = None, server_hostname: str = None) -> tuple: pass
7171
# ?? check Any
7272
# return (Transport, Protocol)
7373
@abstractmethod
74-
def create_server(self, protocol_factory: Any, host: str=None, port: int=None, *,
75-
family: int=AF_UNSPEC, flags: int=AI_PASSIVE,
76-
sock: Any=None, backlog: int=100, ssl: Any=None, reuse_address: Any=None) -> Any: pass
74+
def create_server(self, protocol_factory: Any, host: str = None, port: int = None, *,
75+
family: int = AF_UNSPEC, flags: int = AI_PASSIVE,
76+
sock: Any = None, backlog: int = 100, ssl: Any = None, reuse_address: Any = None) -> Any: pass
7777
# ?? check Any
7878
# return Server
7979
@abstractmethod
8080
def create_unix_connection(self, protocol_factory: Any, path: str, *,
81-
ssl: Any=None, sock: Any=None,
82-
server_hostname: str=None) -> tuple: pass
81+
ssl: Any = None, sock: Any = None,
82+
server_hostname: str = None) -> tuple: pass
8383
# ?? check Any
8484
# return tuple(Transport, Protocol)
8585
@abstractmethod
8686
def create_unix_server(self, protocol_factory: Any, path: str, *,
87-
sock: Any=None, backlog: int=100, ssl: Any=None) -> Any: pass
87+
sock: Any = None, backlog: int = 100, ssl: Any = None) -> Any: pass
8888
# ?? check Any
8989
# return Server
9090
@abstractmethod
9191
def create_datagram_endpoint(self, protocol_factory: Any,
92-
local_addr: str=None, remote_addr: str=None, *,
93-
family: int=0, proto: int=0, flags: int=0) -> tuple: pass
92+
local_addr: str = None, remote_addr: str = None, *,
93+
family: int = 0, proto: int = 0, flags: int = 0) -> tuple: pass
9494
#?? check Any
9595
# return (Transport, Protocol)
9696
# Pipes and subprocesses.
@@ -103,14 +103,14 @@ def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: pass
103103
#?? check Any
104104
# return (Transport, Protocol)
105105
@abstractmethod
106-
def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes,str], *, stdin: Any=PIPE,
107-
stdout: Any=PIPE, stderr: Any=PIPE,
106+
def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes, str], *, stdin: Any = PIPE,
107+
stdout: Any = PIPE, stderr: Any = PIPE,
108108
**kwargs: Dict[str, Any]) -> tuple: pass
109109
#?? check Any
110110
# return (Transport, Protocol)
111111
@abstractmethod
112-
def subprocess_exec(self, protocol_factory: Any, *args: List[Any], stdin: Any=PIPE,
113-
stdout: Any=PIPE, stderr: Any=PIPE,
112+
def subprocess_exec(self, protocol_factory: Any, *args: List[Any], stdin: Any = PIPE,
113+
stdout: Any = PIPE, stderr: Any = PIPE,
114114
**kwargs: Dict[str, Any]) -> tuple: pass
115115
#?? check Any
116116
# return (Transport, Protocol)

stubs/3.4/asyncio/futures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def activate(self) -> None: pass
1717
def clear(self) -> None: pass
1818
def __del__(self) -> None: pass
1919

20-
class Future(Iterator[_T], Generic[_T]): # (Iterable[_T], Generic[_T])
20+
class Future(Iterator[_T], Generic[_T]):
2121
_state = ''
2222
_exception = Any #Exception
2323
_blocking = False
@@ -32,10 +32,10 @@ def cancelled(self) -> bool: pass
3232
def done(self) -> bool: pass
3333
def result(self) -> _T: pass
3434
def exception(self) -> Any: pass
35-
def add_done_callback(self, fn: Function[[Future[_T]],Any]) -> None: pass
35+
def add_done_callback(self, fn: Function[[Future[_T]], Any]) -> None: pass
3636
def remove_done_callback(self, fn: Function[[Future[_T]], Any]) -> int: pass
3737
def set_result(self, result: _T) -> None: pass
3838
def set_exception(self, exception: Any) -> None: pass
3939
def _copy_state(self, other: Any) -> None: pass
40-
def __iter__(self) -> 'Iterator[_T]': pass
41-
def __next__(self) -> '_T': pass
40+
def __iter__(self) -> Iterator[_T]: pass
41+
def __next__(self) -> _T: pass

stubs/3.4/asyncio/tasks.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,25 @@
1414
FIRST_COMPLETED = 'FIRST_COMPLETED'
1515
ALL_COMPLETED = 'ALL_COMPLETED'
1616
_T = typevar('_T')
17-
def coroutine(f: Any) -> Any: pass # Here comes and go a function
18-
def sleep(delay: float, result: _T=None, loop: AbstractEventLoop=None) -> Future[_T]: pass
19-
def wait(fs: List[Any], *, loop: AbstractEventLoop=None,
20-
timeout: float=None, return_when: str=ALL_COMPLETED) -> Future[Tuple[Set[Future[_T]], Set[Future[_T]]]]: pass
21-
def wait_for(fut: Future[_T], timeout: float, *, loop: AbstractEventLoop=None) -> Future[_T]: pass
22-
# def wait(fs: Union[List[Iterable], List[Future[_T]]], *, loop: AbstractEventLoop=None,
23-
# timeout: int=None, return_when: str=ALL_COMPLETED) -> Future[Tuple[Set[Future[_T]], Set[Future[_T]]]]: pass
17+
def coroutine(f: _T) -> _T: pass # Here comes and go a function
18+
def sleep(delay: float, result: _T = None, loop: AbstractEventLoop = None) -> Future[_T]: pass
19+
def wait(fs: List[Future[_T]], *, loop: AbstractEventLoop = None,
20+
timeout: float = None, return_when: str = ALL_COMPLETED) -> Future[Tuple[Set[Future[_T]], Set[Future[_T]]]]: pass
21+
def wait_for(fut: Future[_T], timeout: float, *, loop: AbstractEventLoop = None) -> Future[_T]: pass
22+
2423

2524
class Task(Future[_T], Generic[_T]):
2625
_all_tasks = None # type: Set[Task]
2726
_current_tasks = {} # type: Dict[AbstractEventLoop, Task]
2827
@classmethod
29-
def current_task(cls, loop: AbstractEventLoop=None) -> Task: pass
28+
def current_task(cls, loop: AbstractEventLoop = None) -> Task: pass
3029
@classmethod
31-
def all_tasks(cls, loop: AbstractEventLoop=None) -> Set[Task]: pass
32-
# def __init__(self, coro: Union[Iterable[_T], Future[_T]], *, loop: AbstractEventLoop=None) -> None: pass
33-
def __init__(self, coro: Future[_T], *, loop: AbstractEventLoop=None) -> None: pass
30+
def all_tasks(cls, loop: AbstractEventLoop = None) -> Set[Task]: pass
31+
def __init__(self, coro: Future[_T], *, loop: AbstractEventLoop = None) -> None: pass
3432
def __repr__(self) -> str: pass
35-
def get_stack(self, *, limit: int=None) -> List[Any]: pass # return List[stackframe]
36-
def print_stack(self, *, limit: int=None, file: TextIO=None) -> None: pass
33+
def get_stack(self, *, limit: int = None) -> List[Any]: pass # return List[stackframe]
34+
def print_stack(self, *, limit: int = None, file: TextIO = None) -> None: pass
3735
def cancel(self) -> bool: pass
38-
def _step(self, value: Any=None, exc: Exception=None) -> None: pass
36+
def _step(self, value: Any = None, exc: Exception = None) -> None: pass
3937
def _wakeup(self, future: Future[Any]) -> None: pass
4038

0 commit comments

Comments
 (0)