-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathanalysisError.ml
4104 lines (4005 loc) · 163 KB
/
analysisError.ml
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
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
open Core
open Ast
open Pyre
open Statement
module Type = struct
include Type
let compare = Type.namespace_insensitive_compare
end
(* The `name` field conflicts with that defined in incompatible_type. *)
type missing_annotation = {
name: Reference.t;
annotation: Type.t option;
given_annotation: Type.t option;
evidence_locations: Location.WithPath.t list;
thrown_at_source: bool;
}
[@@deriving compare, eq, sexp, show, hash]
type class_kind =
| Class
| Enumeration
| Protocol of Reference.t
| Abstract of Reference.t
[@@deriving compare, eq, sexp, show, hash]
type invalid_class_instantiation =
| AbstractClassInstantiation of {
class_name: Reference.t;
abstract_methods: string list;
}
| ProtocolInstantiation of Reference.t
[@@deriving compare, eq, sexp, show, hash]
type module_reference =
| ExplicitModule of SourcePath.t
| ImplicitModule of Reference.t
[@@deriving compare, eq, sexp, show, hash]
type origin =
| Class of {
class_type: Type.t;
parent_source_path: SourcePath.t option;
}
| Module of module_reference
and analysis_failure =
| UnexpectedUndefinedType of string
| FixpointThresholdReached of { define: Reference.t }
and mismatch = {
actual: Type.t;
expected: Type.t;
due_to_invariance: bool;
}
and annotation_and_parent = {
parent: Identifier.t;
annotation: Type.t;
}
and typed_dictionary_field_mismatch =
| RequirednessMismatch of {
required_field_class: Identifier.t;
non_required_field_class: Identifier.t;
field_name: Identifier.t;
}
| TypeMismatch of {
field_name: Identifier.t;
annotation_and_parent1: annotation_and_parent;
annotation_and_parent2: annotation_and_parent;
}
and typed_dictionary_initialization_mismatch =
| MissingRequiredField of {
field_name: Identifier.t;
class_name: Identifier.t;
}
| FieldTypeMismatch of {
field_name: Identifier.t;
class_name: Identifier.t;
expected_type: Type.t;
actual_type: Type.t;
}
| UndefinedField of {
field_name: Identifier.t;
class_name: Identifier.t;
}
and incompatible_type = {
name: Reference.t;
mismatch: mismatch;
}
and invalid_argument =
| Keyword of {
expression: Expression.t option;
annotation: Type.t;
require_string_keys: bool;
}
| ConcreteVariable of {
expression: Expression.t option;
annotation: Type.t;
}
| TupleVariadicVariable of {
variable: Type.OrderedTypes.t;
mismatch: SignatureSelectionTypes.mismatch_with_tuple_variadic_type_variable;
}
and precondition_mismatch =
| Found of mismatch
| NotFound of Type.t Type.Callable.Parameter.t
and override =
| StrengthenedPrecondition of precondition_mismatch
| WeakenedPostcondition of mismatch
and unpack_problem =
| UnacceptableType of Type.t
| CountMismatch of int
and type_variable_origin =
| ClassToplevel
| Define
| Toplevel
and type_variance_origin =
| Parameter
| Return
| Inheritance of Type.t
and illegal_action_on_incomplete_type =
| Naming
| Calling
| AttributeAccess of Identifier.t
and override_kind =
| Method
| Attribute
and invalid_inheritance =
| ClassName of Identifier.t
| NonMethodFunction of Identifier.t
| UninheritableType of {
annotation: Type.t;
is_parent_class_typed_dictionary: bool;
}
| TypedDictionarySuperclassCollision of typed_dictionary_field_mismatch
and invalid_override_kind =
| Final
| StaticSuper
| StaticOverride
and invalid_assignment_kind =
| FinalAttribute of Reference.t
| ClassVariable of {
class_variable: Identifier.t;
class_name: Identifier.t;
}
| ReadOnly of Reference.t
and invalid_type_kind =
| FinalNested of Type.t
| FinalParameter of Identifier.t
| InvalidType of {
annotation: Type.t;
expected: string;
}
| NestedAlias of Identifier.t
| NestedTypeVariables of Type.Variable.t
| SingleExplicit of Type.t
| InvalidLiteral of Reference.t
and unawaited_awaitable = {
references: Reference.t list;
expression: Expression.t;
}
and undefined_import =
| UndefinedModule of Reference.t
| UndefinedName of {
from: module_reference;
name: Identifier.t;
}
and incompatible_overload_kind =
| ReturnType of {
implementation_annotation: Type.t;
name: Reference.t;
overload_annotation: Type.t;
}
| Unmatchable of {
name: Reference.t;
matching_overload: Type.t Type.Callable.overload;
unmatched_location: Location.t;
}
| Parameters of {
name: Reference.t;
location: Location.t;
}
| DifferingDecorators
| MisplacedOverloadDecorator
and polymorphism_base_class =
| GenericBase
| ProtocolBase
and unsupported_operand_kind =
| Binary of {
operator_name: Identifier.t;
left_operand: Type.t;
right_operand: Type.t;
}
| Unary of {
operator_name: Identifier.t;
operand: Type.t;
}
and illegal_annotation_target_kind =
| InvalidExpression
| Reassignment
and tuple_concatenation_problem =
| MultipleVariadics of { variadic_expressions: Expression.t list }
| UnpackingNonIterable of { annotation: Type.t }
[@@deriving compare, eq, sexp, show, hash]
type invalid_decoration =
| CouldNotResolve of Expression.t
| CouldNotResolveArgument of {
name: Reference.t;
argument: Expression.t;
}
| NonCallableDecoratorFactory of {
name: Reference.t;
annotation: Type.t;
}
| NonCallableDecorator of {
name: Reference.t;
has_arguments: bool;
annotation: Type.t;
}
| DecoratorFactoryFailedToApply of {
name: Reference.t;
reason: kind option;
}
| ApplicationFailed of {
name: Reference.t;
has_arguments: bool;
reason: kind option;
}
| SetterNameMismatch of {
name: Reference.t;
actual: string;
expected: string;
}
and kind =
| AnalysisFailure of analysis_failure
| ParserFailure of string
| IllegalAnnotationTarget of {
target: Expression.t;
kind: illegal_annotation_target_kind;
}
| IncompatibleAsyncGeneratorReturnType of Type.t
| IncompatibleAttributeType of {
parent: Type.t;
incompatible_type: incompatible_type;
}
| IncompatibleAwaitableType of Type.t
| IncompatibleConstructorAnnotation of Type.t
| IncompatibleParameterType of {
name: Identifier.t option;
position: int;
callee: Reference.t option;
mismatch: mismatch;
}
| IncompatibleReturnType of {
mismatch: mismatch;
is_implicit: bool;
is_unimplemented: bool;
define_location: Location.t;
}
| IncompatibleVariableType of {
incompatible_type: incompatible_type;
declare_location: Location.WithPath.t;
}
| IncompatibleOverload of incompatible_overload_kind
| IncompleteType of {
target: Expression.t;
annotation: Type.t;
attempted_action: illegal_action_on_incomplete_type;
}
| InconsistentOverride of {
overridden_method: Identifier.t;
parent: Reference.t;
override: override;
override_kind: override_kind;
}
| InvalidArgument of invalid_argument
| InvalidClassInstantiation of invalid_class_instantiation
| InvalidDecoration of invalid_decoration
| InvalidException of {
expression: Expression.t;
annotation: Type.t;
}
| InvalidMethodSignature of {
annotation: Type.t option;
name: Identifier.t;
}
| InvalidType of invalid_type_kind
| InvalidTypeParameters of AttributeResolution.type_parameters_mismatch
| InvalidTypeVariable of {
annotation: Type.Variable.t;
origin: type_variable_origin;
}
| InvalidTypeVariance of {
annotation: Type.t;
origin: type_variance_origin;
}
| InvalidInheritance of invalid_inheritance
| InvalidOverride of {
parent: Identifier.t;
decorator: invalid_override_kind;
}
| InvalidAssignment of invalid_assignment_kind
| MissingArgument of {
callee: Reference.t option;
parameter: SignatureSelectionTypes.missing_argument;
}
| MissingAttributeAnnotation of {
parent: Type.t;
missing_annotation: missing_annotation;
}
| MissingCaptureAnnotation of Identifier.t
| MissingGlobalAnnotation of missing_annotation
| MissingOverloadImplementation of Reference.t
| MissingParameterAnnotation of missing_annotation
| MissingReturnAnnotation of missing_annotation
| MutuallyRecursiveTypeVariables of Reference.t option
| NotCallable of Type.t
| PrivateProtocolProperty of {
name: Identifier.t;
parent: Type.t;
}
| ProhibitedAny of {
is_type_alias: bool;
missing_annotation: missing_annotation;
}
| RedefinedClass of {
current_class: Reference.t;
shadowed_class: Reference.t;
is_shadowed_class_imported: bool;
}
| RedundantCast of Type.t
| RevealedType of {
expression: Expression.t;
annotation: Annotation.t;
qualify: bool;
}
| UnsafeCast of {
expression: Expression.t;
annotation: Type.t;
}
| TooManyArguments of {
callee: Reference.t option;
expected: int;
provided: int;
}
| Top
| TypedDictionaryAccessWithNonLiteral of Identifier.t list
| TypedDictionaryKeyNotFound of {
typed_dictionary_name: Identifier.t;
missing_key: string;
}
| UnboundName of Identifier.t
| UninitializedLocal of Identifier.t
| UndefinedAttribute of {
attribute: Identifier.t;
origin: origin;
}
| UndefinedImport of undefined_import
| UndefinedType of Type.t
| UnexpectedKeyword of {
name: Identifier.t;
callee: Reference.t option;
}
| UninitializedAttribute of {
name: Identifier.t;
parent: Type.t;
mismatch: mismatch;
kind: class_kind;
}
| Unpack of {
expected_count: int;
unpack_problem: unpack_problem;
}
| UnsupportedOperand of unsupported_operand_kind
| UnusedIgnore of int list
| UnusedLocalMode of {
unused_mode: Source.local_mode Node.t;
actual_mode: Source.local_mode Node.t;
}
| TypedDictionaryInvalidOperation of {
typed_dictionary_name: Identifier.t;
field_name: Identifier.t;
method_name: Identifier.t;
mismatch: mismatch;
}
| TypedDictionaryInitializationError of typed_dictionary_initialization_mismatch
| DuplicateTypeVariables of {
variable: Type.Variable.t;
base: polymorphism_base_class;
}
| TupleConcatenationError of tuple_concatenation_problem
(* Additional errors. *)
(* TODO(T38384376): split this into a separate module. *)
| DeadStore of Identifier.t
| Deobfuscation of Source.t
| UnawaitedAwaitable of unawaited_awaitable
(* Errors from type operators *)
| BroadcastError of {
expression: Expression.t;
left: Type.t;
right: Type.t;
}
[@@deriving compare, eq, sexp, show, hash]
let code_of_kind = function
| RevealedType _ -> -1
| UnusedIgnore _ -> 0
| Top -> 1
| MissingParameterAnnotation _ -> 2
| MissingReturnAnnotation _ -> 3
| MissingAttributeAnnotation _ -> 4
| MissingGlobalAnnotation _ -> 5
| IncompatibleParameterType _ -> 6
| IncompatibleReturnType _ -> 7
| IncompatibleAttributeType _ -> 8
| IncompatibleVariableType _ -> 9
| UnboundName _ -> 10
| UndefinedType _ -> 11
| IncompatibleAwaitableType _ -> 12
| UninitializedAttribute _ -> 13
| InconsistentOverride { override; _ } -> (
match override with
| StrengthenedPrecondition _ -> 14
| WeakenedPostcondition _ -> 15)
| UndefinedAttribute _ -> 16
| IncompatibleConstructorAnnotation _ -> 17
| TooManyArguments _ -> 19
| MissingArgument _ -> 20
| UndefinedImport _ -> 21
| RedundantCast _ -> 22
| Unpack _ -> 23
| InvalidTypeParameters _ -> 24
| TypedDictionaryAccessWithNonLiteral _ -> 26
| TypedDictionaryKeyNotFound _ -> 27
| UnexpectedKeyword _ -> 28
| NotCallable _ -> 29
| AnalysisFailure _ -> 30
| InvalidType _ -> 31
| InvalidArgument _ -> 32
| ProhibitedAny _ -> 33
| InvalidTypeVariable _ -> 34
| IllegalAnnotationTarget _ -> 35
| MutuallyRecursiveTypeVariables _ -> 36
| IncompleteType _ -> 37
| InvalidInheritance _ -> 39
| InvalidOverride _ -> 40
| InvalidAssignment _ -> 41
| MissingOverloadImplementation _ -> 42
| IncompatibleOverload _ -> 43
| InvalidClassInstantiation _ -> 45
| InvalidTypeVariance _ -> 46
| InvalidMethodSignature _ -> 47
| InvalidException _ -> 48
| UnsafeCast _ -> 49
| RedefinedClass _ -> 50
| UnusedLocalMode _ -> 51
| PrivateProtocolProperty _ -> 52
| MissingCaptureAnnotation _ -> 53
| TypedDictionaryInvalidOperation _ -> 54
| TypedDictionaryInitializationError _ -> 55
| InvalidDecoration _ -> 56
| IncompatibleAsyncGeneratorReturnType _ -> 57
| UnsupportedOperand _ -> 58
| DuplicateTypeVariables _ -> 59
| TupleConcatenationError _ -> 60
| UninitializedLocal _ -> 61
| ParserFailure _ -> 404
(* Additional errors. *)
| UnawaitedAwaitable _ -> 1001
| Deobfuscation _ -> 1002
| DeadStore _ -> 1003
(* Errors from type operators *)
| BroadcastError _ -> 2001
let name_of_kind = function
| AnalysisFailure _ -> "Analysis failure"
| BroadcastError _ -> "Broadcast error"
| DuplicateTypeVariables _ -> "Duplicate type variables"
| ParserFailure _ -> "Parsing failure"
| DeadStore _ -> "Dead store"
| Deobfuscation _ -> "Deobfuscation"
| IllegalAnnotationTarget _ -> "Illegal annotation target"
| IncompatibleAsyncGeneratorReturnType _ -> "Incompatible async generator return type"
| IncompatibleAttributeType _ -> "Incompatible attribute type"
| IncompatibleAwaitableType _ -> "Incompatible awaitable type"
| IncompatibleConstructorAnnotation _ -> "Incompatible constructor annotation"
| IncompatibleParameterType _ -> "Incompatible parameter type"
| IncompatibleReturnType _ -> "Incompatible return type"
| IncompatibleVariableType _ -> "Incompatible variable type"
| InconsistentOverride _ -> "Inconsistent override"
| IncompatibleOverload _ -> "Incompatible overload"
| IncompleteType _ -> "Incomplete type"
| InvalidArgument _ -> "Invalid argument"
| InvalidMethodSignature _ -> "Invalid method signature"
| InvalidClassInstantiation _ -> "Invalid class instantiation"
| InvalidDecoration _ -> "Invalid decoration"
| InvalidException _ -> "Invalid Exception"
| InvalidType _ -> "Invalid type"
| InvalidTypeParameters _ -> "Invalid type parameters"
| InvalidTypeVariable _ -> "Invalid type variable"
| InvalidTypeVariance _ -> "Invalid type variance"
| InvalidInheritance _ -> "Invalid inheritance"
| InvalidOverride _ -> "Invalid override"
| InvalidAssignment _ -> "Invalid assignment"
| MissingArgument _ -> "Missing argument"
| MissingAttributeAnnotation _ -> "Missing attribute annotation"
| MissingCaptureAnnotation _ -> "Missing annotation for captured variable"
| MissingGlobalAnnotation _ -> "Missing global annotation"
| MissingOverloadImplementation _ -> "Missing overload implementation"
| MissingParameterAnnotation _ -> "Missing parameter annotation"
| MissingReturnAnnotation _ -> "Missing return annotation"
| MutuallyRecursiveTypeVariables _ -> "Mutually recursive type variables"
| NotCallable _ -> "Call error"
| PrivateProtocolProperty _ -> "Private protocol property"
| ProhibitedAny _ -> "Prohibited any"
| RedefinedClass _ -> "Redefined class"
| RedundantCast _ -> "Redundant cast"
| RevealedType _ -> "Revealed type"
| TooManyArguments _ -> "Too many arguments"
| Top -> "Undefined error"
| TypedDictionaryAccessWithNonLiteral _ -> "TypedDict accessed with a non-literal"
| TypedDictionaryInitializationError _ -> "TypedDict initialization error"
| TypedDictionaryInvalidOperation _ -> "Invalid TypedDict operation"
| TypedDictionaryKeyNotFound _ -> "TypedDict accessed with a missing key"
| UnawaitedAwaitable _ -> "Unawaited awaitable"
| UnboundName _ -> "Unbound name"
| UndefinedAttribute _ -> "Undefined attribute"
| UndefinedImport _ -> "Undefined import"
| UndefinedType _ -> "Undefined or invalid type"
| UnexpectedKeyword _ -> "Unexpected keyword"
| UninitializedAttribute _ -> "Uninitialized attribute"
| UninitializedLocal _ -> "Uninitialized local"
| Unpack _ -> "Unable to unpack"
| UnsafeCast _ -> "Unsafe cast"
| UnsupportedOperand _ -> "Unsupported operand"
| UnusedIgnore _ -> "Unused ignore"
| UnusedLocalMode _ -> "Unused local mode"
| TupleConcatenationError _ -> "Unable to concatenate tuple"
let weaken_literals kind =
let weaken_mismatch { actual; expected; due_to_invariance } =
let actual =
let weakened_actual = Type.weaken_literals actual in
if Type.contains_literal expected || Type.equal weakened_actual expected then
actual
else
weakened_actual
in
{ actual; expected; due_to_invariance }
in
(* This is necessary because the `int.__add__` stub now takes type variables, which leads to
confusing errors *)
let weaken_int_variable annotation =
let constraints = function
| Type.Variable
{
Type.Record.Variable.RecordUnary.constraints =
Type.Record.Variable.Bound (Type.Primitive "int");
_;
} ->
Some Type.integer
| _ -> None
in
Type.instantiate ~constraints annotation
in
let weaken_missing_annotation = function
| { given_annotation = Some given; _ } as missing when Type.contains_literal given -> missing
| { annotation = Some annotation; _ } as missing ->
{ missing with annotation = Some (weaken_int_variable (Type.weaken_literals annotation)) }
| missing -> missing
in
match kind with
| IncompatibleAttributeType
({ incompatible_type = { mismatch; _ } as incompatible; _ } as attribute) ->
IncompatibleAttributeType
{
attribute with
incompatible_type = { incompatible with mismatch = weaken_mismatch mismatch };
}
| IncompatibleVariableType
({ incompatible_type = { mismatch; _ } as incompatible; _ } as variable) ->
IncompatibleVariableType
{
variable with
incompatible_type = { incompatible with mismatch = weaken_mismatch mismatch };
}
| InconsistentOverride ({ override = WeakenedPostcondition mismatch; _ } as inconsistent) ->
InconsistentOverride
{ inconsistent with override = WeakenedPostcondition (weaken_mismatch mismatch) }
| InconsistentOverride
({ override = StrengthenedPrecondition (Found mismatch); _ } as inconsistent) ->
InconsistentOverride
{ inconsistent with override = StrengthenedPrecondition (Found (weaken_mismatch mismatch)) }
| IncompatibleParameterType ({ mismatch; _ } as incompatible) ->
IncompatibleParameterType { incompatible with mismatch = weaken_mismatch mismatch }
| IncompatibleReturnType ({ mismatch; _ } as incompatible) ->
IncompatibleReturnType { incompatible with mismatch = weaken_mismatch mismatch }
| UninitializedAttribute ({ mismatch; _ } as uninitialized) ->
UninitializedAttribute { uninitialized with mismatch = weaken_mismatch mismatch }
| MissingAttributeAnnotation { parent; missing_annotation } ->
MissingAttributeAnnotation
{ parent; missing_annotation = weaken_missing_annotation missing_annotation }
| MissingGlobalAnnotation missing_annotation ->
MissingGlobalAnnotation (weaken_missing_annotation missing_annotation)
| MissingParameterAnnotation missing_annotation ->
MissingParameterAnnotation (weaken_missing_annotation missing_annotation)
| MissingReturnAnnotation missing_annotation ->
MissingReturnAnnotation (weaken_missing_annotation missing_annotation)
| ProhibitedAny { is_type_alias; missing_annotation } ->
ProhibitedAny
{ is_type_alias; missing_annotation = weaken_missing_annotation missing_annotation }
| UnsupportedOperand (Binary { operator_name; left_operand; right_operand }) ->
UnsupportedOperand
(Binary
{
operator_name;
left_operand = Type.weaken_literals left_operand;
right_operand = Type.weaken_literals right_operand;
})
| UnsupportedOperand (Unary { operator_name; operand }) ->
UnsupportedOperand (Unary { operator_name; operand = Type.weaken_literals operand })
| Unpack { expected_count; unpack_problem = UnacceptableType annotation } ->
Unpack { expected_count; unpack_problem = UnacceptableType (Type.weaken_literals annotation) }
| IncompatibleAwaitableType annotation ->
IncompatibleAwaitableType (Type.weaken_literals annotation)
| NotCallable annotation -> NotCallable (Type.weaken_literals annotation)
| TypedDictionaryInvalidOperation ({ mismatch; _ } as record) ->
TypedDictionaryInvalidOperation { record with mismatch = weaken_mismatch mismatch }
| TypedDictionaryInitializationError mismatch ->
let mismatch =
match mismatch with
| FieldTypeMismatch ({ expected_type; actual_type; _ } as field_record) ->
FieldTypeMismatch
{
field_record with
expected_type = Type.weaken_literals expected_type;
actual_type = Type.weaken_literals actual_type;
}
| _ -> mismatch
in
TypedDictionaryInitializationError mismatch
| _ -> kind
let rec messages ~concise ~signature location kind =
let {
Location.WithPath.start = { Location.line = start_line; _ };
stop = { Location.line = stop_line; _ };
_;
}
=
location
in
let { Node.value = { Define.Signature.name = define_name; _ }; location = define_location } =
signature
in
let show_sanitized_expression expression =
Ast.Transform.sanitize_expression expression |> Expression.show
in
let show_sanitized_optional_expression expression =
expression >>| show_sanitized_expression >>| Format.sprintf " `%s`" |> Option.value ~default:""
in
let ordinal number =
let suffix =
if number % 10 = 1 && number % 100 <> 11 then
"st"
else if number % 10 = 2 && number % 100 <> 12 then
"nd"
else if number % 10 = 3 && number % 100 <> 13 then
"rd"
else
"th"
in
string_of_int number ^ suffix
in
let invariance_message =
"See https://pyre-check.org/docs/errors#covariance-and-contravariance"
^ " for mutable container errors."
in
let pp_type = if concise then Type.pp_concise else Type.pp in
let pp_reference format reference =
if concise then
Reference.last reference |> Reference.create |> Reference.pp_sanitized format
else
Reference.pp_sanitized format reference
in
let pp_identifier = Identifier.pp_sanitized in
let kind = weaken_literals kind in
match kind with
| AnalysisFailure (UnexpectedUndefinedType annotation) when concise ->
[Format.asprintf "Terminating analysis - type `%s` not defined." annotation]
| AnalysisFailure (UnexpectedUndefinedType annotation) ->
[Format.asprintf "Terminating analysis because type `%s` is not defined." annotation]
| AnalysisFailure (FixpointThresholdReached { define }) when concise ->
[
Format.asprintf
"Pyre gave up inferring some types - function `%a` was too complex."
pp_reference
define;
]
| AnalysisFailure (FixpointThresholdReached { define }) ->
[
Format.asprintf
"Pyre gave up inferring types for some variables because function `%a` was too complex."
pp_reference
define;
"Please simplify the function by factoring out some if-statements or for-loops.";
]
| BroadcastError { expression; left; right } ->
[
Format.asprintf
"Broadcast error at expression `%s`; types `%a` and `%a` cannot be broadcasted together."
(show_sanitized_expression expression)
pp_type
left
pp_type
right;
]
| ParserFailure message -> [message]
| DeadStore name -> [Format.asprintf "Value assigned to `%a` is never used." pp_identifier name]
| Deobfuscation source -> [Format.asprintf "\n%a" Source.pp source]
| IllegalAnnotationTarget _ when concise -> ["Target cannot be annotated."]
| IllegalAnnotationTarget { target; kind } ->
let reason =
match kind with
| InvalidExpression -> ""
| Reassignment -> " after it is first declared"
in
[
Format.asprintf
"Target `%s` cannot be annotated%s."
(show_sanitized_expression target)
reason;
]
| IncompleteType { target; annotation; attempted_action } ->
let inferred =
match annotation with
| Type.Variable variable when Type.Variable.Unary.is_escaped_and_free variable -> ""
| _ -> Format.asprintf "`%a` " pp_type annotation
in
let consequence =
match attempted_action with
| Naming -> "add an explicit annotation."
| Calling ->
"cannot be called. "
^ "Separate the expression into an assignment and give it an explicit annotation."
| AttributeAccess attribute ->
Format.asprintf
"so attribute `%s` cannot be accessed. Separate the expression into an assignment \
and give it an explicit annotation."
attribute
in
[
Format.asprintf
"Type %sinferred for `%s` is incomplete, %s"
inferred
(show_sanitized_expression target)
consequence;
]
| IncompatibleAsyncGeneratorReturnType annotation ->
[
Format.asprintf
"Expected return annotation to be AsyncGenerator or a superclass but got `%a`."
pp_type
annotation;
]
| IncompatibleAwaitableType actual ->
[Format.asprintf "Expected an awaitable but got `%a`." pp_type actual]
| IncompatibleOverload kind -> (
match kind with
| ReturnType { implementation_annotation; name; overload_annotation } ->
[
Format.asprintf
"The return type of overloaded function `%a` (`%a`) is incompatible with the return \
type of the implementation (`%a`)."
pp_reference
name
pp_type
overload_annotation
pp_type
implementation_annotation;
]
| Unmatchable { name; _ } when concise ->
[
Format.asprintf
"Signature of overloaded function `%a` will never be matched."
pp_reference
name;
]
| Unmatchable { name; matching_overload; unmatched_location } ->
[
Format.asprintf
"The overloaded function `%a` on line %d will never be matched. The signature `%s` \
is the same or broader."
pp_reference
name
(Location.line unmatched_location)
(Type.show_concise
(Type.Callable
{ implementation = matching_overload; kind = Anonymous; overloads = [] }));
]
| Parameters { name; location } ->
[
Format.asprintf
"The implementation of `%a` does not accept all possible arguments of overload \
defined on line `%d`."
pp_reference
name
(Location.line location);
]
| DifferingDecorators ->
["This definition does not have the same decorators as the preceding overload(s)."]
| MisplacedOverloadDecorator ->
["The @overload decorator must be the topmost decorator if present."])
| IncompatibleParameterType
{ name; position; callee; mismatch = { actual; expected; due_to_invariance; _ } } -> (
let trace =
if due_to_invariance then
[Format.asprintf "This call might modify the type of the parameter."; invariance_message]
else
[]
in
let target =
let parameter =
match name with
| Some name -> Format.asprintf "parameter `%a`" pp_identifier name
| _ -> "positional only parameter"
in
let callee =
match callee with
| Some callee -> Format.asprintf "call `%a`" pp_reference callee
| _ -> "anonymous call"
in
if concise then
Format.asprintf "%s param" (ordinal position)
else
Format.asprintf "%s %s to %s" (ordinal position) parameter callee
in
match Option.map ~f:Reference.as_list callee with
| Some ["int"; "__add__"]
| Some ["int"; "__sub__"]
| Some ["int"; "__mul__"]
| Some ["int"; "__floordiv__"] ->
Format.asprintf "Expected `int` for %s but got `%a`." target pp_type actual :: trace
| _ ->
Format.asprintf
"Expected `%a` for %s but got `%a`."
pp_type
expected
target
pp_type
actual
:: trace)
| IncompatibleConstructorAnnotation _ when concise -> ["`__init__` should return `None`."]
| IncompatibleConstructorAnnotation annotation ->
[
Format.asprintf
"`__init__` is annotated as returning `%a`, but it should return `None`."
pp_type
annotation;
]
| IncompatibleReturnType { mismatch = { actual; expected; due_to_invariance; _ }; is_implicit; _ }
->
let trace =
Format.asprintf
"Type `%a` expected on line %d, specified on line %d.%s"
pp_type
expected
stop_line
define_location.Location.start.Location.line
(if due_to_invariance then " " ^ invariance_message else "")
in
let message =
if is_implicit then
Format.asprintf "Expected `%a` but got implicit return value of `None`." pp_type expected
else
Format.asprintf "Expected `%a` but got `%a`." pp_type expected pp_type actual
in
[message; trace]
| IncompatibleAttributeType
{
parent;
incompatible_type = { name; mismatch = { actual; expected; due_to_invariance; _ } };
} ->
let message =
if concise then
Format.asprintf "Attribute has type `%a`; used as `%a`." pp_type expected pp_type actual
else
Format.asprintf
"Attribute `%a` declared in class `%a` has type `%a` but is used as type `%a`."
pp_reference
name
pp_type
parent
pp_type
expected
pp_type
actual
in
let trace =
if due_to_invariance then
[invariance_message]
else
[]
in
message :: trace
| IncompatibleVariableType
{ incompatible_type = { name; mismatch = { actual; expected; due_to_invariance; _ }; _ }; _ }
->
let message =
if Type.is_tuple expected && not (Type.is_tuple actual) then
Format.asprintf "Unable to unpack `%a`, expected a tuple." pp_type actual
else if concise then
Format.asprintf
"%a has type `%a`; used as `%a`."
pp_reference
name
pp_type
expected
pp_type
actual
else
Format.asprintf
"%a is declared to have type `%a` but is used as type `%a`."
pp_reference
name
pp_type
expected
pp_type
actual
in
let trace =
if due_to_invariance then
if Type.equal (Type.weaken_literals actual) expected then
[
invariance_message;
"Hint: To avoid this error, you may need to use explicit type parameters in your \
constructor: e.g., `Foo[str](\"hello\")` instead of `Foo(\"hello\")`.";
]
else
[invariance_message]
else
[]
in
message :: trace
| InconsistentOverride { parent; override; override_kind; overridden_method } ->
let kind =
match override_kind with
| Method -> "method"
| Attribute -> "attribute"
in
let define_name =
match override_kind with
| Method -> define_name
| Attribute -> Reference.create overridden_method
in
let detail =
match override with
| WeakenedPostcondition { actual; expected; due_to_invariance; _ } ->
if due_to_invariance then
invariance_message
else if equal_override_kind override_kind Attribute then
Format.asprintf
"Type `%a` is not a subtype of the overridden attribute `%a`."