Skip to content

Commit 604f726

Browse files
grievejiafacebook-github-bot
authored andcommitted
Inline Decorator.t in Invalid decorator error
Reviewed By: kbansal Differential Revision: D31865846 fbshipit-source-id: d5d25be12f8d4a8bc60cc70cd5ea967e20e8783f
1 parent a6d22bd commit 604f726

File tree

4 files changed

+105
-86
lines changed

4 files changed

+105
-86
lines changed

source/analysis/analysisError.ml

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,32 @@ and tuple_concatenation_problem =
235235
| UnpackingNonIterable of { annotation: Type.t }
236236
[@@deriving compare, eq, sexp, show, hash]
237237

238-
type invalid_decoration = {
239-
decorator: Decorator.t;
240-
reason: invalid_decoration_reason;
241-
}
242-
243-
and invalid_decoration_reason =
244-
| CouldNotResolve
245-
| CouldNotResolveArgument of Expression.t
246-
| NonCallableDecoratorFactory of Type.t
247-
| NonCallableDecorator of Type.t
248-
| DecoratorFactoryFailedToApply of kind option
249-
| ApplicationFailed of kind option
238+
type invalid_decoration =
239+
| CouldNotResolve of Expression.t
240+
| CouldNotResolveArgument of {
241+
name: Reference.t;
242+
argument: Expression.t;
243+
}
244+
| NonCallableDecoratorFactory of {
245+
name: Reference.t;
246+
annotation: Type.t;
247+
}
248+
| NonCallableDecorator of {
249+
name: Reference.t;
250+
has_arguments: bool;
251+
annotation: Type.t;
252+
}
253+
| DecoratorFactoryFailedToApply of {
254+
name: Reference.t;
255+
reason: kind option;
256+
}
257+
| ApplicationFailed of {
258+
name: Reference.t;
259+
has_arguments: bool;
260+
reason: kind option;
261+
}
250262
| SetterNameMismatch of {
263+
name: Reference.t;
251264
actual: string;
252265
expected: string;
253266
}
@@ -1109,11 +1122,14 @@ let rec messages ~concise ~signature location kind =
11091122
variable
11101123
unconcatenatable;
11111124
])
1112-
| InvalidDecoration { decorator = { name; _ }; reason = CouldNotResolve } ->
1113-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1114-
[Format.asprintf "Pyre was not able to infer the type of the decorator `%s`." name]
1115-
| InvalidDecoration { decorator = { name; _ }; reason = CouldNotResolveArgument argument } ->
1116-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1125+
| InvalidDecoration (CouldNotResolve expression) ->
1126+
[
1127+
Format.asprintf
1128+
"Pyre was not able to infer the type of the decorator `%s`."
1129+
(show_sanitized_expression expression);
1130+
]
1131+
| InvalidDecoration (CouldNotResolveArgument { name; argument }) ->
1132+
let name = Reference.sanitized name |> Reference.show in
11171133
[
11181134
Format.asprintf
11191135
"Pyre was not able to infer the type of argument `%s` to decorator factory `%s`."
@@ -1122,46 +1138,43 @@ let rec messages ~concise ~signature location kind =
11221138
"This can usually be worked around by extracting your argument into a global variable and \
11231139
providing an explicit type annotation.";
11241140
]
1125-
| InvalidDecoration { decorator = { name; _ }; reason = NonCallableDecoratorFactory result } ->
1126-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1141+
| InvalidDecoration (NonCallableDecoratorFactory { name; annotation }) ->
1142+
let name = Reference.sanitized name |> Reference.show in
11271143
[
11281144
Format.asprintf
11291145
"Decorator factory `%s` could not be called, because its type `%a` is not callable."
11301146
name
11311147
pp_type
1132-
result;
1148+
annotation;
11331149
]
1134-
| InvalidDecoration { decorator = { name; arguments }; reason = NonCallableDecorator result } ->
1135-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1136-
let arguments = if Option.is_some arguments then "(...)" else "" in
1150+
| InvalidDecoration (NonCallableDecorator { name; has_arguments; annotation }) ->
1151+
let name = Reference.sanitized name |> Reference.show in
1152+
let arguments = if has_arguments then "(...)" else "" in
11371153
[
11381154
Format.asprintf
11391155
"Decorator `%s%s` could not be called, because its type `%a` is not callable."
11401156
name
11411157
arguments
11421158
pp_type
1143-
result;
1159+
annotation;
11441160
]
1145-
| InvalidDecoration
1146-
{ decorator = { name; _ }; reason = DecoratorFactoryFailedToApply inner_reason } -> (
1147-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1161+
| InvalidDecoration (DecoratorFactoryFailedToApply { name; reason }) -> (
1162+
let name = Reference.sanitized name |> Reference.show in
11481163
let recurse = messages ~concise ~signature location in
1149-
match inner_reason >>| recurse >>= List.hd with
1164+
match reason >>| recurse >>= List.hd with
11501165
| Some inner_message ->
11511166
[Format.asprintf "While applying decorator factory `%s`: %s" name inner_message]
11521167
| None -> [Format.asprintf "Decorator factory `%s` failed to apply." name])
1153-
| InvalidDecoration { decorator = { name; arguments }; reason = ApplicationFailed inner_reason }
1154-
-> (
1155-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1156-
let arguments = if Option.is_some arguments then "(...)" else "" in
1168+
| InvalidDecoration (ApplicationFailed { name; has_arguments; reason }) -> (
1169+
let name = Reference.sanitized name |> Reference.show in
1170+
let arguments = if has_arguments then "(...)" else "" in
11571171
let recurse = messages ~concise ~signature location in
1158-
match inner_reason >>| recurse >>= List.hd with
1172+
match reason >>| recurse >>= List.hd with
11591173
| Some inner_message ->
11601174
[Format.asprintf "While applying decorator `%s%s`: %s" name arguments inner_message]
11611175
| None -> [Format.asprintf "Decorator `%s%s` failed to apply." name arguments])
1162-
| InvalidDecoration { decorator = { name; _ }; reason = SetterNameMismatch { expected; actual } }
1163-
->
1164-
let name = Node.value name |> Reference.sanitized |> Reference.show in
1176+
| InvalidDecoration (SetterNameMismatch { name; expected; actual }) ->
1177+
let name = Reference.sanitized name |> Reference.show in
11651178
[
11661179
Format.asprintf
11671180
"Invalid property setter `%s`: `%s` does not match decorated method `%s`."
@@ -3573,8 +3586,8 @@ let suppress ~mode ~ignore_codes error =
35733586
| InconsistentOverride
35743587
{ override = StrengthenedPrecondition (Found { expected = Type.Variable _; _ }); _ } ->
35753588
true
3576-
| InvalidDecoration { reason = CouldNotResolve; _ }
3577-
| InvalidDecoration { reason = CouldNotResolveArgument _; _ } ->
3589+
| InvalidDecoration (CouldNotResolve _)
3590+
| InvalidDecoration (CouldNotResolveArgument _) ->
35783591
true
35793592
| InvalidTypeParameters
35803593
{ kind = AttributeResolution.IncorrectNumberOfParameters { actual = 0; _ }; _ } ->

source/analysis/analysisError.mli

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,36 @@ and tuple_concatenation_problem =
226226
| UnpackingNonIterable of { annotation: Type.t }
227227
[@@deriving compare, eq, sexp, show, hash]
228228

229-
type invalid_decoration_reason =
230-
| CouldNotResolve
231-
| CouldNotResolveArgument of Expression.t
232-
| NonCallableDecoratorFactory of Type.t
233-
| NonCallableDecorator of Type.t
234-
| DecoratorFactoryFailedToApply of kind option
235-
| ApplicationFailed of kind option
229+
type invalid_decoration =
230+
| CouldNotResolve of Expression.t
231+
| CouldNotResolveArgument of {
232+
name: Reference.t;
233+
argument: Expression.t;
234+
}
235+
| NonCallableDecoratorFactory of {
236+
name: Reference.t;
237+
annotation: Type.t;
238+
}
239+
| NonCallableDecorator of {
240+
name: Reference.t;
241+
has_arguments: bool;
242+
annotation: Type.t;
243+
}
244+
| DecoratorFactoryFailedToApply of {
245+
name: Reference.t;
246+
reason: kind option;
247+
}
248+
| ApplicationFailed of {
249+
name: Reference.t;
250+
has_arguments: bool;
251+
reason: kind option;
252+
}
236253
| SetterNameMismatch of {
254+
name: Reference.t;
237255
actual: string;
238256
expected: string;
239257
}
240258

241-
and invalid_decoration = {
242-
decorator: Statement.Decorator.t;
243-
reason: invalid_decoration_reason;
244-
}
245-
246259
and kind =
247260
| AnalysisFailure of analysis_failure
248261
| ParserFailure of string

source/analysis/test/analysisErrorTest.ml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -801,28 +801,8 @@ let test_suppress _ =
801801
assert_not_suppressed Source.Strict (missing_return Type.Any);
802802
assert_not_suppressed Source.Strict (Error.AnalysisFailure (UnexpectedUndefinedType "int"));
803803
assert_suppressed Source.Unsafe (missing_return Type.Top);
804-
assert_not_suppressed
805-
Source.Strict
806-
(Error.InvalidDecoration
807-
{
808-
decorator =
809-
{
810-
Decorator.name = Reference.create "test" |> Node.create_with_default_location;
811-
arguments = None;
812-
};
813-
reason = CouldNotResolve;
814-
});
815-
assert_suppressed
816-
Source.Unsafe
817-
(Error.InvalidDecoration
818-
{
819-
decorator =
820-
{
821-
Decorator.name = Reference.create "test" |> Node.create_with_default_location;
822-
arguments = None;
823-
};
824-
reason = CouldNotResolve;
825-
});
804+
assert_not_suppressed Source.Strict (Error.InvalidDecoration (CouldNotResolve !"test"));
805+
assert_suppressed Source.Unsafe (Error.InvalidDecoration (CouldNotResolve !"test"));
826806

827807
(* Should not be made *)
828808
assert_not_suppressed Source.Unsafe (incompatible_return_type Type.integer Type.Any);

source/analysis/typeCheck.ml

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,12 @@ module State (Context : Context) = struct
883883
~location
884884
~kind:
885885
(Error.InvalidDecoration
886-
{
887-
decorator;
888-
reason =
889-
Error.SetterNameMismatch
890-
{ actual = decorated_property_name; expected = Reference.last name };
891-
})
886+
(Error.SetterNameMismatch
887+
{
888+
name = decorator_name;
889+
actual = decorated_property_name;
890+
expected = Reference.last name;
891+
}))
892892
| _ -> errors
893893
else
894894
let { Resolved.errors = decorator_errors; _ } =
@@ -6644,13 +6644,15 @@ let emit_errors_on_exit (module Context : Context) ~errors_sofar ~resolution ()
66446644
else
66456645
index
66466646
in
6647-
let add_error ({ Decorator.name = { Node.location; _ }; arguments } as decorator) =
6647+
let add_error
6648+
({ Decorator.name = { Node.location; value = name }; arguments } as decorator)
6649+
=
66486650
let make_error reason =
66496651
let error =
66506652
Error.create
66516653
~location:(Location.with_module ~qualifier:Context.qualifier location)
66526654
~define:Context.define
6653-
~kind:(Error.InvalidDecoration { decorator; reason })
6655+
~kind:(Error.InvalidDecoration reason)
66546656
in
66556657
error :: errors
66566658
in
@@ -6668,23 +6670,34 @@ let emit_errors_on_exit (module Context : Context) ~errors_sofar ~resolution ()
66686670
reason >>| convert >>= List.hd >>| fun (_, kind) -> kind
66696671
in
66706672
match reason with
6671-
| CouldNotResolve -> make_error CouldNotResolve
6673+
| CouldNotResolve -> make_error (CouldNotResolve (Decorator.to_expression decorator))
66726674
| CouldNotResolveArgument { argument_index } ->
66736675
let add_error argument =
66746676
let argument, _ = Ast.Expression.Call.Argument.unpack argument in
6675-
make_error (CouldNotResolveArgument argument)
6677+
make_error (CouldNotResolveArgument { name; argument })
66766678
in
66776679
arguments
66786680
>>= (fun arguments -> List.nth arguments argument_index)
66796681
>>| add_error
66806682
|> Option.value ~default:errors
66816683
| NonCallableDecoratorFactory resolved ->
6682-
make_error (NonCallableDecoratorFactory resolved)
6683-
| NonCallableDecorator result -> make_error (NonCallableDecorator result)
6684+
make_error (NonCallableDecoratorFactory { name; annotation = resolved })
6685+
| NonCallableDecorator result ->
6686+
make_error
6687+
(NonCallableDecorator
6688+
{ name; has_arguments = Option.is_some arguments; annotation = result })
66846689
| FactorySignatureSelectionFailed { reason; callable } ->
6685-
make_error (DecoratorFactoryFailedToApply (extract_error ~reason ~callable))
6690+
make_error
6691+
(DecoratorFactoryFailedToApply
6692+
{ name; reason = extract_error ~reason ~callable })
66866693
| ApplicationFailed { reason; callable } ->
6687-
make_error (ApplicationFailed (extract_error ~reason ~callable))
6694+
make_error
6695+
(ApplicationFailed
6696+
{
6697+
name;
6698+
has_arguments = Option.is_some arguments;
6699+
reason = extract_error ~reason ~callable;
6700+
})
66886701
in
66896702

66906703
let { StatementDefine.Signature.decorators; _ } = signature in

0 commit comments

Comments
 (0)