[ty] Fix error message for invalidly providing type arguments to NamedTuple when it occurs in a type expression (#19940)

This commit is contained in:
Alex Waygood 2025-08-16 18:45:15 +01:00 committed by GitHub
parent 527a690a73
commit f4d8826428
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 2 deletions

View file

@ -312,6 +312,9 @@ def expects_named_tuple(x: typing.NamedTuple):
def _(y: type[typing.NamedTuple]):
reveal_type(y) # revealed: @Todo(unsupported type[X] special form)
# error: [invalid-type-form] "Special form `typing.NamedTuple` expected no type parameter"
def _(z: typing.NamedTuple[int]): ...
```
Any instance of a `NamedTuple` class can therefore be passed for a function parameter that is

View file

@ -10783,7 +10783,8 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
SpecialFormType::TypingSelf
| SpecialFormType::TypeAlias
| SpecialFormType::TypedDict
| SpecialFormType::Unknown => {
| SpecialFormType::Unknown
| SpecialFormType::NamedTuple => {
self.infer_type_expression(arguments_slice);
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
@ -10808,7 +10809,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
SpecialFormType::Tuple => {
Type::tuple(self.infer_tuple_type_expression(arguments_slice))
}
SpecialFormType::Generic | SpecialFormType::Protocol | SpecialFormType::NamedTuple => {
SpecialFormType::Generic | SpecialFormType::Protocol => {
self.infer_expression(arguments_slice);
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
builder.into_diagnostic(format_args!(