Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ class Baz[*Ts]: ...
# TODO: false positive
z: Baz[int, str, bytes] # error: [not-subscriptable]
```

And we also provide some basic validation in some cases:

```py
# error: [invalid-generic-class] "`TypeVarTuple` must be unpacked with `*` or `Unpack[]` when used as an argument to `Generic`"
class Spam(Generic[Ts]): ...
```
35 changes: 32 additions & 3 deletions crates/ty_python_semantic/src/types/infer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15252,14 +15252,17 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
NotYetSupported,
/// A duplicate typevar was provided.
DuplicateTypevar(&'db str),
/// A `TypeVarTuple` was provided but not unpacked.
TypeVarTupleMustBeUnpacked,
}

impl<'db> LegacyGenericContextError<'db> {
const fn into_type(self) -> Type<'db> {
match self {
LegacyGenericContextError::InvalidArgument(_)
| LegacyGenericContextError::VariadicTupleArguments
| LegacyGenericContextError::DuplicateTypevar(_) => Type::unknown(),
| LegacyGenericContextError::DuplicateTypevar(_)
| LegacyGenericContextError::TypeVarTupleMustBeUnpacked => Type::unknown(),
LegacyGenericContextError::NotYetSupported => {
todo_type!("ParamSpecs and TypeVarTuples")
}
Expand Down Expand Up @@ -15301,6 +15304,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
typevar.name(db),
));
}
} else if let Type::NominalInstance(instance) = argument_ty
&& instance.has_known_class(db, KnownClass::TypeVarTuple)
{
return Err(LegacyGenericContextError::TypeVarTupleMustBeUnpacked);
} else if any_over_type(
db,
argument_ty,
Expand Down Expand Up @@ -15353,7 +15360,18 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
},
))
}
Err(error) => Ok(error.into_type()),
Err(LegacyGenericContextError::TypeVarTupleMustBeUnpacked) => {
Err(SubscriptError::new(
Type::unknown(),
SubscriptErrorKind::TypeVarTupleNotUnpacked {
origin: LegacyGenericOrigin::Generic,
},
))
}
Err(
error @ (LegacyGenericContextError::NotYetSupported
| LegacyGenericContextError::VariadicTupleArguments),
) => Ok(error.into_type()),
}
}
Type::SpecialForm(SpecialFormType::Protocol) => {
Expand All @@ -15379,7 +15397,18 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
},
))
}
Err(error) => Ok(error.into_type()),
Err(LegacyGenericContextError::TypeVarTupleMustBeUnpacked) => {
Err(SubscriptError::new(
Type::unknown(),
SubscriptErrorKind::TypeVarTupleNotUnpacked {
origin: LegacyGenericOrigin::Protocol,
},
))
}
Err(
error @ (LegacyGenericContextError::NotYetSupported
| LegacyGenericContextError::VariadicTupleArguments),
) => Ok(error.into_type()),
}
}
Type::SpecialForm(SpecialFormType::Concatenate) => {
Expand Down
10 changes: 10 additions & 0 deletions crates/ty_python_semantic/src/types/subscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub(crate) enum SubscriptErrorKind<'db> {
origin: LegacyGenericOrigin,
typevar_name: &'db str,
},
/// A `TypeVarTuple` was provided to `Generic` or `Protocol` without being unpacked.
TypeVarTupleNotUnpacked { origin: LegacyGenericOrigin },
}

impl<'db> SubscriptError<'db> {
Expand Down Expand Up @@ -334,6 +336,14 @@ impl<'db> SubscriptErrorKind<'db> {
));
}
}
Self::TypeVarTupleNotUnpacked { origin } => {
if let Some(builder) = context.report_lint(&INVALID_GENERIC_CLASS, subscript) {
builder.into_diagnostic(format_args!(
"`TypeVarTuple` must be unpacked with `*` or `Unpack[]` when \
used as an argument to `{origin}`",
));
}
}
}
}

Expand Down