Commit d2a3e66
authored
Fix crash with PartialTypes and the enum plugin (#14021)
Fixes #12109.
The original issue reported that the bug had to do with the use of the
`--follow-imports=skip` flag. However, it turned out this was a red
herring after closer inspection: I was able to trigger a more minimal
repro both with and without this flag:
```python
from enum import Enum
class Foo(Enum):
a = [] # E: Need type annotation for "a" (hint: "a: List[<type>] = ...")
b = None
def check(self) -> None:
reveal_type(Foo.a.value) # N: Revealed type is "<partial list[?]>"
reveal_type(Foo.b.value) # N: Revealed type is "<partial None>"
```
The first two `reveal_types` demonstrate the crux of the bug: the enum
plugin does not correctly handle and convert partial types into regular
types when inferring the type of the `.value` field.
This can then cause any number of downstream problems. For example,
suppose we modify `def check(...)` so it runs `reveal_type(self.value)`.
Doing this will trigger a crash in mypy because it makes the enum plugin
eventually try running `is_equivalent(...)` on the two partial types.
But `is_equivalent` does not support partial types, so we crash.
I opted to solve this problem by:
1. Making the enum plugin explicitly call the `fixup_partial_types`
function on all field types. This prevents the code from crashing.
2. Modifies mypy so that Final vars are never marked as being
PartialTypes. Without this, `reveal_type(Foo.b.value)` would report a
type of `Union[Any, None]` instead of just `None`. (Note that all enum
fields are implicitly final).1 parent e8de6d1 commit d2a3e66
File tree
5 files changed
+70
-28
lines changed- mypy
- plugins
- test-data/unit
5 files changed
+70
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
162 | 163 | | |
163 | 164 | | |
164 | 165 | | |
| |||
2738 | 2739 | | |
2739 | 2740 | | |
2740 | 2741 | | |
2741 | | - | |
2742 | | - | |
| 2742 | + | |
| 2743 | + | |
2743 | 2744 | | |
2744 | 2745 | | |
2745 | 2746 | | |
| |||
3687 | 3688 | | |
3688 | 3689 | | |
3689 | 3690 | | |
3690 | | - | |
| 3691 | + | |
| 3692 | + | |
| 3693 | + | |
| 3694 | + | |
3691 | 3695 | | |
3692 | 3696 | | |
3693 | 3697 | | |
| |||
6114 | 6118 | | |
6115 | 6119 | | |
6116 | 6120 | | |
6117 | | - | |
| 6121 | + | |
6118 | 6122 | | |
6119 | 6123 | | |
6120 | 6124 | | |
| |||
6145 | 6149 | | |
6146 | 6150 | | |
6147 | 6151 | | |
6148 | | - | |
6149 | | - | |
6150 | | - | |
6151 | | - | |
6152 | | - | |
6153 | | - | |
6154 | | - | |
6155 | | - | |
6156 | | - | |
6157 | | - | |
6158 | | - | |
6159 | | - | |
6160 | | - | |
6161 | | - | |
| 6152 | + | |
6162 | 6153 | | |
6163 | 6154 | | |
6164 | 6155 | | |
| |||
7006 | 6997 | | |
7007 | 6998 | | |
7008 | 6999 | | |
7009 | | - | |
7010 | | - | |
| 7000 | + | |
| 7001 | + | |
7011 | 7002 | | |
7012 | | - | |
| 7003 | + | |
| 7004 | + | |
7013 | 7005 | | |
7014 | 7006 | | |
7015 | 7007 | | |
7016 | 7008 | | |
7017 | 7009 | | |
7018 | | - | |
7019 | | - | |
7020 | | - | |
7021 | | - | |
7022 | | - | |
| 7010 | + | |
| 7011 | + | |
| 7012 | + | |
| 7013 | + | |
| 7014 | + | |
| 7015 | + | |
| 7016 | + | |
| 7017 | + | |
| 7018 | + | |
| 7019 | + | |
| 7020 | + | |
7023 | 7021 | | |
7024 | 7022 | | |
7025 | 7023 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
| |||
2925 | 2926 | | |
2926 | 2927 | | |
2927 | 2928 | | |
2928 | | - | |
| 2929 | + | |
2929 | 2930 | | |
2930 | 2931 | | |
2931 | 2932 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
1016 | 1017 | | |
1017 | 1018 | | |
1018 | 1019 | | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2100 | 2100 | | |
2101 | 2101 | | |
2102 | 2102 | | |
| 2103 | + | |
| 2104 | + | |
| 2105 | + | |
| 2106 | + | |
| 2107 | + | |
| 2108 | + | |
| 2109 | + | |
| 2110 | + | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
| 2114 | + | |
| 2115 | + | |
| 2116 | + | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
| 2121 | + | |
| 2122 | + | |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| 2129 | + | |
0 commit comments