Skip to content

Commit f4d8826

Browse files
authored
[ty] Fix error message for invalidly providing type arguments to NamedTuple when it occurs in a type expression (#19940)
1 parent 527a690 commit f4d8826

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

crates/ty_python_semantic/resources/mdtest/named_tuple.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ def expects_named_tuple(x: typing.NamedTuple):
312312

313313
def _(y: type[typing.NamedTuple]):
314314
reveal_type(y) # revealed: @Todo(unsupported type[X] special form)
315+
316+
# error: [invalid-type-form] "Special form `typing.NamedTuple` expected no type parameter"
317+
def _(z: typing.NamedTuple[int]): ...
315318
```
316319

317320
Any instance of a `NamedTuple` class can therefore be passed for a function parameter that is

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10783,7 +10783,8 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
1078310783
SpecialFormType::TypingSelf
1078410784
| SpecialFormType::TypeAlias
1078510785
| SpecialFormType::TypedDict
10786-
| SpecialFormType::Unknown => {
10786+
| SpecialFormType::Unknown
10787+
| SpecialFormType::NamedTuple => {
1078710788
self.infer_type_expression(arguments_slice);
1078810789

1078910790
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
@@ -10808,7 +10809,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
1080810809
SpecialFormType::Tuple => {
1080910810
Type::tuple(self.infer_tuple_type_expression(arguments_slice))
1081010811
}
10811-
SpecialFormType::Generic | SpecialFormType::Protocol | SpecialFormType::NamedTuple => {
10812+
SpecialFormType::Generic | SpecialFormType::Protocol => {
1081210813
self.infer_expression(arguments_slice);
1081310814
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
1081410815
builder.into_diagnostic(format_args!(

0 commit comments

Comments
 (0)