forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastparse.py
2194 lines (1946 loc) · 83.1 KB
/
fastparse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import copy
import re
import sys
import warnings
from typing import Any, Callable, Final, List, Optional, Sequence, TypeVar, Union, cast
from typing_extensions import Literal, overload
from mypy import defaults, errorcodes as codes, message_registry
from mypy.errors import Errors
from mypy.message_registry import ErrorMessage
from mypy.nodes import (
ARG_NAMED,
ARG_NAMED_OPT,
ARG_OPT,
ARG_POS,
ARG_STAR,
ARG_STAR2,
PARAM_SPEC_KIND,
TYPE_VAR_KIND,
TYPE_VAR_TUPLE_KIND,
ArgKind,
Argument,
AssertStmt,
AssignmentExpr,
AssignmentStmt,
AwaitExpr,
Block,
BreakStmt,
BytesExpr,
CallExpr,
ClassDef,
ComparisonExpr,
ComplexExpr,
ConditionalExpr,
ContinueStmt,
Decorator,
DelStmt,
DictExpr,
DictionaryComprehension,
EllipsisExpr,
Expression,
ExpressionStmt,
FakeInfo,
FloatExpr,
ForStmt,
FuncDef,
GeneratorExpr,
GlobalDecl,
IfStmt,
Import,
ImportAll,
ImportBase,
ImportFrom,
IndexExpr,
IntExpr,
LambdaExpr,
ListComprehension,
ListExpr,
MatchStmt,
MemberExpr,
MypyFile,
NameExpr,
Node,
NonlocalDecl,
OperatorAssignmentStmt,
OpExpr,
OverloadedFuncDef,
OverloadPart,
PassStmt,
RaiseStmt,
RefExpr,
ReturnStmt,
SetComprehension,
SetExpr,
SliceExpr,
StarExpr,
Statement,
StrExpr,
SuperExpr,
TempNode,
TryStmt,
TupleExpr,
TypeAliasStmt,
TypeParam,
UnaryExpr,
Var,
WhileStmt,
WithStmt,
YieldExpr,
YieldFromExpr,
check_arg_names,
)
from mypy.options import NEW_GENERIC_SYNTAX, Options
from mypy.patterns import (
AsPattern,
ClassPattern,
MappingPattern,
OrPattern,
SequencePattern,
SingletonPattern,
StarredPattern,
ValuePattern,
)
from mypy.reachability import infer_reachability_of_if_statement, mark_block_unreachable
from mypy.sharedparse import argument_elide_name, special_function_elide_names
from mypy.traverser import TraverserVisitor
from mypy.types import (
AnyType,
CallableArgument,
CallableType,
EllipsisType,
Instance,
ProperType,
RawExpressionType,
TupleType,
Type,
TypeList,
TypeOfAny,
UnboundType,
UnionType,
UnpackType,
)
from mypy.util import bytes_to_human_readable_repr, unnamed_function
# pull this into a final variable to make mypyc be quiet about the
# the default argument warning
PY_MINOR_VERSION: Final = sys.version_info[1]
import ast as ast3
# TODO: Index, ExtSlice are deprecated in 3.9.
from ast import AST, Attribute, Call, FunctionType, Index, Name, Starred, UAdd, UnaryOp, USub
def ast3_parse(
source: str | bytes, filename: str, mode: str, feature_version: int = PY_MINOR_VERSION
) -> AST:
return ast3.parse(
source,
filename,
mode,
type_comments=True, # This works the magic
feature_version=feature_version,
)
NamedExpr = ast3.NamedExpr
Constant = ast3.Constant
if sys.version_info >= (3, 10):
Match = ast3.Match
MatchValue = ast3.MatchValue
MatchSingleton = ast3.MatchSingleton
MatchSequence = ast3.MatchSequence
MatchStar = ast3.MatchStar
MatchMapping = ast3.MatchMapping
MatchClass = ast3.MatchClass
MatchAs = ast3.MatchAs
MatchOr = ast3.MatchOr
AstNode = Union[ast3.expr, ast3.stmt, ast3.pattern, ast3.ExceptHandler]
else:
Match = Any
MatchValue = Any
MatchSingleton = Any
MatchSequence = Any
MatchStar = Any
MatchMapping = Any
MatchClass = Any
MatchAs = Any
MatchOr = Any
AstNode = Union[ast3.expr, ast3.stmt, ast3.ExceptHandler]
if sys.version_info >= (3, 11):
TryStar = ast3.TryStar
else:
TryStar = Any
if sys.version_info >= (3, 12):
ast_TypeAlias = ast3.TypeAlias
ast_ParamSpec = ast3.ParamSpec
ast_TypeVarTuple = ast3.TypeVarTuple
else:
ast_TypeAlias = Any
ast_ParamSpec = Any
ast_TypeVarTuple = Any
N = TypeVar("N", bound=Node)
# There is no way to create reasonable fallbacks at this stage,
# they must be patched later.
MISSING_FALLBACK: Final = FakeInfo("fallback can't be filled out until semanal")
_dummy_fallback: Final = Instance(MISSING_FALLBACK, [], -1)
TYPE_IGNORE_PATTERN: Final = re.compile(r"[^#]*#\s*type:\s*ignore\s*(.*)")
def parse(
source: str | bytes,
fnam: str,
module: str | None,
errors: Errors,
options: Options | None = None,
) -> MypyFile:
"""Parse a source file, without doing any semantic analysis.
Return the parse tree. If errors is not provided, raise ParseError
on failure. Otherwise, use the errors object to report parse errors.
"""
ignore_errors = (options is not None and options.ignore_errors) or (
fnam in errors.ignored_files
)
# If errors are ignored, we can drop many function bodies to speed up type checking.
strip_function_bodies = ignore_errors and (options is None or not options.preserve_asts)
if options is None:
options = Options()
errors.set_file(fnam, module, options=options)
is_stub_file = fnam.endswith(".pyi")
if is_stub_file:
feature_version = defaults.PYTHON3_VERSION[1]
if options.python_version[0] == 3 and options.python_version[1] > feature_version:
feature_version = options.python_version[1]
else:
assert options.python_version[0] >= 3
feature_version = options.python_version[1]
try:
# Disable deprecation warnings about \u
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
ast = ast3_parse(source, fnam, "exec", feature_version=feature_version)
tree = ASTConverter(
options=options,
is_stub=is_stub_file,
errors=errors,
strip_function_bodies=strip_function_bodies,
path=fnam,
).visit(ast)
except SyntaxError as e:
# alias to please mypyc
is_py38_or_earlier = sys.version_info < (3, 9)
if is_py38_or_earlier and e.filename == "<fstring>":
# In Python 3.8 and earlier, syntax errors in f-strings have lineno relative to the
# start of the f-string. This would be misleading, as mypy will report the error as the
# lineno within the file.
e.lineno = None
message = e.msg
if feature_version > sys.version_info.minor and message.startswith("invalid syntax"):
python_version_str = f"{options.python_version[0]}.{options.python_version[1]}"
message += f"; you likely need to run mypy using Python {python_version_str} or newer"
errors.report(
e.lineno if e.lineno is not None else -1,
e.offset,
message,
blocker=True,
code=codes.SYNTAX,
)
tree = MypyFile([], [], False, {})
assert isinstance(tree, MypyFile)
return tree
def parse_type_ignore_tag(tag: str | None) -> list[str] | None:
"""Parse optional "[code, ...]" tag after "# type: ignore".
Return:
* [] if no tag was found (ignore all errors)
* list of ignored error codes if a tag was found
* None if the tag was invalid.
"""
if not tag or tag.strip() == "" or tag.strip().startswith("#"):
# No tag -- ignore all errors.
return []
m = re.match(r"\s*\[([^]#]*)\]\s*(#.*)?$", tag)
if m is None:
# Invalid "# type: ignore" comment.
return None
return [code.strip() for code in m.group(1).split(",")]
def parse_type_comment(
type_comment: str, line: int, column: int, errors: Errors | None
) -> tuple[list[str] | None, ProperType | None]:
"""Parse type portion of a type comment (+ optional type ignore).
Return (ignore info, parsed type).
"""
try:
typ = ast3_parse(type_comment, "<type_comment>", "eval")
except SyntaxError:
if errors is not None:
stripped_type = type_comment.split("#", 2)[0].strip()
err_msg = message_registry.TYPE_COMMENT_SYNTAX_ERROR_VALUE.format(stripped_type)
errors.report(line, column, err_msg.value, blocker=True, code=err_msg.code)
return None, None
else:
raise
else:
extra_ignore = TYPE_IGNORE_PATTERN.match(type_comment)
if extra_ignore:
tag: str | None = extra_ignore.group(1)
ignored: list[str] | None = parse_type_ignore_tag(tag)
if ignored is None:
if errors is not None:
errors.report(
line, column, message_registry.INVALID_TYPE_IGNORE.value, code=codes.SYNTAX
)
else:
raise SyntaxError
else:
ignored = None
assert isinstance(typ, ast3.Expression)
converted = TypeConverter(
errors, line=line, override_column=column, is_evaluated=False
).visit(typ.body)
return ignored, converted
def parse_type_string(
expr_string: str, expr_fallback_name: str, line: int, column: int
) -> ProperType:
"""Parses a type that was originally present inside of an explicit string.
For example, suppose we have the type `Foo["blah"]`. We should parse the
string expression "blah" using this function.
"""
try:
_, node = parse_type_comment(f"({expr_string})", line=line, column=column, errors=None)
return RawExpressionType(expr_string, expr_fallback_name, line, column, node=node)
except (SyntaxError, ValueError):
# Note: the parser will raise a `ValueError` instead of a SyntaxError if
# the string happens to contain things like \x00.
return RawExpressionType(expr_string, expr_fallback_name, line, column)
def is_no_type_check_decorator(expr: ast3.expr) -> bool:
if isinstance(expr, Name):
return expr.id == "no_type_check"
elif isinstance(expr, Attribute):
if isinstance(expr.value, Name):
return expr.value.id == "typing" and expr.attr == "no_type_check"
return False
class ASTConverter:
def __init__(
self,
options: Options,
is_stub: bool,
errors: Errors,
*,
strip_function_bodies: bool,
path: str,
) -> None:
# 'C' for class, 'D' for function signature, 'F' for function, 'L' for lambda
self.class_and_function_stack: list[Literal["C", "D", "F", "L"]] = []
self.imports: list[ImportBase] = []
self.options = options
self.is_stub = is_stub
self.errors = errors
self.strip_function_bodies = strip_function_bodies
self.path = path
self.type_ignores: dict[int, list[str]] = {}
# Cache of visit_X methods keyed by type of visited object
self.visitor_cache: dict[type, Callable[[AST | None], Any]] = {}
def note(self, msg: str, line: int, column: int) -> None:
self.errors.report(line, column, msg, severity="note", code=codes.SYNTAX)
def fail(self, msg: ErrorMessage, line: int, column: int, blocker: bool = True) -> None:
if blocker or not self.options.ignore_errors:
# Make sure self.errors reflects any type ignores that we have parsed
self.errors.set_file_ignored_lines(
self.path, self.type_ignores, self.options.ignore_errors
)
self.errors.report(line, column, msg.value, blocker=blocker, code=msg.code)
def fail_merge_overload(self, node: IfStmt) -> None:
self.fail(
message_registry.FAILED_TO_MERGE_OVERLOADS,
line=node.line,
column=node.column,
blocker=False,
)
def visit(self, node: AST | None) -> Any:
if node is None:
return None
typeobj = type(node)
visitor = self.visitor_cache.get(typeobj)
if visitor is None:
method = "visit_" + node.__class__.__name__
visitor = getattr(self, method)
self.visitor_cache[typeobj] = visitor
return visitor(node)
def set_line(self, node: N, n: AstNode) -> N:
node.line = n.lineno
node.column = n.col_offset
node.end_line = getattr(n, "end_lineno", None)
node.end_column = getattr(n, "end_col_offset", None)
return node
def translate_opt_expr_list(self, l: Sequence[AST | None]) -> list[Expression | None]:
res: list[Expression | None] = []
for e in l:
exp = self.visit(e)
res.append(exp)
return res
def translate_expr_list(self, l: Sequence[AST]) -> list[Expression]:
return cast(List[Expression], self.translate_opt_expr_list(l))
def get_lineno(self, node: ast3.expr | ast3.stmt) -> int:
if (
isinstance(node, (ast3.AsyncFunctionDef, ast3.ClassDef, ast3.FunctionDef))
and node.decorator_list
):
return node.decorator_list[0].lineno
return node.lineno
def translate_stmt_list(
self,
stmts: Sequence[ast3.stmt],
*,
ismodule: bool = False,
can_strip: bool = False,
is_coroutine: bool = False,
) -> list[Statement]:
# A "# type: ignore" comment before the first statement of a module
# ignores the whole module:
if (
ismodule
and stmts
and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])
):
ignores = self.type_ignores[min(self.type_ignores)]
if ignores:
joined_ignores = ", ".join(ignores)
self.fail(
message_registry.TYPE_IGNORE_WITH_ERRCODE_ON_MODULE.format(joined_ignores),
line=min(self.type_ignores),
column=0,
blocker=False,
)
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
codes.FILE.code
)
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
self.set_block_lines(block, stmts)
mark_block_unreachable(block)
return [block]
stack = self.class_and_function_stack
# Fast case for stripping function bodies
if (
can_strip
and self.strip_function_bodies
and len(stack) == 1
and stack[0] == "F"
and not is_coroutine
):
return []
res: list[Statement] = []
for stmt in stmts:
node = self.visit(stmt)
res.append(node)
# Slow case for stripping function bodies
if can_strip and self.strip_function_bodies:
if stack[-2:] == ["C", "F"]:
if is_possible_trivial_body(res):
can_strip = False
else:
# We only strip method bodies if they don't assign to an attribute, as
# this may define an attribute which has an externally visible effect.
visitor = FindAttributeAssign()
for s in res:
s.accept(visitor)
if visitor.found:
can_strip = False
break
if can_strip and stack[-1] == "F" and is_coroutine:
# Yields inside an async function affect the return type and should not
# be stripped.
yield_visitor = FindYield()
for s in res:
s.accept(yield_visitor)
if yield_visitor.found:
can_strip = False
break
if can_strip:
return []
return res
def translate_type_comment(
self, n: ast3.stmt | ast3.arg, type_comment: str | None
) -> ProperType | None:
if type_comment is None:
return None
else:
lineno = n.lineno
extra_ignore, typ = parse_type_comment(type_comment, lineno, n.col_offset, self.errors)
if extra_ignore is not None:
self.type_ignores[lineno] = extra_ignore
return typ
op_map: Final[dict[type[AST], str]] = {
ast3.Add: "+",
ast3.Sub: "-",
ast3.Mult: "*",
ast3.MatMult: "@",
ast3.Div: "/",
ast3.Mod: "%",
ast3.Pow: "**",
ast3.LShift: "<<",
ast3.RShift: ">>",
ast3.BitOr: "|",
ast3.BitXor: "^",
ast3.BitAnd: "&",
ast3.FloorDiv: "//",
}
def from_operator(self, op: ast3.operator) -> str:
op_name = ASTConverter.op_map.get(type(op))
if op_name is None:
raise RuntimeError("Unknown operator " + str(type(op)))
else:
return op_name
comp_op_map: Final[dict[type[AST], str]] = {
ast3.Gt: ">",
ast3.Lt: "<",
ast3.Eq: "==",
ast3.GtE: ">=",
ast3.LtE: "<=",
ast3.NotEq: "!=",
ast3.Is: "is",
ast3.IsNot: "is not",
ast3.In: "in",
ast3.NotIn: "not in",
}
def from_comp_operator(self, op: ast3.cmpop) -> str:
op_name = ASTConverter.comp_op_map.get(type(op))
if op_name is None:
raise RuntimeError("Unknown comparison operator " + str(type(op)))
else:
return op_name
def set_block_lines(self, b: Block, stmts: Sequence[ast3.stmt]) -> None:
first, last = stmts[0], stmts[-1]
b.line = first.lineno
b.column = first.col_offset
b.end_line = getattr(last, "end_lineno", None)
b.end_column = getattr(last, "end_col_offset", None)
if not b.body:
return
new_first = b.body[0]
if isinstance(new_first, (Decorator, OverloadedFuncDef)):
# Decorated function lines are different between Python versions.
# copy the normalization we do for them to block first lines.
b.line = new_first.line
b.column = new_first.column
def as_block(self, stmts: list[ast3.stmt]) -> Block | None:
b = None
if stmts:
b = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
self.set_block_lines(b, stmts)
return b
def as_required_block(
self, stmts: list[ast3.stmt], *, can_strip: bool = False, is_coroutine: bool = False
) -> Block:
assert stmts # must be non-empty
b = Block(
self.fix_function_overloads(
self.translate_stmt_list(stmts, can_strip=can_strip, is_coroutine=is_coroutine)
)
)
self.set_block_lines(b, stmts)
return b
def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
ret: list[Statement] = []
current_overload: list[OverloadPart] = []
current_overload_name: str | None = None
seen_unconditional_func_def = False
last_if_stmt: IfStmt | None = None
last_if_overload: Decorator | FuncDef | OverloadedFuncDef | None = None
last_if_stmt_overload_name: str | None = None
last_if_unknown_truth_value: IfStmt | None = None
skipped_if_stmts: list[IfStmt] = []
for stmt in stmts:
if_overload_name: str | None = None
if_block_with_overload: Block | None = None
if_unknown_truth_value: IfStmt | None = None
if isinstance(stmt, IfStmt) and seen_unconditional_func_def is False:
# Check IfStmt block to determine if function overloads can be merged
if_overload_name = self._check_ifstmt_for_overloads(stmt, current_overload_name)
if if_overload_name is not None:
(if_block_with_overload, if_unknown_truth_value) = (
self._get_executable_if_block_with_overloads(stmt)
)
if (
current_overload_name is not None
and isinstance(stmt, (Decorator, FuncDef))
and stmt.name == current_overload_name
):
if last_if_stmt is not None:
skipped_if_stmts.append(last_if_stmt)
if last_if_overload is not None:
# Last stmt was an IfStmt with same overload name
# Add overloads to current_overload
if isinstance(last_if_overload, OverloadedFuncDef):
current_overload.extend(last_if_overload.items)
else:
current_overload.append(last_if_overload)
last_if_stmt, last_if_overload = None, None
if last_if_unknown_truth_value:
self.fail_merge_overload(last_if_unknown_truth_value)
last_if_unknown_truth_value = None
current_overload.append(stmt)
if isinstance(stmt, FuncDef):
seen_unconditional_func_def = True
elif (
current_overload_name is not None
and isinstance(stmt, IfStmt)
and if_overload_name == current_overload_name
):
# IfStmt only contains stmts relevant to current_overload.
# Check if stmts are reachable and add them to current_overload,
# otherwise skip IfStmt to allow subsequent overload
# or function definitions.
skipped_if_stmts.append(stmt)
if if_block_with_overload is None:
if if_unknown_truth_value is not None:
self.fail_merge_overload(if_unknown_truth_value)
continue
if last_if_overload is not None:
# Last stmt was an IfStmt with same overload name
# Add overloads to current_overload
if isinstance(last_if_overload, OverloadedFuncDef):
current_overload.extend(last_if_overload.items)
else:
current_overload.append(last_if_overload)
last_if_stmt, last_if_overload = None, None
if isinstance(if_block_with_overload.body[-1], OverloadedFuncDef):
skipped_if_stmts.extend(cast(List[IfStmt], if_block_with_overload.body[:-1]))
current_overload.extend(if_block_with_overload.body[-1].items)
else:
current_overload.append(
cast(Union[Decorator, FuncDef], if_block_with_overload.body[0])
)
else:
if last_if_stmt is not None:
ret.append(last_if_stmt)
last_if_stmt_overload_name = current_overload_name
last_if_stmt, last_if_overload = None, None
last_if_unknown_truth_value = None
if current_overload and current_overload_name == last_if_stmt_overload_name:
# Remove last stmt (IfStmt) from ret if the overload names matched
# Only happens if no executable block had been found in IfStmt
popped = ret.pop()
assert isinstance(popped, IfStmt)
skipped_if_stmts.append(popped)
if current_overload and skipped_if_stmts:
# Add bare IfStmt (without overloads) to ret
# Required for mypy to be able to still check conditions
for if_stmt in skipped_if_stmts:
self._strip_contents_from_if_stmt(if_stmt)
ret.append(if_stmt)
skipped_if_stmts = []
if len(current_overload) == 1:
ret.append(current_overload[0])
elif len(current_overload) > 1:
ret.append(OverloadedFuncDef(current_overload))
# If we have multiple decorated functions named "_" next to each, we want to treat
# them as a series of regular FuncDefs instead of one OverloadedFuncDef because
# most of mypy/mypyc assumes that all the functions in an OverloadedFuncDef are
# related, but multiple underscore functions next to each other aren't necessarily
# related
seen_unconditional_func_def = False
if isinstance(stmt, Decorator) and not unnamed_function(stmt.name):
current_overload = [stmt]
current_overload_name = stmt.name
elif isinstance(stmt, IfStmt) and if_overload_name is not None:
current_overload = []
current_overload_name = if_overload_name
last_if_stmt = stmt
last_if_stmt_overload_name = None
if if_block_with_overload is not None:
skipped_if_stmts.extend(
cast(List[IfStmt], if_block_with_overload.body[:-1])
)
last_if_overload = cast(
Union[Decorator, FuncDef, OverloadedFuncDef],
if_block_with_overload.body[-1],
)
last_if_unknown_truth_value = if_unknown_truth_value
else:
current_overload = []
current_overload_name = None
ret.append(stmt)
if current_overload and skipped_if_stmts:
# Add bare IfStmt (without overloads) to ret
# Required for mypy to be able to still check conditions
for if_stmt in skipped_if_stmts:
self._strip_contents_from_if_stmt(if_stmt)
ret.append(if_stmt)
if len(current_overload) == 1:
ret.append(current_overload[0])
elif len(current_overload) > 1:
ret.append(OverloadedFuncDef(current_overload))
elif last_if_overload is not None:
ret.append(last_if_overload)
elif last_if_stmt is not None:
ret.append(last_if_stmt)
return ret
def _check_ifstmt_for_overloads(
self, stmt: IfStmt, current_overload_name: str | None = None
) -> str | None:
"""Check if IfStmt contains only overloads with the same name.
Return overload_name if found, None otherwise.
"""
# Check that block only contains a single Decorator, FuncDef, or OverloadedFuncDef.
# Multiple overloads have already been merged as OverloadedFuncDef.
if not (
len(stmt.body[0].body) == 1
and (
isinstance(stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
or current_overload_name is not None
and isinstance(stmt.body[0].body[0], FuncDef)
)
or len(stmt.body[0].body) > 1
and isinstance(stmt.body[0].body[-1], OverloadedFuncDef)
and all(self._is_stripped_if_stmt(if_stmt) for if_stmt in stmt.body[0].body[:-1])
):
return None
overload_name = cast(
Union[Decorator, FuncDef, OverloadedFuncDef], stmt.body[0].body[-1]
).name
if stmt.else_body is None:
return overload_name
if len(stmt.else_body.body) == 1:
# For elif: else_body contains an IfStmt itself -> do a recursive check.
if (
isinstance(stmt.else_body.body[0], (Decorator, FuncDef, OverloadedFuncDef))
and stmt.else_body.body[0].name == overload_name
):
return overload_name
if (
isinstance(stmt.else_body.body[0], IfStmt)
and self._check_ifstmt_for_overloads(stmt.else_body.body[0], current_overload_name)
== overload_name
):
return overload_name
return None
def _get_executable_if_block_with_overloads(
self, stmt: IfStmt
) -> tuple[Block | None, IfStmt | None]:
"""Return block from IfStmt that will get executed.
Return
0 -> A block if sure that alternative blocks are unreachable.
1 -> An IfStmt if the reachability of it can't be inferred,
i.e. the truth value is unknown.
"""
infer_reachability_of_if_statement(stmt, self.options)
if stmt.else_body is None and stmt.body[0].is_unreachable is True:
# always False condition with no else
return None, None
if (
stmt.else_body is None
or stmt.body[0].is_unreachable is False
and stmt.else_body.is_unreachable is False
):
# The truth value is unknown, thus not conclusive
return None, stmt
if stmt.else_body.is_unreachable is True:
# else_body will be set unreachable if condition is always True
return stmt.body[0], None
if stmt.body[0].is_unreachable is True:
# body will be set unreachable if condition is always False
# else_body can contain an IfStmt itself (for elif) -> do a recursive check
if isinstance(stmt.else_body.body[0], IfStmt):
return self._get_executable_if_block_with_overloads(stmt.else_body.body[0])
return stmt.else_body, None
return None, stmt
def _strip_contents_from_if_stmt(self, stmt: IfStmt) -> None:
"""Remove contents from IfStmt.
Needed to still be able to check the conditions after the contents
have been merged with the surrounding function overloads.
"""
if len(stmt.body) == 1:
stmt.body[0].body = []
if stmt.else_body and len(stmt.else_body.body) == 1:
if isinstance(stmt.else_body.body[0], IfStmt):
self._strip_contents_from_if_stmt(stmt.else_body.body[0])
else:
stmt.else_body.body = []
def _is_stripped_if_stmt(self, stmt: Statement) -> bool:
"""Check stmt to make sure it is a stripped IfStmt.
See also: _strip_contents_from_if_stmt
"""
if not isinstance(stmt, IfStmt):
return False
if not (len(stmt.body) == 1 and len(stmt.body[0].body) == 0):
# Body not empty
return False
if not stmt.else_body or len(stmt.else_body.body) == 0:
# No or empty else_body
return True
# For elif, IfStmt are stored recursively in else_body
return self._is_stripped_if_stmt(stmt.else_body.body[0])
def translate_module_id(self, id: str) -> str:
"""Return the actual, internal module id for a source text id."""
if id == self.options.custom_typing_module:
return "typing"
return id
def visit_Module(self, mod: ast3.Module) -> MypyFile:
self.type_ignores = {}
for ti in mod.type_ignores:
parsed = parse_type_ignore_tag(ti.tag)
if parsed is not None:
self.type_ignores[ti.lineno] = parsed
else:
self.fail(message_registry.INVALID_TYPE_IGNORE, ti.lineno, -1, blocker=False)
body = self.fix_function_overloads(self.translate_stmt_list(mod.body, ismodule=True))
ret = MypyFile(body, self.imports, False, ignored_lines=self.type_ignores)
ret.is_stub = self.is_stub
ret.path = self.path
return ret
# --- stmt ---
# FunctionDef(identifier name, arguments args,
# stmt* body, expr* decorator_list, expr? returns, string? type_comment)
# arguments = (arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults,
# arg? kwarg, expr* defaults)
def visit_FunctionDef(self, n: ast3.FunctionDef) -> FuncDef | Decorator:
return self.do_func_def(n)
# AsyncFunctionDef(identifier name, arguments args,
# stmt* body, expr* decorator_list, expr? returns, string? type_comment)
def visit_AsyncFunctionDef(self, n: ast3.AsyncFunctionDef) -> FuncDef | Decorator:
return self.do_func_def(n, is_coroutine=True)
def do_func_def(
self, n: ast3.FunctionDef | ast3.AsyncFunctionDef, is_coroutine: bool = False
) -> FuncDef | Decorator:
"""Helper shared between visit_FunctionDef and visit_AsyncFunctionDef."""
self.class_and_function_stack.append("D")
no_type_check = bool(
n.decorator_list and any(is_no_type_check_decorator(d) for d in n.decorator_list)
)
lineno = n.lineno
args = self.transform_args(n.args, lineno, no_type_check=no_type_check)
if special_function_elide_names(n.name):
for arg in args:
arg.pos_only = True
arg_kinds = [arg.kind for arg in args]
arg_names = [None if arg.pos_only else arg.variable.name for arg in args]
# Type parameters, if using new syntax for generics (PEP 695)
explicit_type_params: list[TypeParam] | None = None
arg_types: list[Type | None] = []
if no_type_check:
arg_types = [None] * len(args)
return_type = None
elif n.type_comment is not None:
try:
func_type_ast = ast3_parse(n.type_comment, "<func_type>", "func_type")
assert isinstance(func_type_ast, FunctionType)
# for ellipsis arg
if (
len(func_type_ast.argtypes) == 1
and isinstance(func_type_ast.argtypes[0], Constant)
and func_type_ast.argtypes[0].value is Ellipsis
):
if n.returns:
# PEP 484 disallows both type annotations and type comments
self.fail(message_registry.DUPLICATE_TYPE_SIGNATURES, lineno, n.col_offset)
arg_types = [
(
a.type_annotation
if a.type_annotation is not None
else AnyType(TypeOfAny.unannotated)
)
for a in args
]
else:
# PEP 484 disallows both type annotations and type comments
if n.returns or any(a.type_annotation is not None for a in args):
self.fail(message_registry.DUPLICATE_TYPE_SIGNATURES, lineno, n.col_offset)
translated_args: list[Type] = TypeConverter(
self.errors, line=lineno, override_column=n.col_offset
).translate_expr_list(func_type_ast.argtypes)
# Use a cast to work around `list` invariance
arg_types = cast(List[Optional[Type]], translated_args)
return_type = TypeConverter(self.errors, line=lineno).visit(func_type_ast.returns)
# add implicit self type
in_method_scope = self.class_and_function_stack[-2:] == ["C", "D"]
if in_method_scope and len(arg_types) < len(args):
arg_types.insert(0, AnyType(TypeOfAny.special_form))
except SyntaxError:
stripped_type = n.type_comment.split("#", 2)[0].strip()
err_msg = message_registry.TYPE_COMMENT_SYNTAX_ERROR_VALUE.format(stripped_type)
self.fail(err_msg, lineno, n.col_offset)
if n.type_comment and n.type_comment[0] not in ["(", "#"]:
self.note(
"Suggestion: wrap argument types in parentheses", lineno, n.col_offset
)
arg_types = [AnyType(TypeOfAny.from_error)] * len(args)
return_type = AnyType(TypeOfAny.from_error)
else:
if sys.version_info >= (3, 12) and n.type_params:
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
explicit_type_params = self.translate_type_params(n.type_params)
else:
self.fail(
ErrorMessage(
"PEP 695 generics are not yet supported", code=codes.VALID_TYPE
),
n.type_params[0].lineno,
n.type_params[0].col_offset,
blocker=False,
)
arg_types = [a.type_annotation for a in args]
return_type = TypeConverter(
self.errors, line=n.returns.lineno if n.returns else lineno
).visit(n.returns)
for arg, arg_type in zip(args, arg_types):
self.set_type_optional(arg_type, arg.initializer)
func_type = None
if any(arg_types) or return_type:
if len(arg_types) != 1 and any(isinstance(t, EllipsisType) for t in arg_types):
self.fail(message_registry.ELLIPSIS_WITH_OTHER_TYPEARGS, lineno, n.col_offset)
elif len(arg_types) > len(arg_kinds):
self.fail(
message_registry.TYPE_SIGNATURE_TOO_MANY_ARGS,
lineno,
n.col_offset,
blocker=False,
)
elif len(arg_types) < len(arg_kinds):
self.fail(
message_registry.TYPE_SIGNATURE_TOO_FEW_ARGS,
lineno,
n.col_offset,
blocker=False,
)
else:
func_type = CallableType(
[a if a is not None else AnyType(TypeOfAny.unannotated) for a in arg_types],
arg_kinds,
arg_names,
return_type if return_type is not None else AnyType(TypeOfAny.unannotated),
_dummy_fallback,
)
# End position is always the same.
end_line = getattr(n, "end_lineno", None)