From 0a2536736b7ae15e836c68d40d754a45f0455250 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 25 Nov 2025 14:54:04 +0100 Subject: [PATCH] Better diagnostic message --- .../resources/mdtest/implicit_type_aliases.md | 11 ++++++++++- .../src/types/infer/builder/type_expression.rs | 13 +++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/implicit_type_aliases.md b/crates/ty_python_semantic/resources/mdtest/implicit_type_aliases.md index c975b14db2..c2f02be9d5 100644 --- a/crates/ty_python_semantic/resources/mdtest/implicit_type_aliases.md +++ b/crates/ty_python_semantic/resources/mdtest/implicit_type_aliases.md @@ -403,7 +403,6 @@ reveal_type(ListOrTuple) # revealed: types.UnionType reveal_type(ListOrTupleLegacy) # revealed: types.UnionType reveal_type(MyCallable) # revealed: GenericAlias reveal_type(AnnotatedType) # revealed: -# TODO: This should ideally be `T@TransparentAlias` reveal_type(TransparentAlias) # revealed: typing.TypeVar reveal_type(MyOptional) # revealed: types.UnionType @@ -660,6 +659,16 @@ def _( 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 #### Shadowed definitions diff --git a/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs b/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs index 3eb26d6574..3256e13dc7 100644 --- a/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs +++ b/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs @@ -762,19 +762,20 @@ impl<'db> TypeInferenceBuilder<'db, '_> { ) -> Type<'db> { let db = self.db(); - let Some(type_alias_definition) = typevar_binding_context else { - // TODO + let Some(typevar_binding_context) = typevar_binding_context else { if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) { - builder.into_diagnostic( - "Cannot specialize implicit type alias with unknown definition", - ); + let mut diag = builder.into_diagnostic(format_args!( + "`{}` 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(); }; let generic_type_alias = value_ty.apply_type_mapping( db, - &TypeMapping::BindLegacyTypevars(BindingContext::Definition(type_alias_definition)), + &TypeMapping::BindLegacyTypevars(BindingContext::Definition(typevar_binding_context)), TypeContext::default(), );