Skip to content

Commit 75eda5e

Browse files
committed
check boolean value of infer_variance kwarg, too
1 parent 249c0aa commit 75eda5e

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

crates/ty_python_semantic/resources/mdtest/diagnostics/legacy_typevars.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ from typing import TypeVar
9797
T = TypeVar("T", covariant=True, contravariant=True)
9898
```
9999

100-
## Variance parameters must be unambiguous
100+
## Boolean parameters must be unambiguous
101101

102102
```py
103-
from typing import TypeVar
103+
from typing_extensions import TypeVar
104104

105105
def cond() -> bool:
106106
return True
@@ -110,6 +110,9 @@ T = TypeVar("T", covariant=cond())
110110

111111
# error: [invalid-legacy-type-variable]
112112
U = TypeVar("U", contravariant=cond())
113+
114+
# error: [invalid-legacy-type-variable]
115+
V = TypeVar("V", infer_variance=cond())
113116
```
114117

115118
## Invalid keyword arguments

crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ from typing import TypeVar
234234
T = TypeVar("T", covariant=True, contravariant=True)
235235
```
236236

237-
### Variance parameters must be unambiguous
237+
### Boolean parameters must be unambiguous
238238

239239
```py
240-
from typing import TypeVar
240+
from typing_extensions import TypeVar
241241

242242
def cond() -> bool:
243243
return True
@@ -247,6 +247,9 @@ T = TypeVar("T", covariant=cond())
247247

248248
# error: [invalid-legacy-type-variable]
249249
U = TypeVar("U", contravariant=cond())
250+
251+
# error: [invalid-legacy-type-variable]
252+
V = TypeVar("V", infer_variance=cond())
250253
```
251254

252255
### Invalid keyword arguments
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ source: crates/ty_test/src/lib.rs
33
expression: snapshot
44
---
55
---
6-
mdtest name: legacy_typevars.md - Legacy typevar creation diagnostics - Variance parameters must be unambiguous
6+
mdtest name: legacy_typevars.md - Legacy typevar creation diagnostics - Boolean parameters must be unambiguous
77
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/legacy_typevars.md
88
---
99

@@ -12,7 +12,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/legacy_typev
1212
## mdtest_snippet.py
1313

1414
```
15-
1 | from typing import TypeVar
15+
1 | from typing_extensions import TypeVar
1616
2 |
1717
3 | def cond() -> bool:
1818
4 | return True
@@ -22,17 +22,20 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/legacy_typev
2222
8 |
2323
9 | # error: [invalid-legacy-type-variable]
2424
10 | U = TypeVar("U", contravariant=cond())
25+
11 |
26+
12 | # error: [invalid-legacy-type-variable]
27+
13 | V = TypeVar("V", infer_variance=cond())
2528
```
2629

2730
# Diagnostics
2831

2932
```
3033
error[invalid-legacy-type-variable]: The `covariant` parameter of `TypeVar` cannot have an ambiguous truthiness
31-
--> src/mdtest_snippet.py:7:18
34+
--> src/mdtest_snippet.py:7:28
3235
|
3336
6 | # error: [invalid-legacy-type-variable]
3437
7 | T = TypeVar("T", covariant=cond())
35-
| ^^^^^^^^^^^^^^^^
38+
| ^^^^^^
3639
8 |
3740
9 | # error: [invalid-legacy-type-variable]
3841
|
@@ -42,11 +45,25 @@ info: rule `invalid-legacy-type-variable` is enabled by default
4245

4346
```
4447
error[invalid-legacy-type-variable]: The `contravariant` parameter of `TypeVar` cannot have an ambiguous truthiness
45-
--> src/mdtest_snippet.py:10:18
48+
--> src/mdtest_snippet.py:10:32
4649
|
4750
9 | # error: [invalid-legacy-type-variable]
4851
10 | U = TypeVar("U", contravariant=cond())
49-
| ^^^^^^^^^^^^^^^^^^^^
52+
| ^^^^^^
53+
11 |
54+
12 | # error: [invalid-legacy-type-variable]
55+
|
56+
info: rule `invalid-legacy-type-variable` is enabled by default
57+
58+
```
59+
60+
```
61+
error[invalid-legacy-type-variable]: The `infer_variance` parameter of `TypeVar` cannot have an ambiguous truthiness
62+
--> src/mdtest_snippet.py:13:33
63+
|
64+
12 | # error: [invalid-legacy-type-variable]
65+
13 | V = TypeVar("V", infer_variance=cond())
66+
| ^^^^^^
5067
|
5168
info: rule `invalid-legacy-type-variable` is enabled by default
5269

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,7 +4134,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
41344134
&self.context,
41354135
"The `covariant` parameter of `TypeVar` \
41364136
cannot have an ambiguous truthiness",
4137-
kwarg,
4137+
&kwarg.value,
41384138
);
41394139
}
41404140
}
@@ -4151,7 +4151,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
41514151
&self.context,
41524152
"The `contravariant` parameter of `TypeVar` \
41534153
cannot have an ambiguous truthiness",
4154-
kwarg,
4154+
&kwarg.value,
41554155
);
41564156
}
41574157
}
@@ -4180,7 +4180,18 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
41804180
);
41814181
}
41824182
// TODO support `infer_variance` in legacy TypeVars
4183-
self.infer_expression(&kwarg.value, TypeContext::default());
4183+
if self
4184+
.infer_expression(&kwarg.value, TypeContext::default())
4185+
.bool(db)
4186+
.is_ambiguous()
4187+
{
4188+
return error(
4189+
&self.context,
4190+
"The `infer_variance` parameter of `TypeVar` \
4191+
cannot have an ambiguous truthiness",
4192+
&kwarg.value,
4193+
);
4194+
}
41844195
}
41854196
name => {
41864197
// We don't return here; this error is informational since this will error

0 commit comments

Comments
 (0)