Skip to content

Commit f342638

Browse files
arthaudfacebook-github-bot
authored andcommitted
Add origins to typing.Union nodes created from the union shorthand a | b
Summary: We turn `a | b` into `typing.Union[a, b]` during preprocessing. Again, those nodes need an origin, since they are not in the original code. Reviewed By: tianhan0 Differential Revision: D74739793 fbshipit-source-id: 31d8b15c7fe9bfa1f3c6657620b53b2718a892c0
1 parent c83d2e8 commit f342638

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

source/analysis/preprocessing.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,7 +3889,7 @@ let replace_union_shorthand_in_annotation_expression =
38893889
in
38903890
let value =
38913891
match value with
3892-
| Expression.BinaryOperator { operator = BinaryOperator.BitOr; left; right; origin = _ } ->
3892+
| Expression.BinaryOperator { operator = BinaryOperator.BitOr; left; right; origin } ->
38933893
let indices =
38943894
[left; right]
38953895
(* Recursively transform them into `typing.Union[...]` form *)
@@ -3899,6 +3899,7 @@ let replace_union_shorthand_in_annotation_expression =
38993899
|> List.rev
39003900
in
39013901
let index = { Node.value = Expression.Tuple indices; location } in
3902+
let origin = Some (Origin.create ?base:origin ~location Origin.UnionShorthand) in
39023903
Expression.Subscript
39033904
{
39043905
base =
@@ -3910,11 +3911,11 @@ let replace_union_shorthand_in_annotation_expression =
39103911
{
39113912
base = { Node.location; value = Name (Name.Identifier "typing") };
39123913
attribute = "Union";
3913-
origin = None;
3914+
origin;
39143915
});
39153916
};
39163917
index;
3917-
origin = None;
3918+
origin;
39183919
}
39193920
| Subscript { Subscript.base; index; origin } ->
39203921
Subscript { base; index = transform_expression index; origin }

source/ast/expression.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ and Origin : sig
11791179
| InGetItem (* `e in l` can be turned into `l.__getitem__(0).__eq__(e)` *)
11801180
| InGetItemEq (* `e in l` can be turned into `l.__getitem__(0).__eq__(e)` *)
11811181
| Slice (* `1:2` is turned into `slice(1,2,None)` *)
1182+
| UnionShorthand (* `a | b` is turned into `typing.Union[a, b]` when in typing context *)
11821183
| Negate (* `if cond:` is turned into `assert(cond)` and `assert(not cond)` *)
11831184
| NegateIs (* `not(a is not b)` is turned into `a is b` *)
11841185
| NegateIsNot (* `not(a is b)` is turned into `a is not b` *)
@@ -1287,6 +1288,7 @@ end = struct
12871288
| InGetItem
12881289
| InGetItemEq
12891290
| Slice
1291+
| UnionShorthand
12901292
| Negate
12911293
| NegateIs
12921294
| NegateIsNot

source/ast/expression.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ and Origin : sig
428428
| InGetItem (* `e in l` can be turned into `l.__getitem__(0).__eq__(e)` *)
429429
| InGetItemEq (* `e in l` can be turned into `l.__getitem__(0).__eq__(e)` *)
430430
| Slice (* `1:2` is turned into `slice(1,2,None)` *)
431+
| UnionShorthand (* `a | b` is turned into `typing.Union[a, b]` when in typing context *)
431432
| Negate (* `if cond:` is turned into `assert(cond)` and `assert(not cond)` *)
432433
| NegateIs (* `not(a is not b)` is turned into `a is b` *)
433434
| NegateIsNot (* `not(a is b)` is turned into `a is not b` *)

0 commit comments

Comments
 (0)