Skip to content

Commit

Permalink
Upgrade to pyre-ast 0.1.11
Browse files Browse the repository at this point in the history
Summary:
This upgrade brings Python 3.12 parsing support, although we still need to build
support in the Pyre CPythonParser wrapper around pyre-ast.

It looks like pyre-ast changes the locations of literals within format strings.
The new locations are more narrow, containing only the literal portion. Before
the location included the entire format string expression. I think this is a
nice improvement and it passes all tests.

Reviewed By: yangdanny97

Differential Revision: D60124494

fbshipit-source-id: ce65503b40ed1ba24814ff490953d96e6cf1b878
  • Loading branch information
samwgoldman authored and facebook-github-bot committed Jul 24, 2024
1 parent e01582e commit 7438d40
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scripts/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"lwt.5.6.1",
"lwt_ppx.2.1.0",
"ounit2-lwt.2.2.7",
"pyre-ast.0.1.9",
"pyre-ast.0.1.11",
"mtime.1.4.0",
"errpy.0.0.9",
]
Expand Down
51 changes: 48 additions & 3 deletions source/cpython_parser/pyreCPythonParser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,18 @@ let statement =
|> Node.create ~location;
]
in
let function_def ~location ~name ~args ~body ~decorator_list ~returns ~type_comment ~context =
let function_def
~location
~name
~args
~body
~decorator_list
~returns
~type_comment
~type_params
~context
=
ignore type_params (* TODO *);
create_function_definition
~location
~async:false
Expand All @@ -790,8 +801,18 @@ let statement =
~type_comment
~context
in
let async_function_def ~location ~name ~args ~body ~decorator_list ~returns ~type_comment ~context
let async_function_def
~location
~name
~args
~body
~decorator_list
~returns
~type_comment
~type_params
~context
=
ignore type_params (* TODO *);
create_function_definition
~location
~async:true
Expand All @@ -803,7 +824,8 @@ let statement =
~type_comment
~context
in
let class_def ~location ~name ~bases ~keywords ~body ~decorator_list ~context =
let class_def ~location ~name ~bases ~keywords ~body ~decorator_list ~type_params ~context =
ignore type_params (* TODO *);
let base_arguments =
List.append
(List.map bases ~f:(fun arg -> convert_positional_argument arg, arg.Node.location))
Expand Down Expand Up @@ -865,6 +887,10 @@ let statement =
in
List.map targets ~f:create_assign_for_target
in
let type_alias ~location ~name ~type_params ~value ~context =
ignore type_params (* TODO *);
assign ~location ~targets:[name] ~value ~type_comment:None ~context
in
let aug_assign ~location ~target ~op ~value ~context:_ =
[
Statement.AugmentedAssign { AugmentedAssign.target; operator = op; value }
Expand Down Expand Up @@ -1034,6 +1060,7 @@ let statement =
~return
~delete
~assign
~type_alias
~aug_assign
~ann_assign
~for_
Expand Down Expand Up @@ -1066,6 +1093,23 @@ let function_type ~argtypes ~returns =
{ FunctionSignature.parameter_annotations = argtypes; return_annotation = returns }


let type_param =
let raise_unimplemented location =
let {
Ast.Location.start = { Location.line; column };
stop = { Location.line = end_line; column = end_column };
}
=
location
in
raise (InternalError { Error.line; column; end_line; end_column; message = "Unimplemented" })
in
let type_var ~location ~name:_ ~bound:_ = raise_unimplemented location in
let param_spec ~location ~name:_ = raise_unimplemented location in
let type_var_tuple ~location ~name:_ = raise_unimplemented location in
{ PyreAst.TaglessFinal.TypeParam.type_var; param_spec; type_var_tuple }


let specification =
PyreAst.TaglessFinal.make
~argument
Expand All @@ -1089,6 +1133,7 @@ let specification =
~position
~statement
~type_ignore
~type_param
~unary_operator
~with_item
()
Expand Down
1 change: 1 addition & 0 deletions source/cpython_parser/test/expressionTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ let test_fstring =
format (Expression.Name (Name.Identifier "w"));
literal ".";
format (Expression.Name (Name.Identifier "p"));
literal "";
]);
];
labeled_test_case __FUNCTION__ __LINE__
Expand Down
10 changes: 5 additions & 5 deletions source/cpython_parser/test/locationTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2890,7 +2890,7 @@ let test_string_locations =
~start:(1, 0)
~stop:(1, 6)
(Expression.FormatString
[Substring.Literal (node ~start:(1, 0) ~stop:(1, 6) "foo")])));
[Substring.Literal (node ~start:(1, 2) ~stop:(1, 5) "foo")])));
];
labeled_test_case __FUNCTION__ __LINE__
@@ assert_parsed
Expand All @@ -2906,7 +2906,7 @@ let test_string_locations =
~stop:(1, 10)
(Expression.FormatString
[
Substring.Literal (node ~start:(1, 0) ~stop:(1, 10) "foo ");
Substring.Literal (node ~start:(1, 2) ~stop:(1, 6) "foo ");
Substring.Format
{
format_spec = None;
Expand All @@ -2931,7 +2931,7 @@ let test_string_locations =
~start:(1, 0)
~stop:(1, 13)
(Expression.FormatString
[Substring.Literal (node ~start:(1, 0) ~stop:(1, 13) "foobar")])));
[Substring.Literal (node ~start:(1, 2) ~stop:(1, 12) "foobar")])));
];
labeled_test_case __FUNCTION__ __LINE__
@@ assert_parsed
Expand Down Expand Up @@ -3020,7 +3020,7 @@ let test_string_locations =
~stop:(3, 3)
(Expression.FormatString
[
Substring.Literal (node ~start:(1, 0) ~stop:(3, 3) "\n");
Substring.Literal (node ~start:(1, 4) ~stop:(2, 0) "\n");
Substring.Format
{
format_spec = None;
Expand All @@ -3031,7 +3031,7 @@ let test_string_locations =
(Expression.Constant
(Constant.String (StringLiteral.create "a")));
};
Substring.Literal (node ~start:(1, 0) ~stop:(3, 3) "b\n");
Substring.Literal (node ~start:(2, 5) ~stop:(3, 0) "b\n");
])));
];
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@
{ "always-via": "tito" }
],
"tito_positions": [
{ "line": 37, "start": 11, "end": 38 },
{ "line": 37, "start": 19, "end": 36 },
{ "line": 37, "start": 21, "end": 35 }
],
"origin": {
Expand Down

0 comments on commit 7438d40

Please sign in to comment.