|
12 | 12 | import sys
|
13 | 13 | import tokenize
|
14 | 14 | import types
|
| 15 | +from typing import Dict |
| 16 | +from typing import List |
| 17 | +from typing import Optional |
| 18 | +from typing import Set |
15 | 19 |
|
16 | 20 | import atomicwrites
|
17 | 21 |
|
@@ -459,39 +463,40 @@ def _fix(node, lineno, col_offset):
|
459 | 463 | return node
|
460 | 464 |
|
461 | 465 |
|
462 |
| -def _get_assertion_exprs(src: bytes): # -> Dict[int, str] |
| 466 | +def _get_assertion_exprs(src: bytes) -> Dict[int, str]: |
463 | 467 | """Returns a mapping from {lineno: "assertion test expression"}"""
|
464 |
| - ret = {} |
| 468 | + ret = {} # type: Dict[int, str] |
465 | 469 |
|
466 | 470 | depth = 0
|
467 |
| - lines = [] |
468 |
| - assert_lineno = None |
469 |
| - seen_lines = set() |
| 471 | + lines = [] # type: List[str] |
| 472 | + assert_lineno = None # type: Optional[int] |
| 473 | + seen_lines = set() # type: Set[int] |
470 | 474 |
|
471 | 475 | def _write_and_reset() -> None:
|
472 | 476 | nonlocal depth, lines, assert_lineno, seen_lines
|
| 477 | + assert assert_lineno is not None |
473 | 478 | ret[assert_lineno] = "".join(lines).rstrip().rstrip("\\")
|
474 | 479 | depth = 0
|
475 | 480 | lines = []
|
476 | 481 | assert_lineno = None
|
477 | 482 | seen_lines = set()
|
478 | 483 |
|
479 | 484 | tokens = tokenize.tokenize(io.BytesIO(src).readline)
|
480 |
| - for tp, src, (lineno, offset), _, line in tokens: |
481 |
| - if tp == tokenize.NAME and src == "assert": |
| 485 | + for tp, source, (lineno, offset), _, line in tokens: |
| 486 | + if tp == tokenize.NAME and source == "assert": |
482 | 487 | assert_lineno = lineno
|
483 | 488 | elif assert_lineno is not None:
|
484 | 489 | # keep track of depth for the assert-message `,` lookup
|
485 |
| - if tp == tokenize.OP and src in "([{": |
| 490 | + if tp == tokenize.OP and source in "([{": |
486 | 491 | depth += 1
|
487 |
| - elif tp == tokenize.OP and src in ")]}": |
| 492 | + elif tp == tokenize.OP and source in ")]}": |
488 | 493 | depth -= 1
|
489 | 494 |
|
490 | 495 | if not lines:
|
491 | 496 | lines.append(line[offset:])
|
492 | 497 | seen_lines.add(lineno)
|
493 | 498 | # a non-nested comma separates the expression from the message
|
494 |
| - elif depth == 0 and tp == tokenize.OP and src == ",": |
| 499 | + elif depth == 0 and tp == tokenize.OP and source == ",": |
495 | 500 | # one line assert with message
|
496 | 501 | if lineno in seen_lines and len(lines) == 1:
|
497 | 502 | offset_in_trimmed = offset + len(lines[-1]) - len(line)
|
|
0 commit comments