Better diagnostic message

This commit is contained in:
David Peter 2025-11-25 14:54:04 +01:00
parent 6aaa9d784a
commit 0a2536736b
2 changed files with 17 additions and 7 deletions

View File

@ -403,7 +403,6 @@ reveal_type(ListOrTuple) # revealed: types.UnionType
reveal_type(ListOrTupleLegacy) # revealed: types.UnionType reveal_type(ListOrTupleLegacy) # revealed: types.UnionType
reveal_type(MyCallable) # revealed: GenericAlias reveal_type(MyCallable) # revealed: GenericAlias
reveal_type(AnnotatedType) # revealed: <typing.Annotated special form> reveal_type(AnnotatedType) # revealed: <typing.Annotated special form>
# TODO: This should ideally be `T@TransparentAlias`
reveal_type(TransparentAlias) # revealed: typing.TypeVar reveal_type(TransparentAlias) # revealed: typing.TypeVar
reveal_type(MyOptional) # revealed: types.UnionType reveal_type(MyOptional) # revealed: types.UnionType
@ -660,6 +659,16 @@ def _(
reveal_type(specialized) # revealed: Unknown reveal_type(specialized) # revealed: Unknown
``` ```
Similarly, if you try to specialize a union type without a binding context, we emit an error:
```py
# error: [invalid-type-form] "`types.UnionType` is not subscriptable"
x: (list[T] | set[T])[int]
def _():
reveal_type(x) # revealed: Unknown
```
### Multiple definitions ### Multiple definitions
#### Shadowed definitions #### Shadowed definitions

View File

@ -762,19 +762,20 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
) -> Type<'db> { ) -> Type<'db> {
let db = self.db(); let db = self.db();
let Some(type_alias_definition) = typevar_binding_context else { let Some(typevar_binding_context) = typevar_binding_context else {
// TODO
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) { if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
builder.into_diagnostic( let mut diag = builder.into_diagnostic(format_args!(
"Cannot specialize implicit type alias with unknown definition", "`{}` is not subscriptable",
); value_ty.display(db)
));
diag.info("Consider creating a type alias to create a binding context for the type variable(s)");
} }
return Type::unknown(); return Type::unknown();
}; };
let generic_type_alias = value_ty.apply_type_mapping( let generic_type_alias = value_ty.apply_type_mapping(
db, db,
&TypeMapping::BindLegacyTypevars(BindingContext::Definition(type_alias_definition)), &TypeMapping::BindLegacyTypevars(BindingContext::Definition(typevar_binding_context)),
TypeContext::default(), TypeContext::default(),
); );