Repository navigation
Commit 6f1fdeb
Allow building type qualifiers like
Summary:
The idea is:
- If we have a subscript expression, like `x = my_list[0]` where we know that `my_list` is a `list[int]`, then `x` gets inferred as `int`. That's what https://github.com/meta/pyrefly/blob/8003f9b3d96b207c916c4a5c5ba039fb568cb857/pyrefly/lib/alt/expr.rs#L945-L951 does
- If however we have a subscript assignment like `x = NotRequired[int]`, then `NotRequired` is a `_SpecialForm` which is typed to return `object` for `__getitem__`, so we need to infer `x` as `object` rather than reporting it as an invalid annotation
- For cases in class definitions, like `class MyClass(TypedDict, total=False)`, however, we want to preserve the current behaviour and raise on something like `extra_items=NotRequired[int]`, which is why in `fn expr_class_keyword` we need to pass down `Some(TypeFormContext::ClassKeyword)` so that `if type_form_context.is_none()` in `expr_infer_without_shape_comment` doesn't evaluate to `true`
Note that `x: NotRequired[int]` still gets checked as-before, as this change only covers the `if type_form_context.is_none()` case, and in the `x: NotRequired[int]` case, there is a type form context
Fixes #5429
- [x] I am a human, and I fully understand the code I am submitting.
I used Claude to generate the code, but the description here is fully written by me
Pull Request resolved: #5450
Reviewed By: rchen152
Differential Revision: D128312510
fbshipit-source-id: a6ffbb2c8dc35645e4270cdffc18a112963ef627NotRequired[...] at runtime (#5450)1 parent bb5c70b commit 6f1fdeb
4 files changed
Lines changed: 57 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13742 | 13742 | | |
13743 | 13743 | | |
13744 | 13744 | | |
13745 | | - | |
13746 | | - | |
| 13745 | + | |
| 13746 | + | |
13747 | 13747 | | |
13748 | | - | |
| 13748 | + | |
13749 | 13749 | | |
13750 | 13750 | | |
13751 | 13751 | | |
13752 | 13752 | | |
13753 | 13753 | | |
13754 | 13754 | | |
13755 | 13755 | | |
13756 | | - | |
13757 | | - | |
| 13756 | + | |
| 13757 | + | |
13758 | 13758 | | |
13759 | | - | |
| 13759 | + | |
13760 | 13760 | | |
13761 | 13761 | | |
13762 | 13762 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
946 | 946 | | |
947 | 947 | | |
948 | 948 | | |
949 | | - | |
950 | | - | |
951 | | - | |
952 | | - | |
953 | | - | |
954 | | - | |
955 | | - | |
| 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 | + | |
956 | 975 | | |
957 | 976 | | |
958 | 977 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
796 | 796 | | |
797 | 797 | | |
798 | 798 | | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
799 | 804 | | |
800 | 805 | | |
801 | 806 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2940 | 2940 | | |
2941 | 2941 | | |
2942 | 2942 | | |
| 2943 | + | |
| 2944 | + | |
| 2945 | + | |
| 2946 | + | |
| 2947 | + | |
| 2948 | + | |
| 2949 | + | |
| 2950 | + | |
| 2951 | + | |
| 2952 | + | |
| 2953 | + | |
| 2954 | + | |
| 2955 | + | |
| 2956 | + | |
| 2957 | + | |
| 2958 | + | |
| 2959 | + | |
| 2960 | + | |
| 2961 | + | |
| 2962 | + | |
2943 | 2963 | | |
2944 | 2964 | | |
2945 | 2965 | | |
| |||
0 commit comments